189 lines
4.9 KiB
TypeScript
189 lines
4.9 KiB
TypeScript
import { v } from "convex/values";
|
|
import { query, mutation } from "./_generated/server";
|
|
import { simboloTipo } from "./schema";
|
|
|
|
export const getAll = query({
|
|
args: {},
|
|
returns: v.array(
|
|
v.object({
|
|
_id: v.id("funcionarios"),
|
|
_creationTime: v.number(),
|
|
nome: v.string(),
|
|
nascimento: v.string(),
|
|
rg: v.string(),
|
|
cpf: v.string(),
|
|
endereco: v.string(),
|
|
cep: v.string(),
|
|
cidade: v.string(),
|
|
uf: v.string(),
|
|
telefone: v.string(),
|
|
email: v.string(),
|
|
matricula: v.string(),
|
|
admissaoData: v.optional(v.string()),
|
|
desligamentoData: v.optional(v.string()),
|
|
simboloId: v.id("simbolos"),
|
|
simboloTipo: simboloTipo,
|
|
})
|
|
),
|
|
handler: async (ctx) => {
|
|
return await ctx.db.query("funcionarios").collect();
|
|
},
|
|
});
|
|
|
|
export const getById = query({
|
|
args: { id: v.id("funcionarios") },
|
|
returns: v.union(
|
|
v.object({
|
|
_id: v.id("funcionarios"),
|
|
_creationTime: v.number(),
|
|
nome: v.string(),
|
|
nascimento: v.string(),
|
|
rg: v.string(),
|
|
cpf: v.string(),
|
|
endereco: v.string(),
|
|
cep: v.string(),
|
|
cidade: v.string(),
|
|
uf: v.string(),
|
|
telefone: v.string(),
|
|
email: v.string(),
|
|
matricula: v.string(),
|
|
admissaoData: v.optional(v.string()),
|
|
desligamentoData: v.optional(v.string()),
|
|
simboloId: v.id("simbolos"),
|
|
simboloTipo: simboloTipo,
|
|
}),
|
|
v.null()
|
|
),
|
|
handler: async (ctx, args) => {
|
|
return await ctx.db.get(args.id);
|
|
},
|
|
});
|
|
|
|
export const create = mutation({
|
|
args: {
|
|
nome: v.string(),
|
|
matricula: v.string(),
|
|
simboloId: v.id("simbolos"),
|
|
nascimento: v.string(),
|
|
rg: v.string(),
|
|
cpf: v.string(),
|
|
endereco: v.string(),
|
|
cep: v.string(),
|
|
cidade: v.string(),
|
|
uf: v.string(),
|
|
telefone: v.string(),
|
|
email: v.string(),
|
|
admissaoData: v.optional(v.string()),
|
|
desligamentoData: v.optional(v.string()),
|
|
simboloTipo: simboloTipo,
|
|
},
|
|
returns: v.id("funcionarios"),
|
|
handler: async (ctx, args) => {
|
|
// Unicidade: CPF
|
|
const cpfExists = await ctx.db
|
|
.query("funcionarios")
|
|
.withIndex("by_cpf", (q) => q.eq("cpf", args.cpf))
|
|
.unique();
|
|
if (cpfExists) {
|
|
throw new Error("CPF já cadastrado");
|
|
}
|
|
|
|
// Unicidade: Matrícula
|
|
const matriculaExists = await ctx.db
|
|
.query("funcionarios")
|
|
.withIndex("by_matricula", (q) => q.eq("matricula", args.matricula))
|
|
.unique();
|
|
if (matriculaExists) {
|
|
throw new Error("Matrícula já cadastrada");
|
|
}
|
|
|
|
const novoFuncionarioId = await ctx.db.insert("funcionarios", {
|
|
nome: args.nome,
|
|
nascimento: args.nascimento,
|
|
rg: args.rg,
|
|
cpf: args.cpf,
|
|
endereco: args.endereco,
|
|
cep: args.cep,
|
|
cidade: args.cidade,
|
|
uf: args.uf,
|
|
telefone: args.telefone,
|
|
email: args.email,
|
|
matricula: args.matricula,
|
|
admissaoData: args.admissaoData,
|
|
desligamentoData: args.desligamentoData,
|
|
simboloId: args.simboloId,
|
|
simboloTipo: args.simboloTipo,
|
|
});
|
|
return novoFuncionarioId;
|
|
},
|
|
});
|
|
|
|
export const update = mutation({
|
|
args: {
|
|
id: v.id("funcionarios"),
|
|
nome: v.string(),
|
|
matricula: v.string(),
|
|
simboloId: v.id("simbolos"),
|
|
nascimento: v.string(),
|
|
rg: v.string(),
|
|
cpf: v.string(),
|
|
endereco: v.string(),
|
|
cep: v.string(),
|
|
cidade: v.string(),
|
|
uf: v.string(),
|
|
telefone: v.string(),
|
|
email: v.string(),
|
|
admissaoData: v.optional(v.string()),
|
|
desligamentoData: v.optional(v.string()),
|
|
simboloTipo: simboloTipo,
|
|
},
|
|
returns: v.null(),
|
|
handler: async (ctx, args) => {
|
|
// Unicidade: CPF (excluindo o próprio registro)
|
|
const cpfExists = await ctx.db
|
|
.query("funcionarios")
|
|
.withIndex("by_cpf", (q) => q.eq("cpf", args.cpf))
|
|
.unique();
|
|
if (cpfExists && cpfExists._id !== args.id) {
|
|
throw new Error("CPF já cadastrado");
|
|
}
|
|
|
|
// Unicidade: Matrícula (excluindo o próprio registro)
|
|
const matriculaExists = await ctx.db
|
|
.query("funcionarios")
|
|
.withIndex("by_matricula", (q) => q.eq("matricula", args.matricula))
|
|
.unique();
|
|
if (matriculaExists && matriculaExists._id !== args.id) {
|
|
throw new Error("Matrícula já cadastrada");
|
|
}
|
|
|
|
await ctx.db.patch(args.id, {
|
|
nome: args.nome,
|
|
nascimento: args.nascimento,
|
|
rg: args.rg,
|
|
cpf: args.cpf,
|
|
endereco: args.endereco,
|
|
cep: args.cep,
|
|
cidade: args.cidade,
|
|
uf: args.uf,
|
|
telefone: args.telefone,
|
|
email: args.email,
|
|
matricula: args.matricula,
|
|
admissaoData: args.admissaoData,
|
|
desligamentoData: args.desligamentoData,
|
|
simboloId: args.simboloId,
|
|
simboloTipo: args.simboloTipo,
|
|
});
|
|
return null;
|
|
},
|
|
});
|
|
|
|
export const remove = mutation({
|
|
args: { id: v.id("funcionarios") },
|
|
returns: v.null(),
|
|
handler: async (ctx, args) => {
|
|
await ctx.db.delete(args.id);
|
|
return null;
|
|
},
|
|
});
|