Files
opencrm/backend/src/services/cancellation-period.service.ts
T
2026-03-21 18:23:54 +01:00

62 lines
1.3 KiB
TypeScript

import prisma from '../lib/prisma.js';
export async function getAllCancellationPeriods(includeInactive = false) {
const where = includeInactive ? {} : { isActive: true };
return prisma.cancellationPeriod.findMany({
where,
orderBy: { code: 'asc' },
});
}
export async function getCancellationPeriodById(id: number) {
return prisma.cancellationPeriod.findUnique({
where: { id },
include: {
_count: {
select: { contracts: true },
},
},
});
}
export async function createCancellationPeriod(data: {
code: string;
description: string;
}) {
return prisma.cancellationPeriod.create({
data: {
...data,
isActive: true,
},
});
}
export async function updateCancellationPeriod(
id: number,
data: {
code?: string;
description?: string;
isActive?: boolean;
}
) {
return prisma.cancellationPeriod.update({
where: { id },
data,
});
}
export async function deleteCancellationPeriod(id: number) {
// Check if cancellation period is used by any contracts
const count = await prisma.contract.count({
where: { cancellationPeriodId: id },
});
if (count > 0) {
throw new Error(
`Kündigungsfrist kann nicht gelöscht werden, da sie von ${count} Verträgen verwendet wird`
);
}
return prisma.cancellationPeriod.delete({ where: { id } });
}