refactor(seeders): added complexity to shifts and expenses seeders
This commit is contained in:
parent
4f7563ce9b
commit
a73ed4b620
|
|
@ -504,6 +504,52 @@
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"/timesheets/shifts/{email}": {
|
||||||
|
"post": {
|
||||||
|
"operationId": "TimesheetsController_createTimesheetShifts",
|
||||||
|
"parameters": [
|
||||||
|
{
|
||||||
|
"name": "email",
|
||||||
|
"required": true,
|
||||||
|
"in": "path",
|
||||||
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "offset",
|
||||||
|
"required": true,
|
||||||
|
"in": "query",
|
||||||
|
"schema": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"requestBody": {
|
||||||
|
"required": true,
|
||||||
|
"content": {
|
||||||
|
"application/json": {
|
||||||
|
"schema": {
|
||||||
|
"$ref": "#/components/schemas/CreateWeekShiftsDto"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"responses": {
|
||||||
|
"201": {
|
||||||
|
"description": ""
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"security": [
|
||||||
|
{
|
||||||
|
"access-token": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"tags": [
|
||||||
|
"Timesheets"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
},
|
||||||
"/timesheets/{id}": {
|
"/timesheets/{id}": {
|
||||||
"get": {
|
"get": {
|
||||||
"operationId": "TimesheetsController_findOne",
|
"operationId": "TimesheetsController_findOne",
|
||||||
|
|
@ -2359,6 +2405,10 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"CreateWeekShiftsDto": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {}
|
||||||
|
},
|
||||||
"CreateTimesheetDto": {
|
"CreateTimesheetDto": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {}
|
"properties": {}
|
||||||
|
|
|
||||||
|
|
@ -4,14 +4,16 @@ const prisma = new PrismaClient();
|
||||||
|
|
||||||
// ====== Config ======
|
// ====== Config ======
|
||||||
const PREVIOUS_WEEKS = 5;
|
const PREVIOUS_WEEKS = 5;
|
||||||
const INCLUDE_CURRENT = false;
|
const INCLUDE_CURRENT = true;
|
||||||
|
const INCR = 15; // incrément ferme de 15 minutes (0.25 h)
|
||||||
|
const DAY_MIN = 5 * 60; // 5h
|
||||||
|
const DAY_MAX = 11 * 60; // 11h
|
||||||
|
const HARD_END = 19 * 60 + 30; // 19:30
|
||||||
|
|
||||||
// Times-only via Date (UTC 1970-01-01)
|
// ====== Helpers temps ======
|
||||||
function timeAt(hour: number, minute: number) {
|
function timeAt(hour: number, minute: number) {
|
||||||
return new Date(Date.UTC(1970, 0, 1, hour, minute, 0));
|
return new Date(Date.UTC(1970, 0, 1, hour, minute, 0));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Lundi (UTC) de la date fournie
|
|
||||||
function mondayOfThisWeekUTC(now = new Date()) {
|
function mondayOfThisWeekUTC(now = new Date()) {
|
||||||
const d = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()));
|
const d = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()));
|
||||||
const day = d.getUTCDay();
|
const day = d.getUTCDay();
|
||||||
|
|
@ -20,7 +22,6 @@ function mondayOfThisWeekUTC(now = new Date()) {
|
||||||
d.setUTCHours(0, 0, 0, 0);
|
d.setUTCHours(0, 0, 0, 0);
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
function weekDatesFromMonday(monday: Date) {
|
function weekDatesFromMonday(monday: Date) {
|
||||||
return Array.from({ length: 5 }, (_, i) => {
|
return Array.from({ length: 5 }, (_, i) => {
|
||||||
const d = new Date(monday);
|
const d = new Date(monday);
|
||||||
|
|
@ -28,16 +29,35 @@ function weekDatesFromMonday(monday: Date) {
|
||||||
return d;
|
return d;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function mondayNWeeksBefore(monday: Date, n: number) {
|
function mondayNWeeksBefore(monday: Date, n: number) {
|
||||||
const d = new Date(monday);
|
const d = new Date(monday);
|
||||||
d.setUTCDate(d.getUTCDate() - n * 7);
|
d.setUTCDate(d.getUTCDate() - n * 7);
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
|
||||||
function rndInt(min: number, max: number) {
|
function rndInt(min: number, max: number) {
|
||||||
return Math.floor(Math.random() * (max - min + 1)) + min;
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
||||||
}
|
}
|
||||||
|
function clamp(n: number, min: number, max: number) {
|
||||||
|
return Math.min(max, Math.max(min, n));
|
||||||
|
}
|
||||||
|
function addMinutes(h: number, m: number, delta: number) {
|
||||||
|
const total = h * 60 + m + delta;
|
||||||
|
const hh = Math.floor(total / 60);
|
||||||
|
const mm = ((total % 60) + 60) % 60;
|
||||||
|
return { h: hh, m: mm };
|
||||||
|
}
|
||||||
|
// Aligne vers le multiple de INCR le plus proche
|
||||||
|
function quantize(mins: number): number {
|
||||||
|
const q = Math.round(mins / INCR) * INCR;
|
||||||
|
return q;
|
||||||
|
}
|
||||||
|
// Tire un multiple de INCR dans [min,max] (inclus), supposés entiers minutes
|
||||||
|
function rndQuantized(min: number, max: number): number {
|
||||||
|
const qmin = Math.ceil(min / INCR);
|
||||||
|
const qmax = Math.floor(max / INCR);
|
||||||
|
const q = rndInt(qmin, qmax);
|
||||||
|
return q * INCR;
|
||||||
|
}
|
||||||
|
|
||||||
// Helper: garantit le timesheet de la semaine (upsert)
|
// Helper: garantit le timesheet de la semaine (upsert)
|
||||||
async function getOrCreateTimesheet(employee_id: number, start_date: Date) {
|
async function getOrCreateTimesheet(employee_id: number, start_date: Date) {
|
||||||
|
|
@ -50,8 +70,13 @@ async function getOrCreateTimesheet(employee_id: number, start_date: Date) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
// Bank codes utilisés
|
// --- Bank codes (pondérés: surtout G1 = régulier) ---
|
||||||
const BANKS = ['G1', 'G56', 'G48', 'G700', 'G105', 'G305', 'G43'] as const;
|
const BANKS = ['G1', 'G56', 'G48', 'G700', 'G105', 'G305', 'G43'] as const;
|
||||||
|
const WEIGHTED_CODES = [
|
||||||
|
'G1','G1','G1','G1','G1','G1','G1','G1', // 8x régulier
|
||||||
|
'G56','G48','G700','G105','G305','G43'
|
||||||
|
] as const;
|
||||||
|
|
||||||
const bcRows = await prisma.bankCodes.findMany({
|
const bcRows = await prisma.bankCodes.findMany({
|
||||||
where: { bank_code: { in: BANKS as unknown as string[] } },
|
where: { bank_code: { in: BANKS as unknown as string[] } },
|
||||||
select: { id: true, bank_code: true },
|
select: { id: true, bank_code: true },
|
||||||
|
|
@ -70,59 +95,140 @@ async function main() {
|
||||||
const mondayThisWeek = mondayOfThisWeekUTC();
|
const mondayThisWeek = mondayOfThisWeekUTC();
|
||||||
const mondays: Date[] = [];
|
const mondays: Date[] = [];
|
||||||
if (INCLUDE_CURRENT) mondays.push(mondayThisWeek);
|
if (INCLUDE_CURRENT) mondays.push(mondayThisWeek);
|
||||||
for (let n = 1; n <= PREVIOUS_WEEKS; n++) {
|
for (let n = 1; n <= PREVIOUS_WEEKS; n++) mondays.push(mondayNWeeksBefore(mondayThisWeek, n));
|
||||||
mondays.push(mondayNWeeksBefore(mondayThisWeek, n));
|
|
||||||
}
|
|
||||||
|
|
||||||
let created = 0;
|
let created = 0;
|
||||||
|
|
||||||
for (let wi = 0; wi < mondays.length; wi++) {
|
for (let wi = 0; wi < mondays.length; wi++) {
|
||||||
const monday = mondays[wi];
|
const monday = mondays[wi];
|
||||||
const weekDays = weekDatesFromMonday(monday);
|
const days = weekDatesFromMonday(monday);
|
||||||
|
|
||||||
for (let ei = 0; ei < employees.length; ei++) {
|
for (let ei = 0; ei < employees.length; ei++) {
|
||||||
const e = employees[ei];
|
const e = employees[ei];
|
||||||
|
|
||||||
const baseStartHour = 6 + (ei % 5);
|
// Cible hebdo 35–45h, multiple de 15 min
|
||||||
const baseStartMinute = (ei * 15) % 60;
|
const weeklyTargetMin = rndQuantized(35 * 60, 45 * 60);
|
||||||
|
|
||||||
for (let di = 0; di < weekDays.length; di++) {
|
// Start de base (7:00, 7:15, 7:30, 7:45, 8:00, 8:15, 8:30, 8:45, 9:00 ...)
|
||||||
const date = weekDays[di];
|
const baseStartH = 7 + (ei % 3); // 7,8,9
|
||||||
|
const baseStartM = ( (ei * 15) % 60 ); // aligné 15 min
|
||||||
|
|
||||||
// 1) Trouver/Créer le timesheet de CETTE semaine pour CET employé
|
// Planification journalière (5 jours) ~8h ± 45 min, quantisée 15 min
|
||||||
const weekStart = mondayOfThisWeekUTC(date);
|
const plannedDaily: number[] = [];
|
||||||
const ts = await getOrCreateTimesheet(e.id, weekStart);
|
for (let d = 0; d < 5; d++) {
|
||||||
|
const jitter = rndInt(-3, 3) * INCR; // -45..+45 par pas de 15
|
||||||
|
const base = 8 * 60 + jitter;
|
||||||
|
plannedDaily.push(quantize(clamp(base, DAY_MIN, DAY_MAX)));
|
||||||
|
}
|
||||||
|
|
||||||
// 2) Tirage aléatoire du bank_code
|
// Ajuster le 5e jour pour atteindre la cible hebdo exactement (par pas de 15)
|
||||||
const randomCode = BANKS[Math.floor(Math.random() * BANKS.length)];
|
const sumFirst4 = plannedDaily.slice(0, 4).reduce((a, b) => a + b, 0);
|
||||||
const bank_code_id = bcMap.get(randomCode)!;
|
plannedDaily[4] = quantize(clamp(weeklyTargetMin - sumFirst4, DAY_MIN, DAY_MAX));
|
||||||
|
|
||||||
// 3) Horaire
|
// Corriger le petit écart restant (devrait être multiple de 15) en redistribuant ±15
|
||||||
const duration = rndInt(4, 10);
|
let diff = weeklyTargetMin - plannedDaily.reduce((a, b) => a + b, 0);
|
||||||
const dayWeekOffset = (di + wi + (ei % 3)) % 3;
|
const step = diff > 0 ? INCR : -INCR;
|
||||||
const startH = Math.min(12, baseStartHour + dayWeekOffset);
|
let guard = 100; // anti-boucle
|
||||||
const startM = baseStartMinute;
|
while (diff !== 0 && guard-- > 0) {
|
||||||
const endH = startH + duration;
|
for (let d = 0; d < 5 && diff !== 0; d++) {
|
||||||
const endM = startM;
|
const next = plannedDaily[d] + step;
|
||||||
|
if (next >= DAY_MIN && next <= DAY_MAX) {
|
||||||
|
plannedDaily[d] = next;
|
||||||
|
diff -= step;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Upsert du timesheet (semaine)
|
||||||
|
const ts = await getOrCreateTimesheet(e.id, mondayOfThisWeekUTC(days[0]));
|
||||||
|
|
||||||
|
for (let di = 0; di < 5; di++) {
|
||||||
|
const date = days[di];
|
||||||
|
const targetWorkMin = plannedDaily[di]; // multiple de 15
|
||||||
|
|
||||||
|
// Départ ~ base + jitter (par pas de 15 min aussi)
|
||||||
|
const startJitter = rndInt(-1, 2) * INCR; // -15,0,+15,+30
|
||||||
|
const { h: startH, m: startM } = addMinutes(baseStartH, baseStartM, startJitter);
|
||||||
|
|
||||||
|
// Pause: entre 11:00 et 14:00, mais pas avant start+3h ni après start+6h (le tout quantisé 15)
|
||||||
|
const earliestLunch = Math.max((startH * 60 + startM) + 3 * 60, 11 * 60);
|
||||||
|
const latestLunch = Math.min((startH * 60 + startM) + 6 * 60, 14 * 60);
|
||||||
|
const lunchStartMin = rndQuantized(earliestLunch, latestLunch);
|
||||||
|
const lunchDur = rndQuantized(30, 120); // 30..120 min en pas de 15
|
||||||
|
const lunchEndMin = lunchStartMin + lunchDur;
|
||||||
|
|
||||||
|
// Travail = (lunchStart - start) + (end - lunchEnd)
|
||||||
|
const morningWork = Math.max(0, lunchStartMin - (startH * 60 + startM)); // multiple de 15
|
||||||
|
let afternoonWork = Math.max(60, targetWorkMin - morningWork); // multiple de 15 (diff de deux multiples de 15)
|
||||||
|
if (afternoonWork % INCR !== 0) {
|
||||||
|
// sécurité (ne devrait pas arriver)
|
||||||
|
afternoonWork = quantize(afternoonWork);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fin de journée (quantisée par construction)
|
||||||
|
const endMinRaw = lunchEndMin + afternoonWork;
|
||||||
|
const endMin = Math.min(endMinRaw, HARD_END);
|
||||||
|
|
||||||
|
// Bank codes variés
|
||||||
|
const bcMorningCode = WEIGHTED_CODES[rndInt(0, WEIGHTED_CODES.length - 1)];
|
||||||
|
const bcAfternoonCode = WEIGHTED_CODES[rndInt(0, WEIGHTED_CODES.length - 1)];
|
||||||
|
const bcMorningId = bcMap.get(bcMorningCode)!;
|
||||||
|
const bcAfternoonId = bcMap.get(bcAfternoonCode)!;
|
||||||
|
|
||||||
|
// Shift matin
|
||||||
|
const lunchStartHM = { h: Math.floor(lunchStartMin / 60), m: lunchStartMin % 60 };
|
||||||
await prisma.shifts.create({
|
await prisma.shifts.create({
|
||||||
data: {
|
data: {
|
||||||
timesheet_id: ts.id,
|
timesheet_id: ts.id,
|
||||||
bank_code_id,
|
bank_code_id: bcMorningId,
|
||||||
description: `Shift ${di + 1} (semaine du ${monday.toISOString().slice(0, 10)}) emp ${e.id} — ${randomCode}`,
|
description: `Matin J${di + 1} (sem ${monday.toISOString().slice(0, 10)}) emp ${e.id} — ${bcMorningCode}`,
|
||||||
date,
|
date,
|
||||||
start_time: timeAt(startH, startM),
|
start_time: timeAt(startH, startM),
|
||||||
end_time: timeAt(endH, endM),
|
end_time: timeAt(lunchStartHM.h, lunchStartHM.m),
|
||||||
is_approved: Math.random() < 0.5,
|
is_approved: Math.random() < 0.6,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
created++;
|
created++;
|
||||||
|
|
||||||
|
// Shift après-midi (si >= 30 min — sera de toute façon multiple de 15)
|
||||||
|
const pmDuration = endMin - lunchEndMin;
|
||||||
|
if (pmDuration >= 30) {
|
||||||
|
const lunchEndHM = { h: Math.floor(lunchEndMin / 60), m: lunchEndMin % 60 };
|
||||||
|
const finalEndHM = { h: Math.floor(endMin / 60), m: endMin % 60 };
|
||||||
|
await prisma.shifts.create({
|
||||||
|
data: {
|
||||||
|
timesheet_id: ts.id,
|
||||||
|
bank_code_id: bcAfternoonId,
|
||||||
|
description: `Après-midi J${di + 1} (sem ${monday.toISOString().slice(0, 10)}) emp ${e.id} — ${bcAfternoonCode}`,
|
||||||
|
date,
|
||||||
|
start_time: timeAt(lunchEndHM.h, lunchEndHM.m),
|
||||||
|
end_time: timeAt(finalEndHM.h, finalEndHM.m),
|
||||||
|
is_approved: Math.random() < 0.6,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
created++;
|
||||||
|
} else {
|
||||||
|
// Fallback très rare : un seul shift couvrant la journée (tout en multiples de 15)
|
||||||
|
const fallbackEnd = addMinutes(startH, startM, targetWorkMin + lunchDur);
|
||||||
|
await prisma.shifts.create({
|
||||||
|
data: {
|
||||||
|
timesheet_id: ts.id,
|
||||||
|
bank_code_id: bcMap.get('G1')!,
|
||||||
|
description: `Fallback J${di + 1} (sem ${monday.toISOString().slice(0, 10)}) emp ${e.id} — G1`,
|
||||||
|
date,
|
||||||
|
start_time: timeAt(startH, startM),
|
||||||
|
end_time: timeAt(fallbackEnd.h, fallbackEnd.m),
|
||||||
|
is_approved: Math.random() < 0.6,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
created++;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const total = await prisma.shifts.count();
|
const total = await prisma.shifts.count();
|
||||||
console.log(`✓ Shifts: ${created} nouvelles lignes, ${total} total rows (${INCLUDE_CURRENT ? 'courante +' : ''}${PREVIOUS_WEEKS} semaines précédentes, L→V)`);
|
console.log(`✓ Shifts créés: ${created} | total en DB: ${total} (${INCLUDE_CURRENT ? 'inclut semaine courante, ' : ''}${PREVIOUS_WEEKS} sem passées, L→V, 2 shifts/jour, pas de décimaux foireux})`);
|
||||||
}
|
}
|
||||||
|
|
||||||
main().finally(() => prisma.$disconnect());
|
main().finally(() => prisma.$disconnect());
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,12 @@ import { PrismaClient } from '@prisma/client';
|
||||||
|
|
||||||
const prisma = new PrismaClient();
|
const prisma = new PrismaClient();
|
||||||
|
|
||||||
// Lundi (UTC) de la date fournie
|
// ====== Config ======
|
||||||
|
const WEEKS_BACK = 4; // 4 semaines avant + semaine courante
|
||||||
|
const INCLUDE_CURRENT = true; // inclure la semaine courante
|
||||||
|
const STEP_CENTS = 25; // montants en quarts de dollar (.00/.25/.50/.75)
|
||||||
|
|
||||||
|
// ====== Helpers dates ======
|
||||||
function mondayOfThisWeekUTC(now = new Date()) {
|
function mondayOfThisWeekUTC(now = new Date()) {
|
||||||
const d = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()));
|
const d = new Date(Date.UTC(now.getUTCFullYear(), now.getUTCMonth(), now.getUTCDate()));
|
||||||
const day = d.getUTCDay();
|
const day = d.getUTCDay();
|
||||||
|
|
@ -11,10 +16,13 @@ function mondayOfThisWeekUTC(now = new Date()) {
|
||||||
d.setUTCHours(0, 0, 0, 0);
|
d.setUTCHours(0, 0, 0, 0);
|
||||||
return d;
|
return d;
|
||||||
}
|
}
|
||||||
|
function mondayNWeeksBefore(monday: Date, n: number) {
|
||||||
// Dates Lundi→Vendredi (UTC minuit)
|
const d = new Date(monday);
|
||||||
function currentWeekDates() {
|
d.setUTCDate(monday.getUTCDate() - n * 7);
|
||||||
const monday = mondayOfThisWeekUTC();
|
return d;
|
||||||
|
}
|
||||||
|
// L→V (UTC minuit)
|
||||||
|
function weekDatesMonToFri(monday: Date) {
|
||||||
return Array.from({ length: 5 }, (_, i) => {
|
return Array.from({ length: 5 }, (_, i) => {
|
||||||
const d = new Date(monday);
|
const d = new Date(monday);
|
||||||
d.setUTCDate(monday.getUTCDate() + i);
|
d.setUTCDate(monday.getUTCDate() + i);
|
||||||
|
|
@ -22,15 +30,30 @@ function currentWeekDates() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ====== Helpers random / amount ======
|
||||||
function rndInt(min: number, max: number) {
|
function rndInt(min: number, max: number) {
|
||||||
return Math.floor(Math.random() * (max - min + 1)) + min;
|
return Math.floor(Math.random() * (max - min + 1)) + min;
|
||||||
}
|
}
|
||||||
function rndAmount(minCents: number, maxCents: number) {
|
// String "xx.yy" à partir de cents ENTiers (jamais de float)
|
||||||
const cents = rndInt(minCents, maxCents);
|
function centsToAmountString(cents: number): string {
|
||||||
return (cents / 100).toFixed(2);
|
const sign = cents < 0 ? '-' : '';
|
||||||
|
const abs = Math.abs(cents);
|
||||||
|
const dollars = Math.floor(abs / 100);
|
||||||
|
const c = abs % 100;
|
||||||
|
return `${sign}${dollars}.${c.toString().padStart(2, '0')}`;
|
||||||
|
}
|
||||||
|
// Tire un multiple de STEP_CENTS entre minCents et maxCents (inclus)
|
||||||
|
function rndQuantizedCents(minCents: number, maxCents: number, step = STEP_CENTS): number {
|
||||||
|
const qmin = Math.ceil(minCents / step);
|
||||||
|
const qmax = Math.floor(maxCents / step);
|
||||||
|
const q = rndInt(qmin, qmax);
|
||||||
|
return q * step;
|
||||||
|
}
|
||||||
|
function rndAmount(minCents: number, maxCents: number): string {
|
||||||
|
return centsToAmountString(rndQuantizedCents(minCents, maxCents));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Helper: garantit le timesheet de la semaine (upsert)
|
// ====== Timesheet upsert ======
|
||||||
async function getOrCreateTimesheet(employee_id: number, start_date: Date) {
|
async function getOrCreateTimesheet(employee_id: number, start_date: Date) {
|
||||||
return prisma.timesheets.upsert({
|
return prisma.timesheets.upsert({
|
||||||
where: { employee_id_start_date: { employee_id, start_date } },
|
where: { employee_id_start_date: { employee_id, start_date } },
|
||||||
|
|
@ -41,8 +64,10 @@ async function getOrCreateTimesheet(employee_id: number, start_date: Date) {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
// Codes autorisés (aléatoires à chaque dépense)
|
// Codes d'EXPENSES (exemples)
|
||||||
const BANKS = ['G517', 'G503', 'G502', 'G202', 'G234'] as const;
|
const BANKS = ['G517', 'G503', 'G502', 'G202', 'G234'] as const;
|
||||||
|
|
||||||
|
// Précharger les bank codes
|
||||||
const bcRows = await prisma.bankCodes.findMany({
|
const bcRows = await prisma.bankCodes.findMany({
|
||||||
where: { bank_code: { in: BANKS as unknown as string[] } },
|
where: { bank_code: { in: BANKS as unknown as string[] } },
|
||||||
select: { id: true, bank_code: true },
|
select: { id: true, bank_code: true },
|
||||||
|
|
@ -52,58 +77,85 @@ async function main() {
|
||||||
if (!bcMap.has(c)) throw new Error(`Bank code manquant: ${c}`);
|
if (!bcMap.has(c)) throw new Error(`Bank code manquant: ${c}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Employés
|
||||||
const employees = await prisma.employees.findMany({ select: { id: true } });
|
const employees = await prisma.employees.findMany({ select: { id: true } });
|
||||||
if (!employees.length) {
|
if (!employees.length) {
|
||||||
console.warn('Aucun employé — rien à insérer.');
|
console.warn('Aucun employé — rien à insérer.');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const weekDays = currentWeekDates();
|
// Liste des lundis (courant + 4 précédents)
|
||||||
const monday = weekDays[0];
|
const mondayThisWeek = mondayOfThisWeekUTC();
|
||||||
const friday = weekDays[4];
|
const mondays: Date[] = [];
|
||||||
|
if (INCLUDE_CURRENT) mondays.push(mondayThisWeek);
|
||||||
|
for (let n = 1; n <= WEEKS_BACK; n++) mondays.push(mondayNWeeksBefore(mondayThisWeek, n));
|
||||||
|
|
||||||
let created = 0;
|
let created = 0;
|
||||||
|
|
||||||
for (const e of employees) {
|
for (const monday of mondays) {
|
||||||
// 1) Semaine courante → assurer le timesheet de la semaine
|
const weekDays = weekDatesMonToFri(monday);
|
||||||
const weekStart = mondayOfThisWeekUTC();
|
const friday = weekDays[4];
|
||||||
const ts = await getOrCreateTimesheet(e.id, weekStart);
|
|
||||||
|
|
||||||
// 2) Skip si l’employé a déjà une dépense cette semaine (on garantit ≥1)
|
for (const e of employees) {
|
||||||
const already = await prisma.expenses.findFirst({
|
// Upsert timesheet pour CETTE semaine/employee
|
||||||
where: { timesheet_id: ts.id, date: { gte: monday, lte: friday } },
|
const ts = await getOrCreateTimesheet(e.id, monday);
|
||||||
select: { id: true },
|
|
||||||
});
|
|
||||||
if (already) continue;
|
|
||||||
|
|
||||||
// 3) Choix aléatoire du code + jour
|
// Idempotence: si déjà au moins une expense L→V, on skip la semaine
|
||||||
const randomCode = BANKS[Math.floor(Math.random() * BANKS.length)];
|
const already = await prisma.expenses.findFirst({
|
||||||
const bank_code_id = bcMap.get(randomCode)!;
|
where: { timesheet_id: ts.id, date: { gte: monday, lte: friday } },
|
||||||
const date = weekDays[Math.floor(Math.random() * weekDays.length)];
|
select: { id: true },
|
||||||
|
});
|
||||||
|
if (already) continue;
|
||||||
|
|
||||||
// 4) Montant varié
|
// 1 à 3 expenses (jours distincts)
|
||||||
const amount =
|
const count = rndInt(1, 3);
|
||||||
randomCode === 'G503'
|
const dayIndexes = [0, 1, 2, 3, 4].sort(() => Math.random() - 0.5).slice(0, count);
|
||||||
? rndAmount(1000, 7500) // 10.00..75.00
|
|
||||||
: rndAmount(2000, 25000); // 20.00..250.00
|
|
||||||
|
|
||||||
await prisma.expenses.create({
|
for (const idx of dayIndexes) {
|
||||||
data: {
|
const date = weekDays[idx];
|
||||||
timesheet_id: ts.id,
|
const code = BANKS[rndInt(0, BANKS.length - 1)];
|
||||||
bank_code_id,
|
const bank_code_id = bcMap.get(code)!;
|
||||||
date,
|
|
||||||
amount,
|
// Montants (cents) quantisés à 25¢ => aucun flottant binaire plus tard
|
||||||
attachement: null,
|
let amount: string;
|
||||||
description: `Expense ${randomCode} ${amount}$ (emp ${e.id})`,
|
switch (code) {
|
||||||
is_approved: Math.random() < 0.6,
|
case 'G503': // petites fournitures
|
||||||
supervisor_comment: Math.random() < 0.2 ? 'OK' : null,
|
amount = rndAmount(1000, 7500); // 10.00 à 75.00
|
||||||
},
|
break;
|
||||||
});
|
case 'G502': // repas
|
||||||
created++;
|
amount = rndAmount(1500, 3000); // 15.00 à 30.00
|
||||||
|
break;
|
||||||
|
case 'G202': // essence
|
||||||
|
amount = rndAmount(2000, 15000); // 20.00 à 150.00
|
||||||
|
break;
|
||||||
|
case 'G234': // hébergement
|
||||||
|
amount = rndAmount(6000, 25000); // 60.00 à 250.00
|
||||||
|
break;
|
||||||
|
case 'G517': // péages / divers
|
||||||
|
default:
|
||||||
|
amount = rndAmount(500, 5000); // 5.00 à 50.00
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
await prisma.expenses.create({
|
||||||
|
data: {
|
||||||
|
timesheet_id: ts.id,
|
||||||
|
bank_code_id,
|
||||||
|
date,
|
||||||
|
amount, // string "xx.yy" (2 décimales exactes)
|
||||||
|
attachement: null,
|
||||||
|
description: `Expense ${code} ${amount}$ (emp ${e.id})`,
|
||||||
|
is_approved: Math.random() < 0.65,
|
||||||
|
supervisor_comment: Math.random() < 0.25 ? 'OK' : null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
created++;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const total = await prisma.expenses.count();
|
const total = await prisma.expenses.count();
|
||||||
console.log(`✓ Expenses: ${created} nouvelles lignes, ${total} total rows (≥1 expense/employee pour la semaine courante)`);
|
console.log(`✓ Expenses: ${created} nouvelles lignes, ${total} total rows (sem courante + ${WEEKS_BACK} précédentes, L→V uniquement, montants en quarts de dollar)`);
|
||||||
}
|
}
|
||||||
|
|
||||||
main().finally(() => prisma.$disconnect());
|
main().finally(() => prisma.$disconnect());
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user