first commit
This commit is contained in:
@@ -0,0 +1,183 @@
|
||||
import { PrismaClient } from '@prisma/client';
|
||||
import bcrypt from 'bcryptjs';
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
|
||||
async function main() {
|
||||
console.log('Seeding database...');
|
||||
|
||||
// Create permissions
|
||||
const resources = ['customers', 'contracts', 'users', 'platforms', 'providers', 'developer'];
|
||||
const actions = ['create', 'read', 'update', 'delete', 'access'];
|
||||
|
||||
const permissions: { resource: string; action: string }[] = [];
|
||||
for (const resource of resources) {
|
||||
for (const action of actions) {
|
||||
// developer nur mit 'access' action
|
||||
if (resource === 'developer' && action !== 'access') continue;
|
||||
// andere resources ohne 'access' action
|
||||
if (resource !== 'developer' && action === 'access') continue;
|
||||
permissions.push({ resource, action });
|
||||
}
|
||||
}
|
||||
|
||||
for (const perm of permissions) {
|
||||
await prisma.permission.upsert({
|
||||
where: { resource_action: perm },
|
||||
update: {},
|
||||
create: perm,
|
||||
});
|
||||
}
|
||||
|
||||
console.log('Permissions created');
|
||||
|
||||
// Get all permissions
|
||||
const allPermissions = await prisma.permission.findMany();
|
||||
const customerReadPerm = allPermissions.find(
|
||||
(p) => p.resource === 'customers' && p.action === 'read'
|
||||
);
|
||||
const contractReadPerm = allPermissions.find(
|
||||
(p) => p.resource === 'contracts' && p.action === 'read'
|
||||
);
|
||||
const platformReadPerm = allPermissions.find(
|
||||
(p) => p.resource === 'platforms' && p.action === 'read'
|
||||
);
|
||||
const providerReadPerm = allPermissions.find(
|
||||
(p) => p.resource === 'providers' && p.action === 'read'
|
||||
);
|
||||
|
||||
// Create roles
|
||||
// Admin - all permissions EXCEPT developer:access (that's controlled separately)
|
||||
const adminPermissions = allPermissions.filter(
|
||||
(p) => !(p.resource === 'developer' && p.action === 'access')
|
||||
);
|
||||
const adminRole = await prisma.role.upsert({
|
||||
where: { name: 'Admin' },
|
||||
update: {},
|
||||
create: {
|
||||
name: 'Admin',
|
||||
description: 'Voller Zugriff auf alle Funktionen',
|
||||
permissions: {
|
||||
create: adminPermissions.map((p) => ({ permissionId: p.id })),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Employee - full access to customers, contracts, read platforms and providers
|
||||
const employeePermIds = allPermissions
|
||||
.filter(
|
||||
(p) =>
|
||||
p.resource === 'customers' ||
|
||||
p.resource === 'contracts' ||
|
||||
(p.resource === 'platforms' && p.action === 'read') ||
|
||||
(p.resource === 'providers' && p.action === 'read')
|
||||
)
|
||||
.map((p) => p.id);
|
||||
|
||||
const employeeRole = await prisma.role.upsert({
|
||||
where: { name: 'Mitarbeiter' },
|
||||
update: {},
|
||||
create: {
|
||||
name: 'Mitarbeiter',
|
||||
description: 'Kann Kunden und Verträge verwalten',
|
||||
permissions: {
|
||||
create: employeePermIds.map((id) => ({ permissionId: id })),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Read-only employee
|
||||
const readOnlyPermIds = [customerReadPerm?.id, contractReadPerm?.id, platformReadPerm?.id, providerReadPerm?.id].filter(
|
||||
(id): id is number => id !== undefined
|
||||
);
|
||||
|
||||
const readOnlyRole = await prisma.role.upsert({
|
||||
where: { name: 'Mitarbeiter (Nur-Lesen)' },
|
||||
update: {},
|
||||
create: {
|
||||
name: 'Mitarbeiter (Nur-Lesen)',
|
||||
description: 'Kann nur lesen, keine Änderungen',
|
||||
permissions: {
|
||||
create: readOnlyPermIds.map((id) => ({ permissionId: id })),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Customer role - read own data only (handled in middleware)
|
||||
const customerRole = await prisma.role.upsert({
|
||||
where: { name: 'Kunde' },
|
||||
update: {},
|
||||
create: {
|
||||
name: 'Kunde',
|
||||
description: 'Kann nur eigene Daten lesen',
|
||||
permissions: {
|
||||
create: readOnlyPermIds.map((id) => ({ permissionId: id })),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
console.log('Roles created');
|
||||
|
||||
// Create admin user
|
||||
const hashedPassword = await bcrypt.hash('admin', 10);
|
||||
|
||||
const adminUser = await prisma.user.upsert({
|
||||
where: { email: 'admin@admin.com' },
|
||||
update: {},
|
||||
create: {
|
||||
email: 'admin@admin.com',
|
||||
password: hashedPassword,
|
||||
firstName: 'Admin',
|
||||
lastName: 'User',
|
||||
roles: {
|
||||
create: [{ roleId: adminRole.id }],
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
console.log('Admin user created: admin@admin.com / admin');
|
||||
|
||||
// Create some sales platforms
|
||||
const platforms = ['Moon Fachhandel', 'Verivox', 'Check24', 'Eigenvermittlung'];
|
||||
for (const name of platforms) {
|
||||
await prisma.salesPlatform.upsert({
|
||||
where: { name },
|
||||
update: {},
|
||||
create: { name, isActive: true },
|
||||
});
|
||||
}
|
||||
|
||||
console.log('Sales platforms created');
|
||||
|
||||
// Create contract categories (matching existing enum values)
|
||||
const contractCategories = [
|
||||
{ code: 'ELECTRICITY', name: 'Strom', icon: 'Zap', color: '#FFC107', sortOrder: 1 },
|
||||
{ code: 'GAS', name: 'Gas', icon: 'Flame', color: '#FF5722', sortOrder: 2 },
|
||||
{ code: 'DSL', name: 'DSL', icon: 'Wifi', color: '#2196F3', sortOrder: 3 },
|
||||
{ code: 'FIBER', name: 'Glasfaser', icon: 'Cable', color: '#9C27B0', sortOrder: 4 },
|
||||
{ code: 'MOBILE', name: 'Mobilfunk', icon: 'Smartphone', color: '#4CAF50', sortOrder: 5 },
|
||||
{ code: 'TV', name: 'TV', icon: 'Tv', color: '#E91E63', sortOrder: 6 },
|
||||
{ code: 'CAR_INSURANCE', name: 'KFZ-Versicherung', icon: 'Car', color: '#607D8B', sortOrder: 7 },
|
||||
];
|
||||
|
||||
for (const category of contractCategories) {
|
||||
await prisma.contractCategory.upsert({
|
||||
where: { code: category.code },
|
||||
update: { name: category.name, icon: category.icon, color: category.color, sortOrder: category.sortOrder },
|
||||
create: category,
|
||||
});
|
||||
}
|
||||
|
||||
console.log('Contract categories created');
|
||||
|
||||
console.log('Seeding completed!');
|
||||
}
|
||||
|
||||
main()
|
||||
.catch((e) => {
|
||||
console.error(e);
|
||||
process.exit(1);
|
||||
})
|
||||
.finally(async () => {
|
||||
await prisma.$disconnect();
|
||||
});
|
||||
Reference in New Issue
Block a user