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

@ -51,4 +51,17 @@ export declare const zp_enviar_registros: z.ZodObject<{
tipo?: "texto" | "numero" | "confirmacao" | "lista_texto" | "lista_numero" | "lista_mes" | "lista_data" | "mes" | "data" | null | undefined;
}>[];
}>;
export declare const enviar_registros: ({ conta, produto, emDesenvolvimento, ver_log, }: z.infer<typeof zp_produto_conta>) => ({ registros: registros_entrada, tabela, }: z.infer<typeof zp_enviar_registros>) => Promise<tipoResposta<true>>;
declare 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>);
tabela(tabela: string): this;
adicionarRegistro(...registro: z.infer<typeof zp_enviar_registros>["registros"]): this;
enviar(): Promise<tipoResposta<true>>;
}
export declare const PilaoEnviar: (_: z.infer<typeof zp_produto_conta>) => ClassPilaoEnviar;
export {};

View file

@ -17,27 +17,45 @@ export const zp_enviar_registros = z.object({
tipo: z_tipo_coluna_base_dados.optional().nullable(),
}))),
});
export const enviar_registros = ({ conta, produto, emDesenvolvimento, ver_log, }) => async ({ registros: registros_entrada, tabela, }) => {
const registros = [...registros_entrada];
const url = new URL(`${urlPilao(emDesenvolvimento).api}/${Object.keys({ enviar_registros })[0]}/${produto}/${conta}`);
if (ver_log)
console.log(`[PILÃO]: Enviando "${registros.length}" registros na tabela "${tabela}" para "${url}".`);
const tamanhoBlocos = 1000;
while (registros.length > 0) {
const bloco = registros
.splice(0, tamanhoBlocos)
.map((r) => Object.fromEntries(Object.entries(r).map(([k, v]) => [k, v === undefined ? null : v])));
const resp = await node_fetch(url.toString(), {
method: "POST",
body: JSON.stringify({ tabela, registros: bloco }),
headers: { "Content-Type": "application/json" },
})
.then((r) => r.json())
.catch((e) => respostaComuns.erro("Erro ao enviar registros", [e.message]))
.then((r) => r);
if (resp.eErro) {
return resp;
}
class ClassPilaoEnviar {
constructor({ conta, produto, emDesenvolvimento, ver_log, }) {
this.__registros = [];
this.__conta = conta;
this.__produto = produto;
this.__emDesenvolvimento = emDesenvolvimento;
this.__ver_log = ver_log;
}
return respostaComuns.valor(true);
};
tabela(tabela) {
this.__tabela = tabela;
return this;
}
adicionarRegistro(...registro) {
this.__registros.push(...registro);
return this;
}
async enviar() {
const registros = this.__registros;
const url = new URL(`${urlPilao(this.__emDesenvolvimento).api}/enviar_registros/${this.__produto}/${this.__conta}`);
if (this.__ver_log)
console.log(`[PILÃO]: Enviando "${registros.length}" registros na tabela "${this.__tabela}" para "${url}".`);
const tamanhoBlocos = 1000;
while (registros.length > 0) {
const bloco = registros
.splice(0, tamanhoBlocos)
.map((r) => Object.fromEntries(Object.entries(r).map(([k, v]) => [k, v === undefined ? null : v])));
const resp = await node_fetch(url.toString(), {
method: "POST",
body: JSON.stringify({ tabela: this.__tabela, registros: bloco }),
headers: { "Content-Type": "application/json" },
})
.then((r) => r.json())
.catch((e) => respostaComuns.erro("Erro ao enviar registros", [e.message]))
.then((r) => r);
if (resp.eErro) {
return resp;
}
}
return respostaComuns.valor(true);
}
}
export const PilaoEnviar = (_) => new ClassPilaoEnviar(_);

View file

@ -307,7 +307,17 @@ export declare const pPilao: {
tipo: "texto" | "numero" | "confirmacao" | "lista_texto" | "lista_numero" | "lista_mes" | "lista_data" | "mes" | "data";
}[];
}>;
enviar_registros: ({ conta, produto, emDesenvolvimento, ver_log, }: import("zod").TypeOf<typeof zp_produto_conta>) => ({ registros: registros_entrada, tabela, }: import("zod").TypeOf<typeof zp_enviar_registros>) => Promise<import("p-respostas").tipoResposta<true>>;
PilaoEnviar: (_: import("zod").TypeOf<typeof zp_produto_conta>) => {
__conta: string | undefined;
__produto: string | undefined;
__emDesenvolvimento: boolean | undefined;
__ver_log: boolean | undefined;
__tabela: string | undefined;
__registros: import("zod").TypeOf<typeof zp_enviar_registros>["registros"];
tabela(tabela: string): any;
adicionarRegistro(...registro: import("zod").TypeOf<typeof zp_enviar_registros>["registros"]): any;
enviar(): Promise<import("p-respostas").tipoResposta<true>>;
};
zp_enviar_registros: import("zod").ZodObject<{
tabela: import("zod").ZodString;
registros: import("zod").ZodArray<import("zod").ZodRecord<import("zod").ZodString, import("zod").ZodObject<{

View file

@ -1,12 +1,12 @@
import { deletar_registros, zp_deletar_registros } from "./_deletar_registros";
export { PREFIXO_PILAO, urlPilao } from "./variaveis";
import { enviar_registros, zp_enviar_registros, zp_registrar_base_dados, } from "./_enviar_registros";
import { PilaoEnviar, zp_enviar_registros, zp_registrar_base_dados, } from "./_enviar_registros";
import { operadores_permitidos_por_tipo, operadores_pilao, validarZ, zp_produto_conta, } from "./variaveis";
import { serie_consultar, z_filtro } from "./_serie_consultar";
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,