import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); async function main() { console.log('Seeding database...'); // Create default roles const roles = await Promise.all([ prisma.role.upsert({ where: { name: 'admin' }, update: {}, create: { name: 'admin', description: 'Administrator with full system access', permissions: JSON.stringify([ 'users:read', 'users:write', 'users:delete', 'employees:read', 'employees:write', 'employees:delete', 'departments:read', 'departments:write', 'departments:delete', 'roles:read', 'roles:write', 'roles:delete', 'settings:read', 'settings:write', ]), isSystem: true, }, }), prisma.role.upsert({ where: { name: 'hr-manager' }, update: {}, create: { name: 'hr-manager', description: 'HR Manager with access to employee data', permissions: JSON.stringify([ 'users:read', 'users:write', 'employees:read', 'employees:write', 'departments:read', 'absences:read', 'absences:write', 'time-entries:read', 'time-entries:write', 'reviews:read', 'reviews:write', ]), isSystem: true, }, }), prisma.role.upsert({ where: { name: 'team-lead' }, update: {}, create: { name: 'team-lead', description: 'Team leader with access to team data', permissions: JSON.stringify([ 'users:read', 'employees:read', 'departments:read', 'absences:read', 'absences:approve', 'time-entries:read', 'reviews:read', 'reviews:write', 'skills:read', 'skills:write', ]), isSystem: true, }, }), prisma.role.upsert({ where: { name: 'employee' }, update: {}, create: { name: 'employee', description: 'Regular employee with basic access', permissions: JSON.stringify([ 'profile:read', 'profile:write', 'absences:read', 'absences:request', 'time-entries:read', 'time-entries:write', 'skills:read', ]), isSystem: true, }, }), ]); console.log(`Created ${roles.length} roles`); // Create default departments const departments = await Promise.all([ prisma.department.upsert({ where: { code: 'MGMT' }, update: {}, create: { name: 'Management', code: 'MGMT', }, }), prisma.department.upsert({ where: { code: 'HR' }, update: {}, create: { name: 'Human Resources', code: 'HR', }, }), prisma.department.upsert({ where: { code: 'ENG' }, update: {}, create: { name: 'Engineering', code: 'ENG', }, }), prisma.department.upsert({ where: { code: 'PROD' }, update: {}, create: { name: 'Production', code: 'PROD', }, }), prisma.department.upsert({ where: { code: 'QA' }, update: {}, create: { name: 'Quality Assurance', code: 'QA', }, }), ]); console.log(`Created ${departments.length} departments`); // Create admin user (for development) if (process.env.NODE_ENV !== 'production') { const adminRole = roles.find((r) => r.name === 'admin'); const mgmtDept = departments.find((d) => d.code === 'MGMT'); if (adminRole && mgmtDept) { const adminUser = await prisma.user.upsert({ where: { email: 'admin@tos.local' }, update: {}, create: { email: 'admin@tos.local', firstName: 'System', lastName: 'Administrator', departmentId: mgmtDept.id, roles: { create: { roleId: adminRole.id, }, }, }, }); console.log(`Created admin user: ${adminUser.email}`); } } // Create default skills const skills = await Promise.all([ prisma.skill.upsert({ where: { name_departmentId: { name: 'Communication', departmentId: null as unknown as string } }, update: {}, create: { name: 'Communication', description: 'Effective verbal and written communication', category: 'Soft Skills', }, }), prisma.skill.upsert({ where: { name_departmentId: { name: 'Problem Solving', departmentId: null as unknown as string } }, update: {}, create: { name: 'Problem Solving', description: 'Analytical thinking and problem resolution', category: 'Soft Skills', }, }), prisma.skill.upsert({ where: { name_departmentId: { name: 'Team Collaboration', departmentId: null as unknown as string } }, update: {}, create: { name: 'Team Collaboration', description: 'Working effectively in a team environment', category: 'Soft Skills', }, }), ]); console.log(`Created ${skills.length} skills`); console.log('Database seeding completed!'); } main() .catch((e) => { console.error('Error seeding database:', e); process.exit(1); }) .finally(async () => { await prisma.$disconnect(); });