82 lines
2.4 KiB
TypeScript
82 lines
2.4 KiB
TypeScript
import { PrismaClient, Prisma, LeaveTypes, LeaveApprovalStatus } from '@prisma/client';
|
|
|
|
if (process.env.SKIP_LEAVE_REQUESTS === 'true') {
|
|
console.log('?? Seed leave-requests ignoré (SKIP_LEAVE_REQUESTS=true)');
|
|
process.exit(0);
|
|
}
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
function dateOn(y: number, m: number, d: number) {
|
|
// stocke une date (@db.Date) à minuit UTC
|
|
return new Date(Date.UTC(y, m - 1, d, 0, 0, 0));
|
|
}
|
|
|
|
async function main() {
|
|
const year = new Date().getFullYear();
|
|
const today = new Date();
|
|
|
|
const employees = await prisma.employees.findMany({ select: { id: true } });
|
|
const bankCodes = await prisma.bankCodes.findMany({
|
|
where: { categorie: 'LEAVE' },
|
|
select: { id: true, type: true },
|
|
});
|
|
|
|
if (!employees.length || !bankCodes.length) {
|
|
console.warn('No employees or LEAVE bank codes; aborting');
|
|
return;
|
|
}
|
|
|
|
const types: LeaveTypes[] = [
|
|
LeaveTypes.SICK,
|
|
LeaveTypes.VACATION,
|
|
LeaveTypes.UNPAID,
|
|
LeaveTypes.BEREAVEMENT,
|
|
LeaveTypes.PARENTAL,
|
|
LeaveTypes.LEGAL,
|
|
LeaveTypes.WEDDING,
|
|
];
|
|
const statuses: LeaveApprovalStatus[] = [
|
|
LeaveApprovalStatus.PENDING,
|
|
LeaveApprovalStatus.APPROVED,
|
|
LeaveApprovalStatus.DENIED,
|
|
LeaveApprovalStatus.CANCELLED,
|
|
LeaveApprovalStatus.ESCALATED,
|
|
];
|
|
|
|
const futureMonths = [8, 9, 10, 11, 12]; // Août ? Déc. (1-based)
|
|
|
|
const rows: Prisma.LeaveRequestsCreateManyInput[] = [];
|
|
|
|
for (let i = 0; i < 10; i++) {
|
|
const emp = employees[i % employees.length];
|
|
const m = futureMonths[i % futureMonths.length];
|
|
const date = dateOn(year, m, 5 + i); // 5..14
|
|
if (date <= today) continue; // garantir « futur »
|
|
|
|
const type = types[i % types.length];
|
|
const status = statuses[i % statuses.length];
|
|
const bc = bankCodes[i % bankCodes.length];
|
|
const requestedHours = 4 + (i % 5); // 4 ? 8 h
|
|
const payableHours = status === LeaveApprovalStatus.APPROVED ? Math.min(requestedHours, 8) : null;
|
|
|
|
rows.push({
|
|
employee_id: emp.id,
|
|
bank_code_id: bc.id,
|
|
leave_type: type,
|
|
date,
|
|
comment: `Future leave #${i + 1} (${bc.type})`,
|
|
approval_status: status,
|
|
requested_hours: requestedHours,
|
|
payable_hours: payableHours,
|
|
});
|
|
}
|
|
|
|
if (rows.length) {
|
|
await prisma.leaveRequests.createMany({ data: rows });
|
|
}
|
|
|
|
console.log(`? LeaveRequests (future): ${rows.length} rows`);
|
|
}
|
|
|
|
main().finally(() => prisma.$disconnect()); |