170 lines
4.4 KiB
TypeScript
170 lines
4.4 KiB
TypeScript
import { prisma } from "../config/database";
|
|
import { Cliente, Prisma } from "@prisma/client";
|
|
|
|
export class ClienteRepository {
|
|
async findAll(): Promise<Cliente[]> {
|
|
try {
|
|
const clientes = await prisma.cliente.findMany({
|
|
include: {
|
|
filhos: true,
|
|
processos: {
|
|
select: {
|
|
id: true,
|
|
numero: true,
|
|
natureza: true,
|
|
status: true,
|
|
dataCriacao: true,
|
|
},
|
|
},
|
|
},
|
|
orderBy: {
|
|
nome: "asc",
|
|
},
|
|
});
|
|
|
|
return clientes;
|
|
} catch (error) {
|
|
console.error("Erro ao buscar todos os clientes:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async findById(id: string): Promise<Cliente | null> {
|
|
try {
|
|
const cliente = await prisma.cliente.findUnique({
|
|
where: { id },
|
|
include: {
|
|
filhos: true,
|
|
processos: true,
|
|
},
|
|
});
|
|
|
|
return cliente;
|
|
} catch (error) {
|
|
console.error("Erro ao buscar cliente por ID:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async findByCpfCnpj(cpfCnpj: string): Promise<Cliente | null> {
|
|
try {
|
|
const cliente = await prisma.cliente.findUnique({
|
|
where: { cpfCnpj },
|
|
});
|
|
|
|
return cliente;
|
|
} catch (error) {
|
|
console.error("Erro ao buscar cliente por CPF/CNPJ:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async create(data: Prisma.ClienteCreateInput): Promise<Cliente> {
|
|
try {
|
|
// Verificar se já existe cliente com esse CPF/CNPJ
|
|
const existingCliente = await this.findByCpfCnpj(data.cpfCnpj);
|
|
if (existingCliente) {
|
|
throw new Error(`Cliente com CPF/CNPJ '${data.cpfCnpj}' já existe`);
|
|
}
|
|
|
|
// Criar o cliente
|
|
const cliente = await prisma.cliente.create({
|
|
data,
|
|
include: {
|
|
filhos: true,
|
|
},
|
|
});
|
|
|
|
return cliente;
|
|
} catch (error) {
|
|
console.error("Erro ao criar cliente:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async update(id: string, data: Prisma.ClienteUpdateInput): Promise<Cliente> {
|
|
try {
|
|
// Verificar CPF/CNPJ duplicado
|
|
if (typeof data.cpfCnpj === "string") {
|
|
const existingCliente = await prisma.cliente.findFirst({
|
|
where: {
|
|
cpfCnpj: data.cpfCnpj as string,
|
|
NOT: { id },
|
|
},
|
|
});
|
|
|
|
if (existingCliente) {
|
|
throw new Error(`Cliente com CPF/CNPJ '${data.cpfCnpj}' já existe`);
|
|
}
|
|
}
|
|
|
|
// Formatar dados
|
|
const formattedData = {
|
|
...data,
|
|
// Garantir que a data de nascimento seja um Date válido
|
|
dataNascimento: data.dataNascimento
|
|
? new Date(data.dataNascimento as string)
|
|
: undefined,
|
|
// Campos opcionais como undefined em vez de null/empty
|
|
orgaoEmissor: data.orgaoEmissor || undefined,
|
|
estadoRg: data.estadoRg || undefined,
|
|
rg: data.rg || undefined,
|
|
renda: data.renda ? Number(data.renda) : undefined,
|
|
// Garantir que booleanos sejam boolean
|
|
usaDNI: typeof data.usaDNI === "boolean" ? data.usaDNI : undefined,
|
|
possuiFilhos:
|
|
typeof data.possuiFilhos === "boolean"
|
|
? data.possuiFilhos
|
|
: undefined,
|
|
declaraIR:
|
|
typeof data.declaraIR === "boolean" ? data.declaraIR : undefined,
|
|
contribuiPrevidencia:
|
|
typeof data.contribuiPrevidencia === "boolean"
|
|
? data.contribuiPrevidencia
|
|
: undefined,
|
|
};
|
|
|
|
// Atualizar cliente
|
|
return await prisma.cliente.update({
|
|
where: { id },
|
|
data: formattedData,
|
|
include: {
|
|
filhos: true,
|
|
},
|
|
});
|
|
} catch (error) {
|
|
console.error("Erro ao atualizar cliente:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async delete(id: string): Promise<void> {
|
|
try {
|
|
await prisma.cliente.delete({
|
|
where: { id },
|
|
});
|
|
} catch (error) {
|
|
console.error("Erro ao excluir cliente:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async search(term: string): Promise<Cliente[]> {
|
|
try {
|
|
const clientes = await prisma.cliente.findMany({
|
|
where: {
|
|
OR: [
|
|
{ nome: { contains: term, mode: "insensitive" } },
|
|
{ cpfCnpj: { contains: term } },
|
|
],
|
|
},
|
|
});
|
|
|
|
return clientes;
|
|
} catch (error) {
|
|
console.error("Erro ao pesquisar clientes:", error);
|
|
throw error;
|
|
}
|
|
}
|
|
}
|