implementado drive pilão de dados

This commit is contained in:
Luiz H. R. Silva 2024-06-14 12:30:08 -03:00
parent a1e543cfb8
commit daae40f4b2
54 changed files with 1476 additions and 19 deletions

View file

@ -0,0 +1,43 @@
import { respostaComuns } from "p-respostas"
import { z } from "zod"
export const zAmbiente = z.enum(["desenvolvimento", "producao"])
export const PREFIXO = "/pilao-de-dados"
export const validarZ = <T>(
zodType: z.ZodType<T, any>,
objeto: any,
mensagem: string,
) => {
const validar = zodType.safeParse(objeto)
if (!validar.success) {
debugger
return respostaComuns.erro(
mensagem,
validar.error.errors.map((e) => `${e.path} ${e.message}`),
)
}
return respostaComuns.valor(validar.data)
}
export const zp_produto_conta = z.object({
produto: z.string(),
conta: z.string(),
})
export const tiposColunas = z.enum([
"texto",
"numero",
"confirmacao",
"lista_texto",
"lista_numero",
])
export const validarColuna = {
texto: z.string().nullable(),
numero: z.number().nullable(),
confirmacao: z.boolean().nullable(),
lista_texto: z.array(z.string()).nullable(),
lista_numero: z.array(z.number()).nullable(),
}

View file

@ -0,0 +1,75 @@
import type { tipoResposta } from "p-respostas"
import { respostaComuns } from "p-respostas"
import { z } from "zod"
import { PREFIXO, type zp_produto_conta } from "./_variaveis"
import type { zp_registrar_serie } from "./registrar_serie"
//consultar compilação
export const zp_consultar_serie = z.object({
identificador: z.string(),
})
export const consultar_serie = ({
emDesenvolvimento,
parametros: { identificador },
cliente: { conta, produto },
}: {
emDesenvolvimento?: boolean | undefined | null
/** Identificação do cliente */
cliente: z.infer<typeof zp_produto_conta>
parametros: z.infer<typeof zp_consultar_serie>
}) => {
const dados = async (): Promise<
tipoResposta<{
registros: any[]
legenda: string
serie: z.infer<typeof zp_registrar_serie>
}>
> => {
const url = new URL(
`${
emDesenvolvimento
? "http://127.0.0.1:5080"
: "https://carro-de-boi.idz.one"
}${`${PREFIXO}/consultar-serie/${produto}/${conta}`}`,
)
const resp = await fetch(url.toString(), {
method: "POST",
body: JSON.stringify({
identificador,
}),
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 vUrl = new URL(
`${
emDesenvolvimento
? "http://127.0.0.1:5081"
: "https://carro-de-boi.idz.one"
}${PREFIXO}/grafico`,
)
vUrl.searchParams.append("produto", produto)
vUrl.searchParams.append("conta", conta)
vUrl.searchParams.append("identificador", identificador)
return vUrl.href
}
return {
dados,
url,
}
}

View file

@ -0,0 +1,43 @@
import type { tipoResposta } from "p-respostas"
import { respostaComuns } from "p-respostas"
import { z } from "zod"
import { PREFIXO, type zp_produto_conta } from "./_variaveis"
//enviar registros para base de dados
export const zp_enviar_registros = z.object({
tabela: z.string(),
registros: z.array(z.any()),
})
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(
`${
emDesenvolvimento
? "http://127.0.0.1:5080"
: "https://carro-de-boi.idz.one"
}${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<true>)
return resp
}

View file

@ -0,0 +1,27 @@
import { tiposColunas, validarZ, zp_produto_conta } from "./_variaveis"
import { consultar_serie, zp_consultar_serie } from "./consultar_serie"
import { enviar_registros, zp_enviar_registros } from "./enviar_registros"
import {
registrar_base_dados,
zp_registrar_base_dados,
} from "./registrar_base_dados"
import { registrar_serie, zp_registrar_serie } from "./registrar_serie"
export { tiposColunas }
export const pPilao = {
registrar_base_dados,
zp_registrar_base_dados,
enviar_registros,
zp_enviar_registros,
registrar_serie,
zp_registrar_serie,
consultar_serie,
zp_consultar_serie,
zp_produto_conta,
validarZ,
}

View file

@ -0,0 +1,47 @@
import type { tipoResposta } from "p-respostas"
import { respostaComuns } from "p-respostas"
import { z } from "zod"
import { PREFIXO, tiposColunas, type zp_produto_conta } from "./_variaveis"
/** Faz o registro de uma nova base de dados configurado a estrutura de colunas */
export const zp_registrar_base_dados = z.object({
tabela: z.string(),
colunas: z.array(
z.object({
coluna: z.string(),
tipo: tiposColunas,
}),
),
})
export const registrar_base_dados = async ({
emDesenvolvimento,
cliente: { conta, produto },
parametros: { colunas, 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_registrar_base_dados>
}): Promise<tipoResposta<true>> => {
const url = new URL(
`${
emDesenvolvimento
? "http://127.0.0.1:5080"
: "https://carro-de-boi.idz.one"
}${`${PREFIXO}/registar-base-dados/${produto}/${conta}`}`,
)
const resp = await fetch(url.toString(), {
method: "POST",
body: JSON.stringify({ tabela, colunas }),
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>)
return resp
}

View file

@ -0,0 +1,59 @@
import { respostaComuns, type tipoResposta } from "p-respostas"
import { z } from "zod"
import { PREFIXO, type zp_produto_conta } from "./_variaveis"
//registrar serie
export const zp_registrar_serie = z.object({
tabela: z.string(),
identificador: z.string(),
colanuEixoX: z.string(),
colunaAgrupamento: z.string(),
agregacao: z.enum(["contagem"]),
})
export const registrar_serie = async ({
emDesenvolvimento,
cliente: { conta, produto },
parametros: {
agregacao,
colanuEixoX,
colunaAgrupamento,
identificador,
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_registrar_serie>
}): Promise<tipoResposta<true>> => {
const url = new URL(
`${
emDesenvolvimento
? "http://127.0.0.1:5080"
: "https://carro-de-boi.idz.one"
}${`${PREFIXO}/registar-serie/${produto}/${conta}`}`,
)
const resp = await fetch(url.toString(), {
method: "POST",
body: JSON.stringify({
tabela,
agregacao,
emDesenvolvimento,
colanuEixoX,
colunaAgrupamento,
identificador,
}),
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>)
return resp
}