first commit
This commit is contained in:
+60
@@ -0,0 +1,60 @@
|
||||
"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
|
||||
Reference in New Issue
Block a user