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 } }); }