fix(seeders): fix holiday bank_code and week_start logics to match Sunday to Saturday

This commit is contained in:
Matthieu Haineault 2025-10-14 12:25:09 -04:00
parent a88aaf34f0
commit d1c41ea1bd
2 changed files with 18 additions and 16 deletions

View File

@ -6,17 +6,19 @@ const prisma = new PrismaClient();
const PREVIOUS_WEEKS = 16; // nombre de semaines à créer (passé)
const INCLUDE_CURRENT = false; // true si tu veux aussi la semaine courante
// Lundi (UTC) de la semaine courante
function mondayOfThisWeekUTC(now = new Date()) {
// Dimanche (UTC) de la semaine courante
function sundayOfThisWeekUTC(now = new Date()) {
// normalise à minuit UTC du jour courant
const d = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()));
const day = d.getUTCDay(); // 0=Dim, 1=Lun, ...
const diffToMonday = (day + 6) % 7; // 0 si lundi
d.setUTCDate(d.getUTCDate() - diffToMonday);
const day = d.getUTCDay(); // 0=Dim, 1=Lun, ... 6=Sam
// recule jusqu'au dimanche de cette semaine
d.setUTCDate(d.getUTCDate() - day);
d.setUTCHours(0, 0, 0, 0);
return d;
}
function mondayNWeeksBefore(monday: Date, n: number) {
const d = new Date(monday);
function sundayNWeeksBefore(sunday: Date, n: number) {
const d = new Date(sunday);
d.setUTCDate(d.getUTCDate() - n * 7);
return d;
}
@ -28,21 +30,21 @@ async function main() {
return;
}
// Construit la liste des lundis (1 par semaine)
const mondays: Date[] = [];
const mondayThisWeek = mondayOfThisWeekUTC();
if (INCLUDE_CURRENT) mondays.push(mondayThisWeek);
// Construit la liste des dimanches (1 par semaine)
const sundays: Date[] = [];
const sundayThisWeek = sundayOfThisWeekUTC();
if (INCLUDE_CURRENT) sundays.push(sundayThisWeek);
for (let n = 1; n <= PREVIOUS_WEEKS; n++) {
mondays.push(mondayNWeeksBefore(mondayThisWeek, n));
sundays.push(sundayNWeeksBefore(sundayThisWeek, n));
}
// Prépare les lignes (1 timesheet / employé / semaine)
const rows: Prisma.TimesheetsCreateManyInput[] = [];
for (const e of employees) {
for (const monday of mondays) {
for (const sunday of sundays) {
rows.push({
employee_id: e.id,
start_date: monday,
start_date: sunday,
is_approved: Math.random() < 0.3,
} as Prisma.TimesheetsCreateManyInput);
}

View File

@ -71,7 +71,7 @@ async function getOrCreateTimesheet(employee_id: number, start_date: Date) {
async function main() {
// --- Bank codes (pondérés: surtout G1 = régulier) ---
const BANKS = ['G1', 'G56', 'G48', 'G700', 'G105', 'G305'] as const;
const BANKS = ['G1', 'G56', 'G48','G105','G104', 'G305'] as const;
const WEIGHTED_CODES = [
'G1','G1','G1','G1','G1','G1','G1','G1','G1','G1','G1','G1',
'G56','G48','G104','G105','G305','G1','G1','G1','G1','G1','G1'
@ -181,7 +181,7 @@ async function main() {
data: {
timesheet_id: ts.id,
bank_code_id: bcMorningId,
comment: `Matin J${di + 1} (sem ${monday.toISOString().slice(0, 10)}) emp ${e.id} ${bcMorningCode}`,
comment: `Matin J${di + 1} (sem ${monday.toISOString().slice(0, 10)}) emp ${e.id} - ${bcMorningCode}`,
date,
start_time: timeAt(startH, startM),
end_time: timeAt(lunchStartHM.h, lunchStartHM.m),