drivers/dist-import/pilao-de-dados/_deletar_registros.js
2024-06-28 11:55:47 -03:00

37 lines
1.8 KiB
JavaScript

var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import node_fetch from "cross-fetch";
import { respostaComuns } from "p-respostas";
import { z } from "zod";
import { PREFIXO_PILAO, baseUrlPilao } from "./variaveis";
//enviar registros para base de dados
export const zp_deletar_registros = z.object({
tabela: z.string(),
codigos: z.array(z.string()),
});
export const deletar_registros = ({ conta, produto, emDesenvolvimento }) => (_a) => __awaiter(void 0, [_a], void 0, function* ({ codigos, tabela, }) {
const url = new URL(`${baseUrlPilao(emDesenvolvimento)}${PREFIXO_PILAO}/${Object.keys({ deletar_registros })[0]}/${produto}/${conta}`);
const tamanhoBlocos = 1000;
while (codigos.length > 0) {
const bloco = codigos.splice(0, tamanhoBlocos);
const resp = yield node_fetch(url.toString(), {
method: "POST",
body: JSON.stringify({ 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;
}
}
return respostaComuns.valor(true);
});