70 lines
1.4 KiB
TypeScript
70 lines
1.4 KiB
TypeScript
import prisma from '../lib/prisma.js';
|
|
|
|
export async function getTariffsByProvider(providerId: number, includeInactive = false) {
|
|
const where: { providerId: number; isActive?: boolean } = { providerId };
|
|
if (!includeInactive) {
|
|
where.isActive = true;
|
|
}
|
|
return prisma.tariff.findMany({
|
|
where,
|
|
orderBy: { name: 'asc' },
|
|
include: {
|
|
_count: {
|
|
select: { contracts: true },
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function getTariffById(id: number) {
|
|
return prisma.tariff.findUnique({
|
|
where: { id },
|
|
include: {
|
|
provider: true,
|
|
_count: {
|
|
select: { contracts: true },
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function createTariff(data: {
|
|
providerId: number;
|
|
name: string;
|
|
}) {
|
|
return prisma.tariff.create({
|
|
data: {
|
|
...data,
|
|
isActive: true,
|
|
},
|
|
});
|
|
}
|
|
|
|
export async function updateTariff(
|
|
id: number,
|
|
data: {
|
|
name?: string;
|
|
isActive?: boolean;
|
|
}
|
|
) {
|
|
return prisma.tariff.update({
|
|
where: { id },
|
|
data,
|
|
});
|
|
}
|
|
|
|
export async function deleteTariff(id: number) {
|
|
// Check if tariff is used by any contracts
|
|
const count = await prisma.contract.count({
|
|
where: { tariffId: id },
|
|
});
|
|
|
|
if (count > 0) {
|
|
throw new Error(
|
|
`Tarif kann nicht gelöscht werden, da er von ${count} Verträgen verwendet wird`
|
|
);
|
|
}
|
|
|
|
return prisma.tariff.delete({ where: { id } });
|
|
}
|