refactor(shifts): modified shifts.utils to use ISO format instead of UTC
This commit is contained in:
parent
4ff512a207
commit
2f0982c952
|
|
@ -5,3 +5,13 @@ export type DayShiftResponse = {
|
|||
is_remote: boolean;
|
||||
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;
|
||||
}
|
||||
|
|
@ -7,7 +7,7 @@ export function overlaps(
|
|||
a_end_ms: number,
|
||||
b_start_ms: number,
|
||||
b_end_ms: number,
|
||||
): boolean {
|
||||
): boolean {
|
||||
return a_start_ms < b_end_ms && b_start_ms < a_end_ms;
|
||||
}
|
||||
|
||||
|
|
@ -22,18 +22,39 @@ export function resolveBankCodeByType(type: string): Promise<number> {
|
|||
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
|
||||
const date = payload.date;
|
||||
const start_time = timeFromHHMM(payload.start_time);
|
||||
const end_time = timeFromHHMM(payload.end_time );
|
||||
const date = payload.date?.trim();
|
||||
const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(date ?? '');
|
||||
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 is_remote = payload.is_remote === true;
|
||||
const is_approved = payload.is_approved === false;
|
||||
//normalize comment
|
||||
const raw_comment = payload.comment ?? null;
|
||||
const trimmed = typeof raw_comment === 'string' ? raw_comment.trim() : null;
|
||||
const comment = trimmed && trimmed.length > 0 ? trimmed: null;
|
||||
const trimmed = typeof payload.comment === 'string' ? payload.comment.trim() : null;
|
||||
const comment = trimmed && trimmed.length > 0 ? trimmed : null;
|
||||
|
||||
return { date, start_time, end_time, type, is_remote, is_approved, comment };
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user