feat: add empresa and contatos
- Introduced a new utility function `maskCNPJ` for formatting CNPJ values. - Updated the dashboard pages to replace icons and improve layout, including the addition of a link to manage companies. - Enhanced the display of upcoming features for both the Licitações and Programas Esportivos modules, indicating their development status.
This commit is contained in:
185
packages/backend/convex/empresas.ts
Normal file
185
packages/backend/convex/empresas.ts
Normal file
@@ -0,0 +1,185 @@
|
||||
import { v } from "convex/values";
|
||||
import { mutation, query } from "./_generated/server";
|
||||
import { internal } from "./_generated/api";
|
||||
import { getCurrentUserFunction } from "./auth";
|
||||
|
||||
export const list = query({
|
||||
args: {},
|
||||
handler: async (ctx) => {
|
||||
await ctx.runQuery(internal.permissoesAcoes.assertPermissaoAcaoAtual, {
|
||||
recurso: "empresas",
|
||||
acao: "listar",
|
||||
});
|
||||
|
||||
const empresas = await ctx.db.query("empresas").collect();
|
||||
return empresas;
|
||||
},
|
||||
});
|
||||
|
||||
export const getById = query({
|
||||
args: { id: v.id("empresas") },
|
||||
handler: async (ctx, args) => {
|
||||
await ctx.runQuery(internal.permissoesAcoes.assertPermissaoAcaoAtual, {
|
||||
recurso: "empresas",
|
||||
acao: "ver",
|
||||
});
|
||||
|
||||
const empresa = await ctx.db.get(args.id);
|
||||
if (!empresa) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const contatos = await ctx.db
|
||||
.query("contatosEmpresa")
|
||||
.withIndex("by_empresa", (q) => q.eq("empresaId", args.id))
|
||||
.collect();
|
||||
|
||||
return { ...empresa, contatos };
|
||||
},
|
||||
});
|
||||
|
||||
const contatoInput = v.object({
|
||||
_id: v.optional(v.id("contatosEmpresa")),
|
||||
empresaId: v.optional(v.id("empresas")),
|
||||
nome: v.string(),
|
||||
funcao: v.string(),
|
||||
email: v.string(),
|
||||
telefone: v.string(),
|
||||
adicionadoPor: v.optional(v.id("usuarios")),
|
||||
descricao: v.optional(v.string()),
|
||||
_deleted: v.optional(v.boolean()),
|
||||
});
|
||||
|
||||
export const create = mutation({
|
||||
args: {
|
||||
nome: v.string(),
|
||||
cnpj: v.string(),
|
||||
telefone: v.string(),
|
||||
email: v.string(),
|
||||
descricao: v.optional(v.string()),
|
||||
contatos: v.optional(v.array(contatoInput)),
|
||||
},
|
||||
returns: v.id("empresas"),
|
||||
handler: async (ctx, args) => {
|
||||
const usuarioAtual = await getCurrentUserFunction(ctx);
|
||||
|
||||
if (!usuarioAtual) {
|
||||
throw new Error("Usuário não autenticado.");
|
||||
}
|
||||
|
||||
await ctx.runQuery(internal.permissoesAcoes.assertPermissaoAcaoAtual, {
|
||||
recurso: "empresas",
|
||||
acao: "criar",
|
||||
});
|
||||
|
||||
const cnpjExistente = await ctx.db
|
||||
.query("empresas")
|
||||
.withIndex("by_cnpj", (q) => q.eq("cnpj", args.cnpj))
|
||||
.unique();
|
||||
|
||||
if (cnpjExistente) {
|
||||
throw new Error("Já existe uma empresa cadastrada com este CNPJ.");
|
||||
}
|
||||
|
||||
|
||||
|
||||
const empresaId = await ctx.db.insert("empresas", {
|
||||
nome: args.nome,
|
||||
cnpj: args.cnpj,
|
||||
telefone: args.telefone,
|
||||
email: args.email,
|
||||
descricao: args.descricao,
|
||||
criadoPor: usuarioAtual._id,
|
||||
});
|
||||
|
||||
if (args.contatos && args.contatos.length > 0) {
|
||||
for (const contato of args.contatos) {
|
||||
await ctx.db.insert("contatosEmpresa", {
|
||||
empresaId,
|
||||
nome: contato.nome,
|
||||
funcao: contato.funcao,
|
||||
email: contato.email,
|
||||
telefone: contato.telefone,
|
||||
adicionadoPor: usuarioAtual._id,
|
||||
descricao: contato.descricao,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return empresaId;
|
||||
},
|
||||
});
|
||||
|
||||
export const update = mutation({
|
||||
args: {
|
||||
id: v.id("empresas"),
|
||||
nome: v.string(),
|
||||
cnpj: v.string(),
|
||||
telefone: v.string(),
|
||||
email: v.string(),
|
||||
descricao: v.optional(v.string()),
|
||||
contatos: v.optional(v.array(contatoInput)),
|
||||
},
|
||||
returns: v.null(),
|
||||
handler: async (ctx, args) => {
|
||||
await ctx.runQuery(internal.permissoesAcoes.assertPermissaoAcaoAtual, {
|
||||
recurso: "empresas",
|
||||
acao: "editar",
|
||||
});
|
||||
|
||||
const cnpjExistente = await ctx.db
|
||||
.query("empresas")
|
||||
.withIndex("by_cnpj", (q) => q.eq("cnpj", args.cnpj))
|
||||
.unique();
|
||||
|
||||
if (cnpjExistente && cnpjExistente._id !== args.id) {
|
||||
throw new Error("Já existe uma empresa cadastrada com este CNPJ.");
|
||||
}
|
||||
|
||||
await ctx.db.patch(args.id, {
|
||||
nome: args.nome,
|
||||
cnpj: args.cnpj,
|
||||
telefone: args.telefone,
|
||||
email: args.email,
|
||||
descricao: args.descricao,
|
||||
});
|
||||
|
||||
if (!args.contatos) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for (const contato of args.contatos) {
|
||||
if (contato._id && contato._deleted) {
|
||||
await ctx.db.delete(contato._id);
|
||||
} else if (contato._id) {
|
||||
await ctx.db.patch(contato._id, {
|
||||
nome: contato.nome,
|
||||
funcao: contato.funcao,
|
||||
email: contato.email,
|
||||
telefone: contato.telefone,
|
||||
descricao: contato.descricao,
|
||||
});
|
||||
} else if (!contato._deleted) {
|
||||
const usuarioAtual = await getCurrentUserFunction(ctx);
|
||||
|
||||
if (!usuarioAtual) {
|
||||
throw new Error("Usuário não autenticado.");
|
||||
}
|
||||
|
||||
await ctx.db.insert("contatosEmpresa", {
|
||||
empresaId: args.id,
|
||||
nome: contato.nome,
|
||||
funcao: contato.funcao,
|
||||
email: contato.email,
|
||||
telefone: contato.telefone,
|
||||
adicionadoPor: usuarioAtual._id,
|
||||
descricao: contato.descricao,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user