import { PrismaClient } from '@prisma/client'; const prisma = new PrismaClient(); // Lundi (UTC) de la date fournie function mondayOfThisWeekUTC(now = new Date()) { const d = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate())); const day = d.getUTCDay(); const diffToMonday = (day + 6) % 7; d.setUTCDate(d.getUTCDate() - diffToMonday); d.setUTCHours(0, 0, 0, 0); return d; } // Dates Lundi→Vendredi (UTC minuit) function currentWeekDates() { const monday = mondayOfThisWeekUTC(); return Array.from({ length: 5 }, (_, i) => { const d = new Date(monday); d.setUTCDate(monday.getUTCDate() + i); return d; }); } function rndInt(min: number, max: number) { return Math.floor(Math.random() * (max - min + 1)) + min; } function rndAmount(minCents: number, maxCents: number) { const cents = rndInt(minCents, maxCents); return (cents / 100).toFixed(2); } // Helper: garantit le timesheet de la semaine (upsert) async function getOrCreateTimesheet(employee_id: number, start_date: Date) { return prisma.timesheets.upsert({ where: { employee_id_start_date: { employee_id, start_date } }, update: {}, create: { employee_id, start_date, is_approved: Math.random() < 0.3 }, select: { id: true }, }); } async function main() { // Codes autorisés (aléatoires à chaque dépense) const BANKS = ['G517', 'G503', 'G502', 'G202', 'G234'] as const; const bcRows = await prisma.bankCodes.findMany({ where: { bank_code: { in: BANKS as unknown as string[] } }, select: { id: true, bank_code: true }, }); const bcMap = new Map(bcRows.map(c => [c.bank_code, c.id])); for (const c of BANKS) { if (!bcMap.has(c)) throw new Error(`Bank code manquant: ${c}`); } const employees = await prisma.employees.findMany({ select: { id: true } }); if (!employees.length) { console.warn('Aucun employé — rien à insérer.'); return; } const weekDays = currentWeekDates(); const monday = weekDays[0]; const friday = weekDays[4]; let created = 0; for (const e of employees) { // 1) Semaine courante → assurer le timesheet de la semaine const weekStart = mondayOfThisWeekUTC(); const ts = await getOrCreateTimesheet(e.id, weekStart); // 2) Skip si l’employé a déjà une dépense cette semaine (on garantit ≥1) const already = await prisma.expenses.findFirst({ where: { timesheet_id: ts.id, date: { gte: monday, lte: friday } }, select: { id: true }, }); if (already) continue; // 3) Choix aléatoire du code + jour const randomCode = BANKS[Math.floor(Math.random() * BANKS.length)]; const bank_code_id = bcMap.get(randomCode)!; const date = weekDays[Math.floor(Math.random() * weekDays.length)]; // 4) Montant varié const amount = randomCode === 'G503' ? rndAmount(1000, 7500) // 10.00..75.00 : rndAmount(2000, 25000); // 20.00..250.00 await prisma.expenses.create({ data: { timesheet_id: ts.id, bank_code_id, date, amount, attachement: null, description: `Expense ${randomCode} ${amount}$ (emp ${e.id})`, is_approved: Math.random() < 0.6, supervisor_comment: Math.random() < 0.2 ? 'OK' : null, }, }); created++; } const total = await prisma.expenses.count(); console.log(`✓ Expenses: ${created} nouvelles lignes, ${total} total rows (≥1 expense/employee pour la semaine courante)`); } main().finally(() => prisma.$disconnect());