first commit
This commit is contained in:
commit
c9fb67e751
423 files changed
+53287
No files matched your search
@@ -0,0 +1,236 @@
|
||||
import { prisma } from "../config/database";
|
||||
import { ProcessoAdministrativo, MovimentacaoAdministrativa, CustaAdministrativa, Prisma } from "@prisma/client";
|
||||
|
||||
export class ProcessoAdministrativoRepository {
|
||||
async findAll(): Promise<ProcessoAdministrativo[]> {
|
||||
try {
|
||||
return await prisma.processoAdministrativo.findMany({
|
||||
include: {
|
||||
cliente: true,
|
||||
movimentacoes: {
|
||||
orderBy: {
|
||||
data: 'desc'
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erro ao buscar processos administrativos:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findById(id: string): Promise<ProcessoAdministrativo | null> {
|
||||
try {
|
||||
return await prisma.processoAdministrativo.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
cliente: true,
|
||||
movimentacoes: {
|
||||
orderBy: {
|
||||
data: 'desc'
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erro ao buscar processo administrativo:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async create(data: Partial<ProcessoAdministrativo>): Promise<ProcessoAdministrativo> {
|
||||
try {
|
||||
// Validar campos obrigatórios
|
||||
if (!data.numero) throw new Error("Número é obrigatório");
|
||||
if (!data.descricao) throw new Error("Descrição é obrigatória");
|
||||
if (!data.orgao) throw new Error("Órgão é obrigatório");
|
||||
if (!data.clienteId) throw new Error("Cliente é obrigatório");
|
||||
if (!data.tipoParteCliente) throw new Error("Tipo de parte é obrigatório");
|
||||
|
||||
return await prisma.processoAdministrativo.create({
|
||||
data: {
|
||||
...data,
|
||||
valorPretensao: data.valorPretensao ? Number(data.valorPretensao) : null,
|
||||
},
|
||||
include: {
|
||||
cliente: true,
|
||||
movimentacoes: true
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erro ao criar processo administrativo:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async addMovimentacao(
|
||||
processoId: string,
|
||||
data: Partial<MovimentacaoAdministrativa>
|
||||
): Promise<MovimentacaoAdministrativa> {
|
||||
try {
|
||||
// Validar campos obrigatórios
|
||||
if (!data.descricao) throw new Error("Descrição é obrigatória");
|
||||
if (!data.tipoMovimentacao) throw new Error("Tipo de movimentação é obrigatório");
|
||||
if (!data.data) throw new Error("Data é obrigatória");
|
||||
|
||||
return await prisma.movimentacaoAdministrativa.create({
|
||||
data: {
|
||||
...data,
|
||||
processoId,
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erro ao adicionar movimentação:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async updateMovimentacao(
|
||||
id: string,
|
||||
data: Partial<MovimentacaoAdministrativa>
|
||||
): Promise<MovimentacaoAdministrativa> {
|
||||
try {
|
||||
// Validar campos obrigatórios
|
||||
if (!data.descricao) throw new Error("Descrição é obrigatória");
|
||||
if (!data.tipoMovimentacao) throw new Error("Tipo de movimentação é obrigatório");
|
||||
if (!data.data) throw new Error("Data é obrigatória");
|
||||
|
||||
const { processoId, dataCriacao, ...updateData } = data;
|
||||
|
||||
return await prisma.movimentacaoAdministrativa.update({
|
||||
where: { id },
|
||||
data: {
|
||||
...updateData,
|
||||
data: new Date(updateData.data),
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erro ao atualizar movimentação:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async deleteMovimentacao(id: string): Promise<void> {
|
||||
try {
|
||||
await prisma.movimentacaoAdministrativa.delete({
|
||||
where: { id }
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erro ao excluir movimentação:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async update(id: string, data: Partial<ProcessoAdministrativo>): Promise<ProcessoAdministrativo> {
|
||||
try {
|
||||
// Extrair campos de relacionamento
|
||||
const { clienteId, movimentacoes, ...updateData } = data;
|
||||
|
||||
// Remover campos que não devem ser atualizados diretamente
|
||||
const { id: _id, dataCriacao: _dc, dataAtualizacao: _da, ...cleanData } = updateData;
|
||||
|
||||
return await prisma.processoAdministrativo.update({
|
||||
where: { id },
|
||||
data: {
|
||||
...cleanData,
|
||||
valorPretensao: cleanData.valorPretensao ? Number(cleanData.valorPretensao) : null,
|
||||
// Atualizar relação com cliente se houver clienteId
|
||||
...(clienteId && {
|
||||
cliente: {
|
||||
connect: { id: clienteId }
|
||||
}
|
||||
}),
|
||||
// Remover atualização de movimentações - elas devem ser gerenciadas separadamente
|
||||
movimentacoes: undefined
|
||||
},
|
||||
include: {
|
||||
cliente: true,
|
||||
movimentacoes: {
|
||||
orderBy: {
|
||||
data: 'desc'
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erro ao atualizar processo administrativo:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async delete(id: string): Promise<void> {
|
||||
try {
|
||||
await prisma.processoAdministrativo.delete({
|
||||
where: { id }
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erro ao excluir processo administrativo:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async findCustasByProcesso(processoId: string) {
|
||||
try {
|
||||
return await prisma.custaAdministrativa.findMany({
|
||||
where: { processoId },
|
||||
orderBy: [
|
||||
{ dataCriacao: 'desc' }
|
||||
]
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erro ao buscar custas do processo:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async addCusta(processoId: string, data: Partial<CustaAdministrativa>) {
|
||||
try {
|
||||
// Validar campos obrigatórios
|
||||
if (!data.tipo) throw new Error("Tipo é obrigatório");
|
||||
if (!data.descricao) throw new Error("Descrição é obrigatória");
|
||||
if (!data.valor) throw new Error("Valor é obrigatório");
|
||||
|
||||
return await prisma.custaAdministrativa.create({
|
||||
data: {
|
||||
...data,
|
||||
processoId,
|
||||
valor: new Prisma.Decimal(data.valor.toString()),
|
||||
vencimento: data.vencimento ? new Date(data.vencimento) : null,
|
||||
dataPagamento: data.dataPagamento ? new Date(data.dataPagamento) : null
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erro ao adicionar custa:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async updateCusta(id: string, data: Partial<CustaAdministrativa>) {
|
||||
try {
|
||||
return await prisma.custaAdministrativa.update({
|
||||
where: { id },
|
||||
data: {
|
||||
...data,
|
||||
valor: data.valor ? new Prisma.Decimal(data.valor.toString()) : undefined,
|
||||
vencimento: data.vencimento ? new Date(data.vencimento) : null,
|
||||
dataPagamento: data.dataPagamento ? new Date(data.dataPagamento) : null
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erro ao atualizar custa:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async deleteCusta(id: string) {
|
||||
try {
|
||||
await prisma.custaAdministrativa.delete({
|
||||
where: { id }
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Erro ao excluir custa:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user