shoc-frontend-new/src/domain/work-orders/utils/list-week-utils.ts

68 lines
1.8 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { addDaysIso, mondayOf, todayIso } from "@/domain/work-orders/utils/wizard-date-utils";
export interface WorkWeek {
mondayIso: string;
label: string;
live: boolean;
}
export function currentWeekMonday(): string {
return mondayOf(todayIso());
}
export function weekRange(mondayIso: string): [string, string] {
return [mondayIso, addDaysIso(mondayIso, 4)];
}
export function compactWeekLabel(mondayIso: string): string {
if (!/^\d{4}-\d{2}-\d{2}$/.test(mondayIso)) {
return "";
}
const fridayIso = addDaysIso(mondayIso, 4);
const [, sm, sd] = mondayIso.split("-").map(Number);
const [, em, ed] = fridayIso.split("-").map(Number);
return `${sm}/${sd}–${em}/${ed}`;
}
export function isLiveWeek(mondayIso: string): boolean {
return mondayIso === currentWeekMonday();
}
export function isFutureWeek(mondayIso: string): boolean {
return mondayIso > currentWeekMonday();
}
export function nextWeekMonday(): string {
return addDaysIso(currentWeekMonday(), 7);
}
function toWorkWeek(mondayIso: string): WorkWeek {
return {
mondayIso,
label: compactWeekLabel(mondayIso),
live: isLiveWeek(mondayIso),
};
}
export function buildWeekWindow(anchorMonday: string, before = 4, after = 4): WorkWeek[] {
const weeks: WorkWeek[] = [];
const liveMonday = currentWeekMonday();
const upcomingMonday = nextWeekMonday();
for (let i = -before; i <= after; i += 1) {
const mondayIso = addDaysIso(anchorMonday, i * 7);
if (isFutureWeek(mondayIso) && mondayIso !== upcomingMonday) {
continue;
}
weeks.push(toWorkWeek(mondayIso));
}
for (const requiredMonday of [liveMonday, upcomingMonday]) {
if (!weeks.some((week) => week.mondayIso === requiredMonday)) {
weeks.push(toWorkWeek(requiredMonday));
}
}
weeks.sort((a, b) => a.mondayIso.localeCompare(b.mondayIso));
return weeks;
}