first commit
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
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 } });
|
||||
}
|
||||
Reference in New Issue
Block a user