139 lines
3.9 KiB
TypeScript
139 lines
3.9 KiB
TypeScript
import { prisma } from "../config/database";
|
|
import {
|
|
CustaProcessual,
|
|
TipoCustaProcessual,
|
|
StatusPagamento,
|
|
Prisma,
|
|
} from "@prisma/client";
|
|
|
|
/**
|
|
* Repositório para gerenciar custas processuais
|
|
*/
|
|
export class CustaProcessualRepository {
|
|
/**
|
|
* Cria uma nova custa processual
|
|
*/
|
|
async create(data: any): Promise<CustaProcessual> {
|
|
try {
|
|
// Validar dados obrigatórios
|
|
if (!data.processoId) throw new Error("ID do processo é obrigatório");
|
|
if (!data.tipo) throw new Error("Tipo da custa é obrigatório");
|
|
if (!data.descricao) throw new Error("Descrição da custa é obrigatória");
|
|
if (data.valor === undefined || data.valor === null)
|
|
throw new Error("Valor da custa é obrigatório");
|
|
|
|
// Criar custa
|
|
const novaCusta = await prisma.custaProcessual.create({
|
|
data: {
|
|
processoId: data.processoId,
|
|
tipo: data.tipo as TipoCustaProcessual,
|
|
descricao: data.descricao,
|
|
valor: Number(data.valor),
|
|
percentual: data.percentual ? Number(data.percentual) : null,
|
|
status: (data.status || "PENDENTE") as StatusPagamento,
|
|
vencimento: data.vencimento ? new Date(data.vencimento) : null,
|
|
dataPagamento: data.dataPagamento
|
|
? new Date(data.dataPagamento)
|
|
: null,
|
|
observacoes: data.observacoes || null,
|
|
numeroParcelas: Number(data.numeroParcelas || 1),
|
|
parcelaAtual: Number(data.parcelaAtual || 1),
|
|
comprovante: data.comprovante || null,
|
|
},
|
|
});
|
|
|
|
return novaCusta;
|
|
} catch (error) {
|
|
console.error("Erro ao criar custa processual:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Atualiza uma custa processual existente
|
|
*/
|
|
async update(id: string, data: any): Promise<CustaProcessual> {
|
|
try {
|
|
// Preparar dados para atualização
|
|
const updateData: Prisma.CustaProcessualUpdateInput = {
|
|
tipo: data.tipo,
|
|
descricao: data.descricao,
|
|
valor: data.valor !== undefined ? Number(data.valor) : undefined,
|
|
percentual:
|
|
data.percentual !== undefined ? Number(data.percentual) : undefined,
|
|
status: data.status,
|
|
observacoes: data.observacoes,
|
|
numeroParcelas:
|
|
data.numeroParcelas !== undefined
|
|
? Number(data.numeroParcelas)
|
|
: undefined,
|
|
parcelaAtual:
|
|
data.parcelaAtual !== undefined
|
|
? Number(data.parcelaAtual)
|
|
: undefined,
|
|
comprovante: data.comprovante,
|
|
};
|
|
|
|
if (data.vencimento) {
|
|
updateData.vencimento = new Date(data.vencimento);
|
|
}
|
|
|
|
if (data.dataPagamento) {
|
|
updateData.dataPagamento = new Date(data.dataPagamento);
|
|
}
|
|
|
|
// Atualizar a custa
|
|
return await prisma.custaProcessual.update({
|
|
where: { id },
|
|
data: updateData,
|
|
});
|
|
} catch (error) {
|
|
console.error("Erro ao atualizar custa processual:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove uma custa processual
|
|
*/
|
|
async delete(id: string): Promise<void> {
|
|
try {
|
|
await prisma.custaProcessual.delete({
|
|
where: { id },
|
|
});
|
|
} catch (error) {
|
|
console.error("Erro ao excluir custa processual:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Busca todas as custas de um processo
|
|
*/
|
|
async findByProcesso(processoId: string): Promise<CustaProcessual[]> {
|
|
try {
|
|
return await prisma.custaProcessual.findMany({
|
|
where: { processoId },
|
|
orderBy: { dataCriacao: "desc" },
|
|
});
|
|
} catch (error) {
|
|
console.error("Erro ao buscar custas do processo:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Busca uma custa pelo ID
|
|
*/
|
|
async findById(id: string): Promise<CustaProcessual | null> {
|
|
try {
|
|
return await prisma.custaProcessual.findUnique({
|
|
where: { id },
|
|
});
|
|
} catch (error) {
|
|
console.error("Erro ao buscar custa processual:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
}
|