From d8a5a1238768e0d29964ad1a7d37cd04534d8681 Mon Sep 17 00:00:00 2001 From: "Luiz H. R. Silva" Date: Fri, 14 Jun 2024 13:50:29 -0300 Subject: [PATCH] . --- .../pilao-de-dados/enviar_registros.js | 25 ++++++++++------ .../pilao-de-dados/enviar_registros.js | 21 ++++++++++---- package.json | 2 +- src/pilao-de-dados/enviar_registros.ts | 29 +++++++++++++------ 4 files changed, 53 insertions(+), 24 deletions(-) diff --git a/dist-import/pilao-de-dados/enviar_registros.js b/dist-import/pilao-de-dados/enviar_registros.js index 26f860f..163c952 100644 --- a/dist-import/pilao-de-dados/enviar_registros.js +++ b/dist-import/pilao-de-dados/enviar_registros.js @@ -19,13 +19,20 @@ export const enviar_registros = ({ emDesenvolvimento, cliente: { conta, produto const url = new URL(`${emDesenvolvimento ? "http://127.0.0.1:5080" : "https://carro-de-boi.idz.one"}${PREFIXO}/enviar-registro/${produto}/${conta}`); - const resp = yield fetch(url.toString(), { - method: "POST", - body: JSON.stringify({ tabela, registros }), - headers: { "Content-Type": "application/json" }, - }) - .then((r) => r.json()) - .catch((e) => respostaComuns.erro("Erro ao enviar registros", [e.message])) - .then((r) => r); - return resp; + const tamanhoBlocos = 1000; + while (registros.length > 0) { + const bloco = registros.splice(0, tamanhoBlocos); + const resp = yield 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; + } + } + return respostaComuns.valor(true); }); diff --git a/dist-require/pilao-de-dados/enviar_registros.js b/dist-require/pilao-de-dados/enviar_registros.js index e5ada83..2a1cfee 100644 --- a/dist-require/pilao-de-dados/enviar_registros.js +++ b/dist-require/pilao-de-dados/enviar_registros.js @@ -48,24 +48,35 @@ exports.zp_enviar_registros = zod_1.z.object({ var enviar_registros = function (_a) { var emDesenvolvimento = _a.emDesenvolvimento, _b = _a.cliente, conta = _b.conta, produto = _b.produto, _c = _a.parametros, registros = _c.registros, tabela = _c.tabela; return __awaiter(void 0, void 0, void 0, function () { - var url, resp; + var url, tamanhoBlocos, bloco, resp; return __generator(this, function (_d) { switch (_d.label) { case 0: url = new URL("".concat(emDesenvolvimento ? "http://127.0.0.1:5080" : "https://carro-de-boi.idz.one").concat(_variaveis_1.PREFIXO, "/enviar-registro/").concat(produto, "/").concat(conta)); + tamanhoBlocos = 1000; + _d.label = 1; + case 1: + if (!(registros.length > 0)) return [3 /*break*/, 3]; + bloco = registros.splice(0, tamanhoBlocos); return [4 /*yield*/, fetch(url.toString(), { method: "POST", - body: JSON.stringify({ tabela: tabela, registros: registros }), + body: JSON.stringify({ tabela: tabela, registros: bloco }), headers: { "Content-Type": "application/json" }, }) .then(function (r) { return r.json(); }) - .catch(function (e) { return p_respostas_1.respostaComuns.erro("Erro ao enviar registros", [e.message]); }) + .catch(function (e) { + return p_respostas_1.respostaComuns.erro("Erro ao enviar registros", [e.message]); + }) .then(function (r) { return r; })]; - case 1: + case 2: resp = _d.sent(); - return [2 /*return*/, resp]; + if (resp.eErro) { + return [2 /*return*/, resp]; + } + return [3 /*break*/, 1]; + case 3: return [2 /*return*/, p_respostas_1.respostaComuns.valor(true)]; } }); }); diff --git a/package.json b/package.json index 9ca24dc..cf2c7ac 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "p-drives", - "version": "0.56.0", + "version": "0.57.0", "description": "", "main": "src/index.ts", "exports": { diff --git a/src/pilao-de-dados/enviar_registros.ts b/src/pilao-de-dados/enviar_registros.ts index 6a97d02..896fa64 100644 --- a/src/pilao-de-dados/enviar_registros.ts +++ b/src/pilao-de-dados/enviar_registros.ts @@ -30,14 +30,25 @@ export const enviar_registros = async ({ }${PREFIXO}/enviar-registro/${produto}/${conta}`, ) - const resp = await fetch(url.toString(), { - method: "POST", - body: JSON.stringify({ tabela, registros }), - headers: { "Content-Type": "application/json" }, - }) - .then((r) => r.json()) - .catch((e) => respostaComuns.erro("Erro ao enviar registros", [e.message])) - .then((r) => r as tipoResposta) + const tamanhoBlocos = 1000 - return resp + while (registros.length > 0) { + const bloco = registros.splice(0, tamanhoBlocos) + const resp = await 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 as tipoResposta) + + if (resp.eErro) { + return resp + } + } + + return respostaComuns.valor(true) }