refactor(shifts): modified shifts.utils to use ISO format instead of UTC

This commit is contained in:
Matthieu Haineault 2025-10-10 17:01:05 -04:00
parent 4ff512a207
commit 2f0982c952
2 changed files with 59 additions and 28 deletions

View File

@ -5,3 +5,13 @@ export type DayShiftResponse = {
is_remote: boolean; is_remote: boolean;
comment: string | null; comment: string | null;
} }
export type ShiftPayload = {
date: string;
start_time: string;
end_time: string;
type: string;
is_remote: boolean;
is_approved: boolean;
comment?: string | null;
}

View File

@ -22,17 +22,38 @@ export function resolveBankCodeByType(type: string): Promise<number> {
return bank.id; return bank.id;
} }
export function normalizeShiftPayload(payload: ShiftPayloadDto) { export function normalizeShiftPayload(payload: {
date: string,
start_time: string,
end_time: string,
type: string,
is_remote: boolean,
is_approved: boolean,
comment?: string | null,
}) {
//normalize shift's infos //normalize shift's infos
const date = payload.date; const date = payload.date?.trim();
const start_time = timeFromHHMM(payload.start_time); const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(date ?? '');
const end_time = timeFromHHMM(payload.end_time ); if (!m) throw new Error(`Invalid date format (expected YYYY-MM-DD): "${payload.date}"`);
const year = Number(m[1]), mo = Number(m[2]), d = Number(m[3]);
const asLocalDateOn = (input: string): Date => {
// HH:mm ?
const hm = /^(\d{2}):(\d{2})$/.exec((input ?? '').trim());
if (hm) return new Date(year, mo - 1, d, Number(hm[1]), Number(hm[2]), 0, 0);
const iso = new Date(input);
if (isNaN(iso.getTime())) throw new Error(`Invalid time: "${input}"`);
return new Date(year, mo - 1, d, iso.getHours(), iso.getMinutes(), iso.getSeconds(), iso.getMilliseconds());
};
const start_time = asLocalDateOn(payload.start_time);
const end_time = asLocalDateOn(payload.end_time);
const type = (payload.type || '').trim().toUpperCase(); const type = (payload.type || '').trim().toUpperCase();
const is_remote = payload.is_remote === true; const is_remote = payload.is_remote === true;
const is_approved = payload.is_approved === false; const is_approved = payload.is_approved === false;
//normalize comment //normalize comment
const raw_comment = payload.comment ?? null; const trimmed = typeof payload.comment === 'string' ? payload.comment.trim() : null;
const trimmed = typeof raw_comment === 'string' ? raw_comment.trim() : null;
const comment = trimmed && trimmed.length > 0 ? trimmed : null; const comment = trimmed && trimmed.length > 0 ? trimmed : null;
return { date, start_time, end_time, type, is_remote, is_approved, comment }; return { date, start_time, end_time, type, is_remote, is_approved, comment };