add endereco e edita tabela empresas

This commit is contained in:
2025-11-18 11:15:44 -03:00
parent f021e96eb4
commit 029cd9c637
7 changed files with 525 additions and 95 deletions

View File

@@ -15,9 +15,9 @@ import type * as actions_smtp from "../actions/smtp.js";
import type * as actions_utils_nodeCrypto from "../actions/utils/nodeCrypto.js";
import type * as atestadosLicencas from "../atestadosLicencas.js";
import type * as ausencias from "../ausencias.js";
import type * as auth from "../auth.js";
import type * as auth_utils from "../auth/utils.js";
import type * as chamados from "../chamados.js";
import type * as auth from "../auth.js";
import type * as chat from "../chat.js";
import type * as configuracaoEmail from "../configuracaoEmail.js";
import type * as crons from "../crons.js";
@@ -56,14 +56,6 @@ import type {
FunctionReference,
} from "convex/server";
/**
* A utility for referencing Convex functions in your app's API.
*
* Usage:
* ```js
* const myFunctionReference = api.myModule.myFunction;
* ```
*/
declare const fullApi: ApiFromModules<{
"actions/email": typeof actions_email;
"actions/linkPreview": typeof actions_linkPreview;
@@ -72,9 +64,9 @@ declare const fullApi: ApiFromModules<{
"actions/utils/nodeCrypto": typeof actions_utils_nodeCrypto;
atestadosLicencas: typeof atestadosLicencas;
ausencias: typeof ausencias;
auth: typeof auth;
"auth/utils": typeof auth_utils;
chamados: typeof chamados;
auth: typeof auth;
chat: typeof chat;
configuracaoEmail: typeof configuracaoEmail;
crons: typeof crons;
@@ -107,14 +99,30 @@ declare const fullApi: ApiFromModules<{
"utils/getClientIP": typeof utils_getClientIP;
verificarMatriculas: typeof verificarMatriculas;
}>;
declare const fullApiWithMounts: typeof fullApi;
/**
* A utility for referencing Convex functions in your app's public API.
*
* Usage:
* ```js
* const myFunctionReference = api.myModule.myFunction;
* ```
*/
export declare const api: FilterApi<
typeof fullApiWithMounts,
typeof fullApi,
FunctionReference<any, "public">
>;
/**
* A utility for referencing Convex functions in your app's internal API.
*
* Usage:
* ```js
* const myFunctionReference = internal.myModule.myFunction;
* ```
*/
export declare const internal: FilterApi<
typeof fullApiWithMounts,
typeof fullApi,
FunctionReference<any, "internal">
>;

View File

@@ -10,7 +10,6 @@
import {
ActionBuilder,
AnyComponents,
HttpActionBuilder,
MutationBuilder,
QueryBuilder,
@@ -19,15 +18,9 @@ import {
GenericQueryCtx,
GenericDatabaseReader,
GenericDatabaseWriter,
FunctionReference,
} from "convex/server";
import type { DataModel } from "./dataModel.js";
type GenericCtx =
| GenericActionCtx<DataModel>
| GenericMutationCtx<DataModel>
| GenericQueryCtx<DataModel>;
/**
* Define a query in this Convex app's public API.
*
@@ -92,11 +85,12 @@ export declare const internalAction: ActionBuilder<DataModel, "internal">;
/**
* Define an HTTP action.
*
* This function will be used to respond to HTTP requests received by a Convex
* deployment if the requests matches the path and method where this action
* is routed. Be sure to route your action in `convex/http.js`.
* The wrapped function will be used to respond to HTTP requests received
* by a Convex deployment if the requests matches the path and method where
* this action is routed. Be sure to route your httpAction in `convex/http.js`.
*
* @param func - The function. It receives an {@link ActionCtx} as its first argument.
* @param func - The function. It receives an {@link ActionCtx} as its first argument
* and a Fetch API `Request` object as its second.
* @returns The wrapped function. Import this function from `convex/http.js` and route it to hook it up.
*/
export declare const httpAction: HttpActionBuilder;

View File

@@ -16,7 +16,6 @@ import {
internalActionGeneric,
internalMutationGeneric,
internalQueryGeneric,
componentsGeneric,
} from "convex/server";
/**
@@ -81,10 +80,14 @@ export const action = actionGeneric;
export const internalAction = internalActionGeneric;
/**
* Define a Convex HTTP action.
* Define an HTTP action.
*
* @param func - The function. It receives an {@link ActionCtx} as its first argument, and a `Request` object
* as its second.
* @returns The wrapped endpoint function. Route a URL path to this function in `convex/http.js`.
* The wrapped function will be used to respond to HTTP requests received
* by a Convex deployment if the requests matches the path and method where
* this action is routed. Be sure to route your httpAction in `convex/http.js`.
*
* @param func - The function. It receives an {@link ActionCtx} as its first argument
* and a Fetch API `Request` object as its second.
* @returns The wrapped function. Import this function from `convex/http.js` and route it to hook it up.
*/
export const httpAction = httpActionGeneric;

View File

@@ -2,6 +2,7 @@ import { v } from "convex/values";
import { mutation, query } from "./_generated/server";
import { internal } from "./_generated/api";
import { getCurrentUserFunction } from "./auth";
import type { Id } from "./_generated/dataModel";
export const list = query({
args: {},
@@ -33,8 +34,11 @@ export const getById = query({
.query("contatosEmpresa")
.withIndex("by_empresa", (q) => q.eq("empresaId", args.id))
.collect();
const endereco = empresa.enderecoId
? await ctx.db.get(empresa.enderecoId as Id<"enderecos">)
: null;
return { ...empresa, contatos };
return { ...empresa, endereco, contatos };
},
});
@@ -50,13 +54,25 @@ const contatoInput = v.object({
_deleted: v.optional(v.boolean()),
});
const enderecoInput = v.object({
cep: v.string(),
logradouro: v.string(),
numero: v.string(),
complemento: v.optional(v.string()),
bairro: v.string(),
cidade: v.string(),
uf: v.string(),
});
export const create = mutation({
args: {
nome: v.string(),
razao_social: v.string(),
nome_fantasia: v.optional(v.string()),
cnpj: v.string(),
telefone: v.string(),
email: v.string(),
descricao: v.optional(v.string()),
endereco: v.optional(enderecoInput),
contatos: v.optional(v.array(contatoInput)),
},
returns: v.id("empresas"),
@@ -80,17 +96,49 @@ export const create = mutation({
if (cnpjExistente) {
throw new Error("Já existe uma empresa cadastrada com este CNPJ.");
}
let enderecoId: Id<"enderecos"> | undefined;
if (args.endereco) {
enderecoId = await ctx.db.insert("enderecos", {
cep: args.endereco.cep,
logradouro: args.endereco.logradouro,
numero: args.endereco.numero,
complemento: args.endereco.complemento,
bairro: args.endereco.bairro,
cidade: args.endereco.cidade,
uf: args.endereco.uf,
criadoPor: usuarioAtual._id,
atualizadoPor: usuarioAtual._id,
});
}
const empresaId = await ctx.db.insert("empresas", {
nome: args.nome,
const empresaDoc: {
razao_social: string;
nome_fantasia?: string;
cnpj: string;
telefone: string;
email: string;
descricao?: string;
enderecoId?: Id<"enderecos">;
criadoPor: Id<"usuarios">;
} = {
razao_social: args.razao_social,
cnpj: args.cnpj,
telefone: args.telefone,
email: args.email,
descricao: args.descricao,
criadoPor: usuarioAtual._id,
});
};
if (args.nome_fantasia !== undefined) {
empresaDoc.nome_fantasia = args.nome_fantasia;
}
if (args.descricao !== undefined) {
empresaDoc.descricao = args.descricao;
}
if (enderecoId) {
empresaDoc.enderecoId = enderecoId;
}
const empresaId = await ctx.db.insert("empresas", empresaDoc);
if (args.contatos && args.contatos.length > 0) {
for (const contato of args.contatos) {
@@ -113,11 +161,13 @@ export const create = mutation({
export const update = mutation({
args: {
id: v.id("empresas"),
nome: v.string(),
razao_social: v.string(),
nome_fantasia: v.optional(v.string()),
cnpj: v.string(),
telefone: v.string(),
email: v.string(),
descricao: v.optional(v.string()),
endereco: v.optional(enderecoInput),
contatos: v.optional(v.array(contatoInput)),
},
returns: v.null(),
@@ -135,14 +185,71 @@ export const update = mutation({
if (cnpjExistente && cnpjExistente._id !== args.id) {
throw new Error("Já existe uma empresa cadastrada com este CNPJ.");
}
const empresa = await ctx.db.get(args.id);
if (!empresa) {
throw new Error("Empresa não encontrada.");
}
await ctx.db.patch(args.id, {
nome: args.nome,
if (args.endereco) {
if (empresa.enderecoId) {
const usuarioAtual = await getCurrentUserFunction(ctx);
await ctx.db.patch(empresa.enderecoId as Id<"enderecos">, {
cep: args.endereco.cep,
logradouro: args.endereco.logradouro,
numero: args.endereco.numero,
complemento: args.endereco.complemento,
bairro: args.endereco.bairro,
cidade: args.endereco.cidade,
uf: args.endereco.uf,
atualizadoPor: usuarioAtual?._id,
});
} else {
const usuarioAtual = await getCurrentUserFunction(ctx);
if (!usuarioAtual) {
throw new Error("Usuário não autenticado.");
}
const novoEnderecoId: Id<"enderecos"> = await ctx.db.insert("enderecos", {
cep: args.endereco.cep,
logradouro: args.endereco.logradouro,
numero: args.endereco.numero,
complemento: args.endereco.complemento,
bairro: args.endereco.bairro,
cidade: args.endereco.cidade,
uf: args.endereco.uf,
criadoPor: usuarioAtual._id,
atualizadoPor: usuarioAtual._id,
});
await ctx.db.patch(args.id, {
enderecoId: novoEnderecoId,
});
}
}
const patchDoc: {
razao_social: string;
nome_fantasia?: string;
cnpj: string;
telefone: string;
email: string;
descricao?: string;
} = {
razao_social: args.razao_social,
cnpj: args.cnpj,
telefone: args.telefone,
email: args.email,
descricao: args.descricao,
});
};
if (args.nome_fantasia !== undefined) {
patchDoc.nome_fantasia = args.nome_fantasia;
}
if (args.descricao !== undefined) {
patchDoc.descricao = args.descricao;
}
await ctx.db.patch(args.id, patchDoc);
if (!args.contatos) {
return null;

View File

@@ -125,15 +125,28 @@ export default defineSchema({
text: v.string(),
completed: v.boolean(),
}),
enderecos: defineTable({
cep: v.string(),
logradouro: v.string(),
numero: v.string(),
complemento: v.optional(v.string()),
bairro: v.string(),
cidade: v.string(),
uf: v.string(),
criadoPor: v.optional(v.id("usuarios")),
atualizadoPor: v.optional(v.id("usuarios")),
}).index("by_cep", ["cep"]),
empresas: defineTable({
nome: v.string(),
razao_social: v.string(),
nome_fantasia: v.optional(v.string()),
cnpj: v.string(),
telefone: v.string(),
email: v.string(),
descricao: v.optional(v.string()),
enderecoId: v.optional(v.id("enderecos")),
criadoPor: v.optional(v.id("usuarios")),
})
.index("by_nome", ["nome"])
.index("by_razao_social", ["razao_social"])
.index("by_cnpj", ["cnpj"]),
contatosEmpresa: defineTable({
empresaId: v.id("empresas"),