139 lines
4.4 KiB
TypeScript
139 lines
4.4 KiB
TypeScript
import { v } from "convex/values";
|
|
import { mutation, query } from "./_generated/server";
|
|
|
|
// Mutation para fazer upload de arquivo e obter o storage ID
|
|
export const generateUploadUrl = mutation({
|
|
args: {},
|
|
returns: v.string(),
|
|
handler: async (ctx) => {
|
|
return await ctx.storage.generateUploadUrl();
|
|
},
|
|
});
|
|
|
|
// Mutation para atualizar um campo de documento do funcionário
|
|
export const updateDocumento = mutation({
|
|
args: {
|
|
funcionarioId: v.id("funcionarios"),
|
|
campo: v.string(),
|
|
storageId: v.union(v.id("_storage"), v.null()),
|
|
},
|
|
returns: v.null(),
|
|
handler: async (ctx, args) => {
|
|
const funcionario = await ctx.db.get(args.funcionarioId);
|
|
if (!funcionario) {
|
|
throw new Error("Funcionário não encontrado");
|
|
}
|
|
|
|
// Atualizar o campo específico do documento
|
|
await ctx.db.patch(args.funcionarioId, {
|
|
[args.campo]: args.storageId,
|
|
} as any);
|
|
|
|
return null;
|
|
},
|
|
});
|
|
|
|
// Query para obter URLs de todos os documentos de um funcionário
|
|
export const getDocumentosUrls = query({
|
|
args: { funcionarioId: v.id("funcionarios") },
|
|
returns: v.object({
|
|
certidaoAntecedentesPF: v.union(v.string(), v.null()),
|
|
certidaoAntecedentesJFPE: v.union(v.string(), v.null()),
|
|
certidaoAntecedentesSDS: v.union(v.string(), v.null()),
|
|
certidaoAntecedentesTJPE: v.union(v.string(), v.null()),
|
|
certidaoImprobidade: v.union(v.string(), v.null()),
|
|
rgFrente: v.union(v.string(), v.null()),
|
|
rgVerso: v.union(v.string(), v.null()),
|
|
cpfFrente: v.union(v.string(), v.null()),
|
|
cpfVerso: v.union(v.string(), v.null()),
|
|
situacaoCadastralCPF: v.union(v.string(), v.null()),
|
|
tituloEleitorFrente: v.union(v.string(), v.null()),
|
|
tituloEleitorVerso: v.union(v.string(), v.null()),
|
|
comprovanteVotacao: v.union(v.string(), v.null()),
|
|
carteiraProfissionalFrente: v.union(v.string(), v.null()),
|
|
carteiraProfissionalVerso: v.union(v.string(), v.null()),
|
|
comprovantePIS: v.union(v.string(), v.null()),
|
|
certidaoRegistroCivil: v.union(v.string(), v.null()),
|
|
certidaoNascimentoDependentes: v.union(v.string(), v.null()),
|
|
cpfDependentes: v.union(v.string(), v.null()),
|
|
reservistaDoc: v.union(v.string(), v.null()),
|
|
comprovanteEscolaridade: v.union(v.string(), v.null()),
|
|
comprovanteResidencia: v.union(v.string(), v.null()),
|
|
comprovanteContaBradesco: v.union(v.string(), v.null()),
|
|
declaracaoAcumulacaoCargo: v.union(v.string(), v.null()),
|
|
declaracaoDependentesIR: v.union(v.string(), v.null()),
|
|
declaracaoIdoneidade: v.union(v.string(), v.null()),
|
|
termoNepotismo: v.union(v.string(), v.null()),
|
|
termoOpcaoRemuneracao: v.union(v.string(), v.null()),
|
|
}),
|
|
handler: async (ctx, args) => {
|
|
const funcionario = await ctx.db.get(args.funcionarioId);
|
|
if (!funcionario) {
|
|
throw new Error("Funcionário não encontrado");
|
|
}
|
|
|
|
// Gerar URLs para todos os documentos
|
|
const urls: Record<string, string | null> = {};
|
|
const campos = [
|
|
"certidaoAntecedentesPF",
|
|
"certidaoAntecedentesJFPE",
|
|
"certidaoAntecedentesSDS",
|
|
"certidaoAntecedentesTJPE",
|
|
"certidaoImprobidade",
|
|
"rgFrente",
|
|
"rgVerso",
|
|
"cpfFrente",
|
|
"cpfVerso",
|
|
"situacaoCadastralCPF",
|
|
"tituloEleitorFrente",
|
|
"tituloEleitorVerso",
|
|
"comprovanteVotacao",
|
|
"carteiraProfissionalFrente",
|
|
"carteiraProfissionalVerso",
|
|
"comprovantePIS",
|
|
"certidaoRegistroCivil",
|
|
"certidaoNascimentoDependentes",
|
|
"cpfDependentes",
|
|
"reservistaDoc",
|
|
"comprovanteEscolaridade",
|
|
"comprovanteResidencia",
|
|
"comprovanteContaBradesco",
|
|
"declaracaoAcumulacaoCargo",
|
|
"declaracaoDependentesIR",
|
|
"declaracaoIdoneidade",
|
|
"termoNepotismo",
|
|
"termoOpcaoRemuneracao",
|
|
];
|
|
|
|
for (const campo of campos) {
|
|
const storageId = (funcionario as any)[campo];
|
|
if (storageId) {
|
|
urls[campo] = await ctx.storage.getUrl(storageId);
|
|
} else {
|
|
urls[campo] = null;
|
|
}
|
|
}
|
|
|
|
return urls as any;
|
|
},
|
|
});
|
|
|
|
// Query para obter metadados de um documento
|
|
export const getDocumentoMetadata = query({
|
|
args: { storageId: v.id("_storage") },
|
|
returns: v.union(
|
|
v.object({
|
|
_id: v.id("_storage"),
|
|
_creationTime: v.number(),
|
|
contentType: v.optional(v.string()),
|
|
sha256: v.string(),
|
|
size: v.number(),
|
|
}),
|
|
v.null()
|
|
),
|
|
handler: async (ctx, args) => {
|
|
return await ctx.db.system.get(args.storageId);
|
|
},
|
|
});
|
|
|