46 lines
2.1 KiB
JavaScript
46 lines
2.1 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 { respostaComuns } from "p-respostas";
|
|
import { z } from "zod";
|
|
import { PREFIXO, baseUrlPilao, z_tipo_coluna_base_dados, } from "./_variaveis";
|
|
export const zp_registrar_base_dados = z.object({
|
|
tabela: z.string(),
|
|
colunas: z.array(z.object({
|
|
coluna: z.string(),
|
|
tipo: z_tipo_coluna_base_dados,
|
|
})),
|
|
});
|
|
//enviar registros para base de dados
|
|
export const zp_enviar_registros = z.object({
|
|
tabela: z.string(),
|
|
registros: z.array(z.record(z.string(), z.object({
|
|
valor: z.any(),
|
|
tipo: z_tipo_coluna_base_dados.optional().nullable(),
|
|
}))),
|
|
});
|
|
export const enviar_registros = ({ emDesenvolvimento, cliente: { conta, produto }, parametros: { registros, tabela }, }) => __awaiter(void 0, void 0, void 0, function* () {
|
|
const url = new URL(`${baseUrlPilao(emDesenvolvimento)}${PREFIXO}/${Object.keys({ enviar_registros })[0]}/${produto}/${conta}`);
|
|
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);
|
|
});
|