opencrm/backend/dist/services/contractCategory.service.js

69 lines
2.2 KiB
JavaScript

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.getAllContractCategories = getAllContractCategories;
exports.getContractCategoryById = getContractCategoryById;
exports.getContractCategoryByCode = getContractCategoryByCode;
exports.createContractCategory = createContractCategory;
exports.updateContractCategory = updateContractCategory;
exports.deleteContractCategory = deleteContractCategory;
const client_1 = require("@prisma/client");
const prisma = new client_1.PrismaClient();
async function getAllContractCategories(includeInactive = false) {
return prisma.contractCategory.findMany({
where: includeInactive ? {} : { isActive: true },
orderBy: [{ sortOrder: 'asc' }, { name: 'asc' }],
include: {
_count: {
select: { contracts: true },
},
},
});
}
async function getContractCategoryById(id) {
return prisma.contractCategory.findUnique({
where: { id },
include: {
_count: {
select: { contracts: true },
},
},
});
}
async function getContractCategoryByCode(code) {
return prisma.contractCategory.findUnique({
where: { code },
});
}
async function createContractCategory(data) {
return prisma.contractCategory.create({
data,
include: {
_count: {
select: { contracts: true },
},
},
});
}
async function updateContractCategory(id, data) {
return prisma.contractCategory.update({
where: { id },
data,
include: {
_count: {
select: { contracts: true },
},
},
});
}
async function deleteContractCategory(id) {
// Check if category has contracts
const category = await prisma.contractCategory.findUnique({
where: { id },
include: { _count: { select: { contracts: true } } },
});
if (category && category._count.contracts > 0) {
throw new Error(`Kategorie kann nicht gelöscht werden, da ${category._count.contracts} Verträge zugeordnet sind.`);
}
return prisma.contractCategory.delete({ where: { id } });
}
//# sourceMappingURL=contractCategory.service.js.map