This commit is contained in:
Luiz Silva 2024-10-01 14:19:18 -03:00
parent 93d3caa378
commit 2cf1dfe75d
16 changed files with 149 additions and 201 deletions

View file

@ -1,47 +0,0 @@
import node_fetch from "cross-fetch"
import type { tipoResposta } from "p-respostas"
import { respostaComuns } from "p-respostas"
import { z } from "zod"
import { urlPilao, type zp_produto_conta } 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 }: z.infer<typeof zp_produto_conta>) =>
async ({
codigos: codigos_entrada,
tabela,
}: z.infer<typeof zp_deletar_registros>): Promise<tipoResposta<true>> => {
const codigos = [...codigos_entrada]
const url = new URL(
`${
urlPilao(emDesenvolvimento).api
}/${Object.keys({ deletar_registros })[0]}/${produto}/${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, codigos: 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<true>)
if (resp.eErro) {
return resp
}
}
return respostaComuns.valor(true)
}

View file

@ -38,7 +38,8 @@ class ClassPilaoEnviar {
__emDesenvolvimento: boolean | undefined
__ver_log: boolean | undefined
__tabela: string | undefined
__registros: z.infer<typeof zp_enviar_registros>["registros"] = []
__registrosParaEnvio: z.infer<typeof zp_enviar_registros>["registros"] = []
__codigosParaDeletar: string[] = []
constructor({
conta,
@ -57,15 +58,15 @@ class ClassPilaoEnviar {
return this
}
adicionarRegistro(
adicionarRegistroParaEnviar(
...registro: z.infer<typeof zp_enviar_registros>["registros"]
) {
this.__registros.push(...registro)
this.__registrosParaEnvio.push(...registro)
return this
}
async enviar(): Promise<tipoResposta<true>> {
const registros = this.__registros
async __salvar_enviar_registros(): Promise<tipoResposta<true>> {
const registros = this.__registrosParaEnvio
const url = new URL(
`${
@ -107,6 +108,50 @@ 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 as tipoResposta<true>)
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 = (_: z.infer<typeof zp_produto_conta>) =>

View file

@ -1,4 +1,3 @@
import { deletar_registros, zp_deletar_registros } from "./_deletar_registros"
export { PREFIXO_PILAO, urlPilao } from "./variaveis"
import {
PilaoEnviar,
@ -18,12 +17,12 @@ import { extruturas_de_campos, visoes } from "./visoes"
export const pPilao = {
zp_registrar_base_dados,
PilaoEnviar,
zp_enviar_registros,
serie_consultar,
zp_produto_conta,
validarZ,
deletar_registros,
zp_deletar_registros,
operadores_pilao,
operadores_permitidos_por_tipo,
z_filtro,