melhorias de tipagem
This commit is contained in:
parent
1d5d3a48b4
commit
afa28a0699
32 changed files with 435 additions and 486 deletions
44
src/pilao-de-dados/_deletar_registros.ts
Normal file
44
src/pilao-de-dados/_deletar_registros.ts
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import node_fetch from "cross-fetch"
|
||||
import type { tipoResposta } from "p-respostas"
|
||||
import { respostaComuns } from "p-respostas"
|
||||
import { z } from "zod"
|
||||
import { PREFIXO, baseUrlPilao, 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,
|
||||
tabela,
|
||||
}: z.infer<typeof zp_deletar_registros>): Promise<tipoResposta<true>> => {
|
||||
const url = new URL(
|
||||
`${baseUrlPilao(
|
||||
emDesenvolvimento,
|
||||
)}${PREFIXO}/${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)
|
||||
}
|
||||
69
src/pilao-de-dados/_enviar_registros.ts
Normal file
69
src/pilao-de-dados/_enviar_registros.ts
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
import node_fetch from "cross-fetch"
|
||||
import type { tipoResposta } from "p-respostas"
|
||||
import { respostaComuns } from "p-respostas"
|
||||
import { z } from "zod"
|
||||
import {
|
||||
PREFIXO,
|
||||
baseUrlPilao,
|
||||
z_tipo_coluna_base_dados,
|
||||
type zp_produto_conta,
|
||||
} 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 =
|
||||
({ conta, produto, emDesenvolvimento }: z.infer<typeof zp_produto_conta>) =>
|
||||
async ({
|
||||
registros,
|
||||
tabela,
|
||||
}: z.infer<typeof zp_enviar_registros>): Promise<tipoResposta<true>> => {
|
||||
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 = await node_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<true>)
|
||||
|
||||
if (resp.eErro) {
|
||||
return resp
|
||||
}
|
||||
}
|
||||
|
||||
return respostaComuns.valor(true)
|
||||
}
|
||||
72
src/pilao-de-dados/_serie_consultar.ts
Normal file
72
src/pilao-de-dados/_serie_consultar.ts
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
import node_fetch from "cross-fetch"
|
||||
import type { tipoResposta } from "p-respostas"
|
||||
import { respostaComuns } from "p-respostas"
|
||||
import { z } from "zod"
|
||||
import {
|
||||
PREFIXO,
|
||||
baseUrlPilao,
|
||||
tiposSeriesAgregacoes,
|
||||
type zp_produto_conta,
|
||||
} from "./variaveis"
|
||||
|
||||
export const zp_serie_registrar = z.object({
|
||||
tabela: z.string(),
|
||||
colanuEixoX: z.string(),
|
||||
colunaAgrupamento: z.string().array().optional(),
|
||||
agregacao: tiposSeriesAgregacoes,
|
||||
})
|
||||
|
||||
export const serie_consultar =
|
||||
(cliente: z.infer<typeof zp_produto_conta>) =>
|
||||
(parametros: z.infer<typeof zp_serie_registrar>) => {
|
||||
const dados = async (): Promise<
|
||||
tipoResposta<{
|
||||
registros: any[]
|
||||
legenda: string
|
||||
serie: z.infer<typeof zp_serie_registrar>
|
||||
}>
|
||||
> => {
|
||||
const url = new URL(
|
||||
`${baseUrlPilao(cliente.emDesenvolvimento)}${`${PREFIXO}/${
|
||||
tiposSeriesAgregacoes.enum.contagem
|
||||
}/${cliente.produto}/${cliente.conta}`}`,
|
||||
)
|
||||
|
||||
const resp = await node_fetch(url.toString(), {
|
||||
method: "POST",
|
||||
body: JSON.stringify(parametros),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
})
|
||||
.then((r) => r.json())
|
||||
.catch((e) =>
|
||||
respostaComuns.erro("Erro ao enviar registros", [e.message]),
|
||||
)
|
||||
.then((r) => r as tipoResposta<any>)
|
||||
|
||||
return resp
|
||||
}
|
||||
|
||||
const url = (): string => {
|
||||
const pr = {
|
||||
cliente,
|
||||
parametros,
|
||||
}
|
||||
|
||||
const vUrl = new URL(
|
||||
`${
|
||||
cliente.emDesenvolvimento
|
||||
? "http://127.0.0.1:5081"
|
||||
: "https://carro-de-boi.idz.one"
|
||||
}${PREFIXO}/${tiposSeriesAgregacoes.enum.contagem}`,
|
||||
)
|
||||
|
||||
const serie = encodeURIComponent(JSON.stringify(pr, null, 2))
|
||||
|
||||
return `${vUrl.href}?serie=${serie}`
|
||||
}
|
||||
|
||||
return {
|
||||
dados,
|
||||
url,
|
||||
}
|
||||
}
|
||||
|
|
@ -1,52 +0,0 @@
|
|||
import node_fetch from "cross-fetch"
|
||||
import type { tipoResposta } from "p-respostas"
|
||||
import { respostaComuns } from "p-respostas"
|
||||
import { z } from "zod"
|
||||
import { PREFIXO, baseUrlPilao, 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 = async ({
|
||||
emDesenvolvimento,
|
||||
cliente: { conta, produto },
|
||||
parametros: { codigos, tabela },
|
||||
}: {
|
||||
emDesenvolvimento?: boolean | undefined | null
|
||||
|
||||
/** Identificação do cliente */
|
||||
cliente: z.infer<typeof zp_produto_conta>
|
||||
/** Parametros da função */
|
||||
|
||||
parametros: z.infer<typeof zp_deletar_registros>
|
||||
}): Promise<tipoResposta<true>> => {
|
||||
const url = new URL(
|
||||
`${baseUrlPilao(
|
||||
emDesenvolvimento,
|
||||
)}${PREFIXO}/${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)
|
||||
}
|
||||
|
|
@ -1,76 +0,0 @@
|
|||
import node_fetch from "cross-fetch"
|
||||
import type { tipoResposta } from "p-respostas"
|
||||
import { respostaComuns } from "p-respostas"
|
||||
import { z } from "zod"
|
||||
import {
|
||||
PREFIXO,
|
||||
baseUrlPilao,
|
||||
z_tipo_coluna_base_dados,
|
||||
type zp_produto_conta,
|
||||
} 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 = async ({
|
||||
emDesenvolvimento,
|
||||
cliente: { conta, produto },
|
||||
parametros: { registros, tabela },
|
||||
}: {
|
||||
emDesenvolvimento?: boolean | undefined | null
|
||||
|
||||
/** Identificação do cliente */
|
||||
cliente: z.infer<typeof zp_produto_conta>
|
||||
/** Parametros da função */
|
||||
|
||||
parametros: z.infer<typeof zp_enviar_registros>
|
||||
}): Promise<tipoResposta<true>> => {
|
||||
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 = await node_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<true>)
|
||||
|
||||
if (resp.eErro) {
|
||||
return resp
|
||||
}
|
||||
}
|
||||
|
||||
return respostaComuns.valor(true)
|
||||
}
|
||||
|
|
@ -1,12 +1,12 @@
|
|||
import { tiposSeriesAgregacoes, validarZ, zp_produto_conta } from "./_variaveis"
|
||||
import { deletar_registros, zp_deletar_registros } from "./deletar_registros"
|
||||
import { deletar_registros, zp_deletar_registros } from "./_deletar_registros"
|
||||
import {
|
||||
enviar_registros,
|
||||
zp_enviar_registros,
|
||||
zp_registrar_base_dados,
|
||||
} from "./enviar_registros"
|
||||
} from "./_enviar_registros"
|
||||
import { tiposSeriesAgregacoes, validarZ, zp_produto_conta } from "./variaveis"
|
||||
|
||||
import { serie_consultar, zp_serie_registrar } from "./serie_consultar"
|
||||
import { serie_consultar, zp_serie_registrar } from "./_serie_consultar"
|
||||
|
||||
export { tiposSeriesAgregacoes }
|
||||
|
||||
|
|
|
|||
|
|
@ -1,80 +0,0 @@
|
|||
import node_fetch from "cross-fetch"
|
||||
import type { tipoResposta } from "p-respostas"
|
||||
import { respostaComuns } from "p-respostas"
|
||||
import { z } from "zod"
|
||||
import {
|
||||
PREFIXO,
|
||||
baseUrlPilao,
|
||||
tiposSeriesAgregacoes,
|
||||
type zp_produto_conta,
|
||||
} from "./_variaveis"
|
||||
|
||||
export const zp_serie_registrar = z.object({
|
||||
tabela: z.string(),
|
||||
colanuEixoX: z.string(),
|
||||
colunaAgrupamento: z.string().array().optional(),
|
||||
agregacao: tiposSeriesAgregacoes,
|
||||
})
|
||||
|
||||
export const serie_consultar = ({
|
||||
emDesenvolvimento,
|
||||
cliente,
|
||||
parametros,
|
||||
}: {
|
||||
emDesenvolvimento?: boolean | undefined | null
|
||||
|
||||
/** Identificação do cliente */
|
||||
cliente: z.infer<typeof zp_produto_conta>
|
||||
parametros: z.infer<typeof zp_serie_registrar>
|
||||
}) => {
|
||||
const dados = async (): Promise<
|
||||
tipoResposta<{
|
||||
registros: any[]
|
||||
legenda: string
|
||||
serie: z.infer<typeof zp_serie_registrar>
|
||||
}>
|
||||
> => {
|
||||
const url = new URL(
|
||||
`${baseUrlPilao(emDesenvolvimento)}${`${PREFIXO}/${
|
||||
tiposSeriesAgregacoes.enum.contagem
|
||||
}/${cliente.produto}/${cliente.conta}`}`,
|
||||
)
|
||||
|
||||
const resp = await node_fetch(url.toString(), {
|
||||
method: "POST",
|
||||
body: JSON.stringify(parametros),
|
||||
headers: { "Content-Type": "application/json" },
|
||||
})
|
||||
.then((r) => r.json())
|
||||
.catch((e) =>
|
||||
respostaComuns.erro("Erro ao enviar registros", [e.message]),
|
||||
)
|
||||
.then((r) => r as tipoResposta<any>)
|
||||
|
||||
return resp
|
||||
}
|
||||
|
||||
const url = (): string => {
|
||||
const pr = {
|
||||
cliente,
|
||||
parametros,
|
||||
}
|
||||
|
||||
const vUrl = new URL(
|
||||
`${
|
||||
emDesenvolvimento
|
||||
? "http://127.0.0.1:5081"
|
||||
: "https://carro-de-boi.idz.one"
|
||||
}${PREFIXO}/${tiposSeriesAgregacoes.enum.contagem}`,
|
||||
)
|
||||
|
||||
const serie = encodeURIComponent(JSON.stringify(pr, null, 2))
|
||||
|
||||
return `${vUrl.href}?serie=${serie}`
|
||||
}
|
||||
|
||||
return {
|
||||
dados,
|
||||
url,
|
||||
}
|
||||
}
|
||||
|
|
@ -24,6 +24,7 @@ export const validarZ = <T>(
|
|||
export const zp_produto_conta = z.object({
|
||||
produto: z.string(),
|
||||
conta: z.string(),
|
||||
emDesenvolvimento: z.boolean().optional(),
|
||||
})
|
||||
|
||||
export const z_tipo_coluna_base_dados = z.enum([
|
||||
Loading…
Add table
Add a link
Reference in a new issue