clean(time_and_attendance): cleaning helpers, types, mappers, constants

This commit is contained in:
Matthieu Haineault 2025-10-27 15:50:57 -04:00
parent 4cb01de970
commit 7eb3844713
5 changed files with 72 additions and 71 deletions

View File

@ -3,9 +3,9 @@ import { PrismaService } from "src/prisma/prisma.service";
import { computeHours } from "src/common/utils/date-utils";
import { PayPeriodOverviewDto } from "../dtos/overview-pay-period.dto";
import { EmployeePeriodOverviewDto } from "../dtos/overview-employee-period.dto";
import { computePeriod, listPayYear, payYearOfDate } from "../utils/pay-year.util";
import { PayPeriodDto } from "../dtos/pay-period.dto";
import { mapPayPeriodToDto } from "../mappers/pay-periods.mapper";
import { computePeriod, listPayYear, payYearOfDate } from "src/time-and-attendance/utils/date-time.utils";
@Injectable()
export class PayPeriodsQueryService {

View File

@ -1,35 +0,0 @@
import { toStringFromDate, toUTCDateFromString } from "src/time-and-attendance/utils/date-time.utils";
export const ANCHOR_ISO = '2023-12-17'; // ancre date
export function payYearOfDate(date: string | Date, anchorISO = ANCHOR_ISO): number {
const ANCHOR = toUTCDateFromString(anchorISO);
const d = toUTCDateFromString(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;
}
//compute labels for periods
export function computePeriod(pay_year: number, period_no: number, anchorISO = ANCHOR_ISO) {
const ANCHOR = toUTCDateFromString(anchorISO);
const cycles = pay_year - (ANCHOR.getUTCFullYear() + 1);
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 {
period_no: period_no,
pay_year: pay_year,
payday: toStringFromDate(pay),
period_start: toStringFromDate(start),
period_end: toStringFromDate(end),
label: `${toStringFromDate(start)}.${toStringFromDate(end)}`,
start, end,
};
}
//list of all 26 periods for a full year
export function listPayYear(pay_year: number, anchorISO = ANCHOR_ISO) {
return Array.from({ length: PERIODS_PER_YEAR }, (_, i) => computePeriod(pay_year, i + 1, anchorISO));
}

View File

@ -2,6 +2,7 @@ const NUMBER_OF_TIMESHEETS_TO_RETURN = 2;
const DAILY_LIMIT_HOURS = 8;
const WEEKLY_LIMIT_HOURS = 40;
const PAY_PERIOD_ANCHOR = 2023-12-17;
const ANCHOR_ISO = '2023-12-17'; // ancre date
const PERIOD_DAYS = 14;
const PERIODS_PER_YEAR = 26;
const MS_PER_DAY = 86_400_000;

View File

@ -52,4 +52,35 @@ export const sevenDaysFrom = (date: Date | string): Date[] => {
d.setUTCDate(d.getUTCDate() + i );
return d;
});
}
export function payYearOfDate(date: string | Date, anchorISO = ANCHOR_ISO): number {
const ANCHOR = toUTCDateFromString(anchorISO);
const d = toUTCDateFromString(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;
}
//compute labels for periods
export function computePeriod(pay_year: number, period_no: number, anchorISO = ANCHOR_ISO) {
const ANCHOR = toUTCDateFromString(anchorISO);
const cycles = pay_year - (ANCHOR.getUTCFullYear() + 1);
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 {
period_no: period_no,
pay_year: pay_year,
payday: toStringFromDate(pay),
period_start: toStringFromDate(start),
period_end: toStringFromDate(end),
label: `${toStringFromDate(start)}.${toStringFromDate(end)}`,
start, end,
};
}
//list of all 26 periods for a full year
export function listPayYear(pay_year: number, anchorISO = ANCHOR_ISO) {
return Array.from({ length: PERIODS_PER_YEAR }, (_, i) => computePeriod(pay_year, i + 1, anchorISO));
}

View File

@ -1,45 +1,49 @@
import { Prisma } from "@prisma/client";
export const expense_select = {
id: true,
timesheet_id: true,
bank_code_id: true,
attachment: true,
date: true,
amount: true,
mileage: true,
comment: true,
supervisor_comment: true,
is_approved: true,
id: true,
timesheet_id: true,
bank_code_id: true,
attachment: true,
date: true,
amount: true,
mileage: true,
comment: true,
supervisor_comment: true,
is_approved: true,
} satisfies Prisma.ExpensesSelect;
export const shift_select = {
id: true,
timesheet_id: true,
bank_code_id: true,
date: true,
start_time: true,
end_time: true,
is_remote: true,
is_approved: true,
comment: true,
id: true,
timesheet_id: true,
bank_code_id: true,
date: true,
start_time: true,
end_time: true,
is_remote: true,
is_approved: true,
comment: true,
} satisfies Prisma.ShiftsSelect;
export const leaveRequestsSelect = {
id: true,
bank_code_id: true,
leave_type: true,
date: true,
payable_hours: true,
requested_hours: true,
comment: true,
approval_status: true,
employee: { select: {
id: true,
user: { select: {
email: true,
first_name: true,
last_name: true,
}},
}},
id: true,
bank_code_id: true,
leave_type: true,
date: true,
payable_hours: true,
requested_hours: true,
comment: true,
approval_status: true,
employee: {
select: {
id: true,
user: {
select: {
email: true,
first_name: true,
last_name: true,
},
},
}
},
} satisfies Prisma.LeaveRequestsSelect;