Replace hard deletes with deletedAt timestamp flags on all three entities. Deleting a client or project only sets its own deletedAt; child records are excluded implicitly by filtering on parent deletedAt in every read query. Raw SQL statistics queries also filter out soft-deleted parents. FK ON DELETE CASCADE removed from Project→Client and TimeEntry→Project.
52 lines
1.2 KiB
TypeScript
52 lines
1.2 KiB
TypeScript
import { prisma } from "../prisma/client";
|
|
import { NotFoundError } from "../errors/AppError";
|
|
import type { CreateClientInput, UpdateClientInput } from "../types";
|
|
|
|
export class ClientService {
|
|
async findAll(userId: string) {
|
|
return prisma.client.findMany({
|
|
where: { userId, deletedAt: null },
|
|
orderBy: { name: "asc" },
|
|
});
|
|
}
|
|
|
|
async findById(id: string, userId: string) {
|
|
return prisma.client.findFirst({
|
|
where: { id, userId, deletedAt: null },
|
|
});
|
|
}
|
|
|
|
async create(userId: string, data: CreateClientInput) {
|
|
return prisma.client.create({
|
|
data: {
|
|
...data,
|
|
userId,
|
|
},
|
|
});
|
|
}
|
|
|
|
async update(id: string, userId: string, data: UpdateClientInput) {
|
|
const client = await this.findById(id, userId);
|
|
if (!client) {
|
|
throw new NotFoundError("Client not found");
|
|
}
|
|
|
|
return prisma.client.update({
|
|
where: { id },
|
|
data,
|
|
});
|
|
}
|
|
|
|
async delete(id: string, userId: string) {
|
|
const client = await this.findById(id, userId);
|
|
if (!client) {
|
|
throw new NotFoundError("Client not found");
|
|
}
|
|
|
|
await prisma.client.update({
|
|
where: { id },
|
|
data: { deletedAt: new Date() },
|
|
});
|
|
}
|
|
}
|