93 lines
2.8 KiB
TypeScript
93 lines
2.8 KiB
TypeScript
import { PrismaClient, Roles } from '@prisma/client';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
// base sans underscore, en string
|
|
const BASE_PHONE = "1100000000";
|
|
|
|
function emailFor(i: number) {
|
|
return `user${i + 1}@example.test`;
|
|
}
|
|
|
|
async function main() {
|
|
const usersData: {
|
|
first_name: string;
|
|
last_name: string;
|
|
email: string;
|
|
phone_number: string;
|
|
residence?: string | null;
|
|
role: Roles;
|
|
}[] = [];
|
|
|
|
const firstNames = ['Alex','Sam','Chris','Jordan','Taylor','Morgan','Jamie','Robin','Avery','Casey'];
|
|
const lastNames = ['Smith','Johnson','Williams','Brown','Jones','Miller','Davis','Wilson','Taylor','Clark'];
|
|
|
|
const pick = <T>(arr: T[]) => arr[Math.floor(Math.random() * arr.length)];
|
|
|
|
// 40 employees, avec une distribution initiale
|
|
const rolesForEmployees: Roles[] = [
|
|
Roles.ADMIN,
|
|
...Array(4).fill(Roles.SUPERVISOR), // 4 superviseurs
|
|
Roles.HR,
|
|
Roles.ACCOUNTING,
|
|
...Array(33).fill(Roles.EMPLOYEE),
|
|
];
|
|
|
|
// --- Normalisation : forcer user5@example.test à SUPERVISOR ---
|
|
// user5 => index 4 (i = 4)
|
|
rolesForEmployees[4] = Roles.SUPERVISOR;
|
|
|
|
for (let i = 0; i < 40; i++) {
|
|
const fn = pick(firstNames);
|
|
const ln = pick(lastNames);
|
|
usersData.push({
|
|
first_name: fn,
|
|
last_name: ln,
|
|
email: emailFor(i),
|
|
phone_number: BASE_PHONE + i.toString(),
|
|
residence: Math.random() < 0.5 ? 'QC' : 'ON',
|
|
role: rolesForEmployees[i],
|
|
});
|
|
}
|
|
|
|
// 10 customers
|
|
for (let i = 40; i < 50; i++) {
|
|
const fn = pick(firstNames);
|
|
const ln = pick(lastNames);
|
|
usersData.push({
|
|
first_name: fn,
|
|
last_name: ln,
|
|
email: emailFor(i),
|
|
phone_number: BASE_PHONE + i.toString(),
|
|
residence: Math.random() < 0.5 ? 'QC' : 'ON',
|
|
role: Roles.CUSTOMER,
|
|
});
|
|
}
|
|
|
|
// 1) Insert (sans doublons)
|
|
await prisma.users.createMany({ data: usersData, skipDuplicates: true });
|
|
|
|
// 2) Validation/Correction post-insert :
|
|
// - garantir que user5@example.test est SUPERVISOR
|
|
// - si jamais le projet avait un user avec la typo, on tente aussi de le corriger (fallback)
|
|
const targetEmails = ['user5@example.test', 'user5@examplte.tset'];
|
|
for (const email of targetEmails) {
|
|
try {
|
|
await prisma.users.update({
|
|
where: { email },
|
|
data: { role: Roles.SUPERVISOR },
|
|
});
|
|
console.log(`✓ Validation: ${email} est SUPERVISOR`);
|
|
break; // on s'arrête dès qu'on a corrigé l'un des deux
|
|
} catch {
|
|
// ignore si non trouvé, on tente l'autre
|
|
}
|
|
}
|
|
|
|
// 3) Petite vérif : compter les superviseurs pour sanity check
|
|
const supCount = await prisma.users.count({ where: { role: Roles.SUPERVISOR } });
|
|
console.log(`✓ Users: 50 rows (40 employees, 10 customers) — SUPERVISORS: ${supCount}`);
|
|
}
|
|
|
|
main().finally(() => prisma.$disconnect());
|