45 lines
1.3 KiB
TypeScript
45 lines
1.3 KiB
TypeScript
import { PrismaClient } from '@prisma/client';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
function daysAgo(n:number) {
|
|
const d = new Date();
|
|
d.setUTCDate(d.getUTCDate() - n);
|
|
d.setUTCHours(0,0,0,0);
|
|
return d;
|
|
}
|
|
|
|
async function main() {
|
|
const expenseCodes = await prisma.bankCodes.findMany({ where: { type: 'EXPENSE' }, select: { id: true } });
|
|
if (!expenseCodes.length) throw new Error('Need EXPENSE bank codes');
|
|
|
|
const timesheets = await prisma.timesheets.findMany({ select: { id: true } });
|
|
if (!timesheets.length) {
|
|
console.warn('No timesheets found; aborting expenses seed.');
|
|
return;
|
|
}
|
|
|
|
// 5 expenses distribuées aléatoirement parmi les employés (via timesheets)
|
|
for (let i = 0; i < 5; i++) {
|
|
const ts = timesheets[Math.floor(Math.random() * timesheets.length)];
|
|
const bc = expenseCodes[i % expenseCodes.length];
|
|
await prisma.expenses.create({
|
|
data: {
|
|
timesheet_id: ts.id,
|
|
bank_code_id: bc.id,
|
|
date: daysAgo(3 + i),
|
|
amount: (50 + i * 10).toFixed(2),
|
|
attachement: null,
|
|
description: `Expense #${i + 1}`,
|
|
is_approved: Math.random() < 0.5,
|
|
supervisor_comment: Math.random() < 0.3 ? 'OK' : null,
|
|
},
|
|
});
|
|
}
|
|
|
|
const total = await prisma.expenses.count();
|
|
console.log(`✓ Expenses: ${total} total rows`);
|
|
}
|
|
|
|
main().finally(() => prisma.$disconnect());
|