opencrm/backend/src/services/platform.service.ts

62 lines
1.2 KiB
TypeScript

import prisma from '../lib/prisma.js';
export async function getAllPlatforms(includeInactive = false) {
const where = includeInactive ? {} : { isActive: true };
return prisma.salesPlatform.findMany({
where,
orderBy: { name: 'asc' },
});
}
export async function getPlatformById(id: number) {
return prisma.salesPlatform.findUnique({
where: { id },
include: {
_count: {
select: { contracts: true },
},
},
});
}
export async function createPlatform(data: {
name: string;
contactInfo?: string;
}) {
return prisma.salesPlatform.create({
data: {
...data,
isActive: true,
},
});
}
export async function updatePlatform(
id: number,
data: {
name?: string;
contactInfo?: string;
isActive?: boolean;
}
) {
return prisma.salesPlatform.update({
where: { id },
data,
});
}
export async function deletePlatform(id: number) {
// Check if platform is used by any contracts
const count = await prisma.contract.count({
where: { salesPlatformId: id },
});
if (count > 0) {
throw new Error(
`Plattform kann nicht gelöscht werden, da sie von ${count} Verträgen verwendet wird`
);
}
return prisma.salesPlatform.delete({ where: { id } });
}