60 lines
1.6 KiB
JavaScript
60 lines
1.6 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.getTariffsByProvider = getTariffsByProvider;
|
|
exports.getTariffById = getTariffById;
|
|
exports.createTariff = createTariff;
|
|
exports.updateTariff = updateTariff;
|
|
exports.deleteTariff = deleteTariff;
|
|
const client_1 = require("@prisma/client");
|
|
const prisma = new client_1.PrismaClient();
|
|
async function getTariffsByProvider(providerId, includeInactive = false) {
|
|
const where = { providerId };
|
|
if (!includeInactive) {
|
|
where.isActive = true;
|
|
}
|
|
return prisma.tariff.findMany({
|
|
where,
|
|
orderBy: { name: 'asc' },
|
|
include: {
|
|
_count: {
|
|
select: { contracts: true },
|
|
},
|
|
},
|
|
});
|
|
}
|
|
async function getTariffById(id) {
|
|
return prisma.tariff.findUnique({
|
|
where: { id },
|
|
include: {
|
|
provider: true,
|
|
_count: {
|
|
select: { contracts: true },
|
|
},
|
|
},
|
|
});
|
|
}
|
|
async function createTariff(data) {
|
|
return prisma.tariff.create({
|
|
data: {
|
|
...data,
|
|
isActive: true,
|
|
},
|
|
});
|
|
}
|
|
async function updateTariff(id, data) {
|
|
return prisma.tariff.update({
|
|
where: { id },
|
|
data,
|
|
});
|
|
}
|
|
async function deleteTariff(id) {
|
|
// 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 } });
|
|
}
|
|
//# sourceMappingURL=tariff.service.js.map
|