38 lines
1.1 KiB
TypeScript
38 lines
1.1 KiB
TypeScript
// Absences are calendar dates stored as UTC dates, independent of the server timezone.
|
|
export function absenceDate(value: string) {
|
|
if (!/^\d{4}-\d{2}-\d{2}$/.test(value)) throw new Error("Ungültiges Datum.");
|
|
const date = new Date(`${value}T00:00:00.000Z`);
|
|
if (Number.isNaN(date.getTime()) || date.toISOString().slice(0, 10) !== value)
|
|
throw new Error("Ungültiges Datum.");
|
|
return date;
|
|
}
|
|
|
|
export function isAbsenceDate(value: string) {
|
|
try {
|
|
absenceDate(value);
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
export function absenceToday(now = new Date()) {
|
|
const parts = new Intl.DateTimeFormat("en-CA", {
|
|
timeZone: "Europe/Berlin",
|
|
year: "numeric",
|
|
month: "2-digit",
|
|
day: "2-digit",
|
|
}).formatToParts(now);
|
|
const part = (type: string) =>
|
|
parts.find((item) => item.type === type)!.value;
|
|
return `${part("year")}-${part("month")}-${part("day")}`;
|
|
}
|
|
|
|
export function canManageKitaAbsences(role: string) {
|
|
return ["ADMIN", "KOORDINATOR", "ERZIEHER"].includes(role);
|
|
}
|
|
|
|
export function canEditOwnAbsence(startDate: string, today: string) {
|
|
return startDate > today;
|
|
}
|