From bdf666237439eaac8f8671b64b0f145105406afa Mon Sep 17 00:00:00 2001 From: Matthieu Haineault Date: Thu, 21 Aug 2025 10:07:04 -0400 Subject: [PATCH] feat(label): modified label of pay-periods --- .../pay-periods/mappers/pay-periods.mapper.ts | 2 +- .../pay-periods/utils/pay-year.util.ts | 37 +++++-------------- 2 files changed, 11 insertions(+), 28 deletions(-) diff --git a/src/modules/pay-periods/mappers/pay-periods.mapper.ts b/src/modules/pay-periods/mappers/pay-periods.mapper.ts index 55c78ca..64265b6 100644 --- a/src/modules/pay-periods/mappers/pay-periods.mapper.ts +++ b/src/modules/pay-periods/mappers/pay-periods.mapper.ts @@ -13,7 +13,7 @@ export function mapPayPeriodToDto(row: PayPeriods): PayPeriodDto { period_end: toDateString(row.period_end), payday:pay, pay_year: new Date(pay).getFullYear(), - label: `${start} => ${end}`, + label: `${start}.${end}`, }; } diff --git a/src/modules/pay-periods/utils/pay-year.util.ts b/src/modules/pay-periods/utils/pay-year.util.ts index 8f2e1fe..dd9a512 100644 --- a/src/modules/pay-periods/utils/pay-year.util.ts +++ b/src/modules/pay-periods/utils/pay-year.util.ts @@ -1,5 +1,4 @@ export const ANCHOR_ISO = '2023-12-17'; // ancre date - const PERIOD_DAYS = 14; const PERIODS_PER_YEAR = 26; const MS_PER_DAY = 86_400_000; @@ -10,35 +9,19 @@ const toUTCDate = (iso: string | Date) => { }; export const toDateString = (d: Date) => d.toISOString().slice(0, 10); -export function payYearOfDate(date: string | Date, anchor_ISO = ANCHOR_ISO): number { - const ANCHOR = toUTCDate(anchor_ISO); +export function payYearOfDate(date: string | Date, anchorISO = ANCHOR_ISO): number { + const ANCHOR = toUTCDate(anchorISO); const d = toUTCDate(date); const days = Math.floor((+d - +ANCHOR) / MS_PER_DAY); const cycles = Math.floor(days / (PERIODS_PER_YEAR * PERIOD_DAYS)); return ANCHOR.getUTCFullYear() + 1 + cycles; } - -//build a string example : 10.au.23.aout.2025 or 24.aout.au.6.septembre.2025 -function buildLabel(start: Date, end: Date): string { - const start_day = String(start.getUTCDate()); - const end_day = String(end.getUTCDate()); - const start_month = String(start.getUTCMonth()); - const end_month = String(end.getUTCMonth()); - const year = String(end.getUTCFullYear()); - - if(start.getUTCMonth() === end.getUTCMonth() && start.getUTCFullYear() === end.getUTCFullYear()){ - return [start_day, 'au', end_day, end_month, year].join('.'); - } - - return [start_day, start_month, 'au', end_day, end_month, year].join('.'); -} - -//compute periods with label -export function computePeriod(pay_year: number, period_no: number, anchor_ISO = ANCHOR_ISO) { - const ANCHOR = toUTCDate(anchor_ISO); +//compute labels for periods +export function computePeriod(pay_year: number, period_no: number, anchorISO = ANCHOR_ISO) { + const ANCHOR = toUTCDate(anchorISO); const cycles = pay_year - (ANCHOR.getUTCFullYear() + 1); - const offset_periods = cycles * PERIODS_PER_YEAR + (period_no - 1); - const start = new Date(+ANCHOR + offset_periods * PERIOD_DAYS * MS_PER_DAY); + const offsetPeriods = cycles * PERIODS_PER_YEAR + (period_no - 1); + const start = new Date(+ANCHOR + offsetPeriods * PERIOD_DAYS * MS_PER_DAY); const end = new Date(+start + (PERIOD_DAYS - 1) * MS_PER_DAY); const pay = new Date(end.getTime() + 6 * MS_PER_DAY); return { @@ -47,12 +30,12 @@ export function computePeriod(pay_year: number, period_no: number, anchor_ISO = payday: toDateString(pay), period_start: toDateString(start), period_end: toDateString(end), - label: buildLabel(start, end), + label: `${toDateString(start)}.${toDateString(end)}`, start, end, }; } //list of all 26 periods for a full year -export function listPayYear(pay_year: number, anchor_ISO = ANCHOR_ISO) { - return Array.from({ length: PERIODS_PER_YEAR }, (_, i) => computePeriod(pay_year, i + 1, anchor_ISO)); +export function listPayYear(pay_year: number, anchorISO = ANCHOR_ISO) { + return Array.from({ length: PERIODS_PER_YEAR }, (_, i) => computePeriod(pay_year, i + 1, anchorISO)); }