build
This commit is contained in:
parent
93d3caa378
commit
2cf1dfe75d
16 changed files with 149 additions and 201 deletions
|
|
@ -19,7 +19,8 @@ export const zp_enviar_registros = z.object({
|
|||
});
|
||||
class ClassPilaoEnviar {
|
||||
constructor({ conta, produto, emDesenvolvimento, ver_log, }) {
|
||||
this.__registros = [];
|
||||
this.__registrosParaEnvio = [];
|
||||
this.__codigosParaDeletar = [];
|
||||
this.__conta = conta;
|
||||
this.__produto = produto;
|
||||
this.__emDesenvolvimento = emDesenvolvimento;
|
||||
|
|
@ -29,12 +30,12 @@ class ClassPilaoEnviar {
|
|||
this.__tabela = tabela;
|
||||
return this;
|
||||
}
|
||||
adicionarRegistro(...registro) {
|
||||
this.__registros.push(...registro);
|
||||
adicionarRegistroParaEnviar(...registro) {
|
||||
this.__registrosParaEnvio.push(...registro);
|
||||
return this;
|
||||
}
|
||||
async enviar() {
|
||||
const registros = this.__registros;
|
||||
async __salvar_enviar_registros() {
|
||||
const registros = this.__registrosParaEnvio;
|
||||
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}".`);
|
||||
|
|
@ -57,5 +58,35 @@ class ClassPilaoEnviar {
|
|||
}
|
||||
return respostaComuns.valor(true);
|
||||
}
|
||||
async __salvar_deletar_registros() {
|
||||
const codigos = [...this.__codigosParaDeletar];
|
||||
const url = new URL(`${urlPilao(this.__emDesenvolvimento).api}/deletar_registros/${this.__produto}/${this.__conta}`);
|
||||
const tamanhoBlocos = 1000;
|
||||
while (codigos.length > 0) {
|
||||
const bloco = codigos.splice(0, tamanhoBlocos);
|
||||
const resp = await node_fetch(url.toString(), {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ tabela: this.__tabela, codigos: 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;
|
||||
}
|
||||
}
|
||||
this.__codigosParaDeletar;
|
||||
return respostaComuns.valor(true);
|
||||
}
|
||||
async salvar() {
|
||||
const re = await this.__salvar_enviar_registros();
|
||||
if (re.eErro)
|
||||
return re;
|
||||
const rd = await this.__salvar_deletar_registros();
|
||||
if (rd.eErro)
|
||||
return rd;
|
||||
return respostaComuns.valor(true);
|
||||
}
|
||||
}
|
||||
export const PilaoEnviar = (_) => new ClassPilaoEnviar(_);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue