refatoração PilaoEnviar

This commit is contained in:
Luiz Silva 2024-10-01 13:38:40 -03:00
parent dc1b5171d7
commit 93d3caa378
11 changed files with 179 additions and 71 deletions

View file

@ -32,28 +32,50 @@ export const zp_enviar_registros = z.object({
),
})
export const enviar_registros =
({
class ClassPilaoEnviar {
__conta: string | undefined
__produto: string | undefined
__emDesenvolvimento: boolean | undefined
__ver_log: boolean | undefined
__tabela: string | undefined
__registros: z.infer<typeof zp_enviar_registros>["registros"] = []
constructor({
conta,
produto,
emDesenvolvimento,
ver_log,
}: z.infer<typeof zp_produto_conta>) =>
async ({
registros: registros_entrada,
tabela,
}: z.infer<typeof zp_enviar_registros>): Promise<tipoResposta<true>> => {
const registros = [...registros_entrada]
}: z.infer<typeof zp_produto_conta>) {
this.__conta = conta
this.__produto = produto
this.__emDesenvolvimento = emDesenvolvimento
this.__ver_log = ver_log
}
tabela(tabela: string) {
this.__tabela = tabela
return this
}
adicionarRegistro(
...registro: z.infer<typeof zp_enviar_registros>["registros"]
) {
this.__registros.push(...registro)
return this
}
async enviar(): Promise<tipoResposta<true>> {
const registros = this.__registros
const url = new URL(
`${
urlPilao(emDesenvolvimento).api
}/${Object.keys({ enviar_registros })[0]}/${produto}/${conta}`,
urlPilao(this.__emDesenvolvimento).api
}/enviar_registros/${this.__produto}/${this.__conta}`,
)
if (ver_log)
if (this.__ver_log)
console.log(
`[PILÃO]: Enviando "${registros.length}" registros na tabela "${tabela}" para "${url}".`,
`[PILÃO]: Enviando "${registros.length}" registros na tabela "${this.__tabela}" para "${url}".`,
)
const tamanhoBlocos = 1000
@ -69,7 +91,7 @@ export const enviar_registros =
const resp = await node_fetch(url.toString(), {
method: "POST",
body: JSON.stringify({ tabela, registros: bloco }),
body: JSON.stringify({ tabela: this.__tabela, registros: bloco }),
headers: { "Content-Type": "application/json" },
})
.then((r) => r.json())
@ -85,3 +107,7 @@ export const enviar_registros =
return respostaComuns.valor(true)
}
}
export const PilaoEnviar = (_: z.infer<typeof zp_produto_conta>) =>
new ClassPilaoEnviar(_)

View file

@ -1,7 +1,7 @@
import { deletar_registros, zp_deletar_registros } from "./_deletar_registros"
export { PREFIXO_PILAO, urlPilao } from "./variaveis"
import {
enviar_registros,
PilaoEnviar,
zp_enviar_registros,
zp_registrar_base_dados,
} from "./_enviar_registros"
@ -17,7 +17,7 @@ import { extruturas_de_campos, visoes } from "./visoes"
export const pPilao = {
zp_registrar_base_dados,
enviar_registros,
PilaoEnviar,
zp_enviar_registros,
serie_consultar,
zp_produto_conta,