87 lines
2.4 KiB
TypeScript
87 lines
2.4 KiB
TypeScript
import { PrismaClient, Roles } from '@prisma/client';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
function randInt(min: number, max: number) {
|
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
|
}
|
|
|
|
function randomPastDate(yearsBack = 3) {
|
|
const now = new Date();
|
|
const past = new Date(now);
|
|
past.setFullYear(now.getFullYear() - yearsBack);
|
|
const t = randInt(past.getTime(), now.getTime());
|
|
const d = new Date(t);
|
|
d.setHours(0,0,0,0);
|
|
return d;
|
|
}
|
|
|
|
const jobTitles = [
|
|
'Directeur des ventes',
|
|
'Directeur technique',
|
|
'Programmeur',
|
|
'Technicien',
|
|
'Comptable',
|
|
'Magasinier',
|
|
'Responsable Resources Humaines',
|
|
'Conseiller en vente',
|
|
'Support technique',
|
|
];
|
|
|
|
function randomTitle() {
|
|
return jobTitles[randInt(0, jobTitles.length -1)];
|
|
}
|
|
|
|
async function main() {
|
|
const employeeUsers = await prisma.users.findMany({
|
|
where: { role: { in: [Roles.ADMIN, Roles.SUPERVISOR, Roles.HR, Roles.ACCOUNTING, Roles.EMPLOYEE] } },
|
|
orderBy: { email: 'asc' },
|
|
});
|
|
|
|
// Create supervisors first
|
|
const supervisorUsers = employeeUsers.filter(u => u.role === Roles.SUPERVISOR);
|
|
const supervisorEmployeeIds: number[] = [];
|
|
|
|
for (const u of supervisorUsers) {
|
|
const emp = await prisma.employees.upsert({
|
|
where: { user_id: u.id },
|
|
update: {},
|
|
create: {
|
|
user_id: u.id,
|
|
external_payroll_id: randInt(10000, 99999),
|
|
company_code: randInt(1, 5),
|
|
first_work_day: randomPastDate(3),
|
|
last_work_day: null,
|
|
job_title: randomTitle(),
|
|
},
|
|
});
|
|
supervisorEmployeeIds.push(emp.id);
|
|
}
|
|
|
|
// Create remaining employees, assign a random supervisor (admin can have none)
|
|
for (const u of employeeUsers) {
|
|
const already = await prisma.employees.findUnique({ where: { user_id: u.id } });
|
|
if (already) continue;
|
|
|
|
const supervisor_id =
|
|
u.role === Roles.ADMIN ? null : supervisorEmployeeIds[randInt(0, supervisorEmployeeIds.length - 1)];
|
|
|
|
await prisma.employees.create({
|
|
data: {
|
|
user_id: u.id,
|
|
external_payroll_id: randInt(10000, 99999),
|
|
company_code: randInt(1, 5),
|
|
first_work_day: randomPastDate(3),
|
|
last_work_day: null,
|
|
supervisor_id,
|
|
job_title: randomTitle(),
|
|
},
|
|
});
|
|
}
|
|
|
|
const total = await prisma.employees.count();
|
|
console.log(`✓ Employees: ${total} rows (with supervisors linked)`);
|
|
}
|
|
|
|
main().finally(() => prisma.$disconnect());
|