Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/view-models/src/booking/availability.vm.ts
T

216 lines
7.1 KiB
TypeScript

import type { VMState } from "../index.js";
/**
* AK-P16b · provider availability view-model + form payload codecs (M06 Block F,
* interim UI AD-28). Pure (DoD A.3): the loader resolves the provider's persisted
* rules/exceptions (AvailabilityService), this groups them for the weekly editor.
* The editor submits one serialized JSON payload (atomic full replace); the codecs
* here only validate the SHAPE — deep validation (times, timezone, windows) stays
* in AvailabilityService on the server.
*/
export type AvailabilityWeekday = "monday" | "tuesday" | "wednesday" | "thursday" | "friday" | "saturday" | "sunday";
export const AVAILABILITY_WEEKDAYS: readonly AvailabilityWeekday[] = [
"monday",
"tuesday",
"wednesday",
"thursday",
"friday",
"saturday",
"sunday",
];
export type AvailabilityExceptionKindVM = "unavailable" | "custom_window";
export interface AvailabilityRuleVM {
readonly id: string | null;
readonly dayOfWeek: AvailabilityWeekday;
/** `HH:mm` wall-clock times. */
readonly startTime: string;
readonly endTime: string;
readonly timezone: string;
}
export interface AvailabilityExceptionVM {
readonly id: string | null;
/** ISO calendar date `YYYY-MM-DD`. */
readonly date: string;
readonly kind: AvailabilityExceptionKindVM;
readonly startTime: string | null;
readonly endTime: string | null;
readonly reason: string | null;
readonly timezone: string;
}
export interface AvailabilityWeekdayGroup {
readonly day: AvailabilityWeekday;
/** i18n key for the weekday label (e.g. `bookings.availability.weekday.monday`). */
readonly dayKey: string;
readonly windows: readonly AvailabilityRuleVM[];
}
export interface AvailabilityData {
readonly rulesByWeekday: readonly AvailabilityWeekdayGroup[];
readonly exceptions: readonly AvailabilityExceptionVM[];
/** Timezone the editor pre-selects: first persisted rule/exception, else fallback. */
readonly defaultTimezone: string;
readonly emptyHeadlineKey: string;
readonly emptyBodyKey: string;
}
export type AvailabilityVM = VMState<AvailabilityData>;
export interface BuildAvailabilityInput {
readonly rules: readonly AvailabilityRuleVM[];
readonly exceptions: readonly AvailabilityExceptionVM[];
readonly fallbackTimezone: string;
}
export function availabilityWeekdayKey(day: AvailabilityWeekday): string {
return `bookings.availability.weekday.${day}`;
}
export function buildAvailabilityVM(input: BuildAvailabilityInput): AvailabilityVM {
const rulesByWeekday = AVAILABILITY_WEEKDAYS.map((day) => ({
day,
dayKey: availabilityWeekdayKey(day),
windows: input.rules
.filter((rule) => rule.dayOfWeek === day)
.slice()
.sort((a, b) => a.startTime.localeCompare(b.startTime)),
}));
const defaultTimezone = input.rules[0]?.timezone ?? input.exceptions[0]?.timezone ?? input.fallbackTimezone;
return {
status: "ready",
data: {
rulesByWeekday,
exceptions: input.exceptions,
defaultTimezone,
emptyHeadlineKey: "bookings.availability.empty.headline",
emptyBodyKey: "bookings.availability.empty.body",
},
};
}
// --- form payload codecs (editor <-> action) ---
export interface AvailabilityRulePayload {
readonly dayOfWeek: AvailabilityWeekday;
readonly startTime: string;
readonly endTime: string;
readonly timezone: string;
}
export interface AvailabilityExceptionPayload {
readonly date: string;
readonly kind: AvailabilityExceptionKindVM;
readonly startTime: string | null;
readonly endTime: string | null;
readonly reason: string | null;
readonly timezone: string;
}
export interface AvailabilityPayload {
readonly rules: readonly AvailabilityRulePayload[];
readonly exceptions: readonly AvailabilityExceptionPayload[];
}
export type ParsedAvailabilityPayload = { readonly ok: true; readonly payload: AvailabilityPayload } | { readonly ok: false; readonly errorKey: string };
export function serializeAvailabilityPayload(payload: AvailabilityPayload): string {
return JSON.stringify(payload);
}
const WEEKDAY_SET: ReadonlySet<string> = new Set(AVAILABILITY_WEEKDAYS);
function isNonEmptyString(value: unknown): value is string {
return typeof value === "string" && value.length > 0;
}
function parseRule(value: unknown): AvailabilityRulePayload | null {
if (typeof value !== "object" || value === null) {
return null;
}
const rule = value as Record<string, unknown>;
if (!isNonEmptyString(rule.dayOfWeek) || !WEEKDAY_SET.has(rule.dayOfWeek)) {
return null;
}
if (!isNonEmptyString(rule.startTime) || !isNonEmptyString(rule.endTime) || !isNonEmptyString(rule.timezone)) {
return null;
}
return {
dayOfWeek: rule.dayOfWeek as AvailabilityWeekday,
startTime: rule.startTime,
endTime: rule.endTime,
timezone: rule.timezone,
};
}
function parseException(value: unknown): AvailabilityExceptionPayload | null {
if (typeof value !== "object" || value === null) {
return null;
}
const exception = value as Record<string, unknown>;
if (!isNonEmptyString(exception.date) || !isNonEmptyString(exception.timezone)) {
return null;
}
if (exception.kind !== "unavailable" && exception.kind !== "custom_window") {
return null;
}
const startTime = exception.startTime;
const endTime = exception.endTime;
const reason = exception.reason;
if (startTime !== null && startTime !== undefined && typeof startTime !== "string") {
return null;
}
if (endTime !== null && endTime !== undefined && typeof endTime !== "string") {
return null;
}
if (reason !== null && reason !== undefined && typeof reason !== "string") {
return null;
}
return {
date: exception.date,
kind: exception.kind,
startTime: (startTime as string | undefined) ?? null,
endTime: (endTime as string | undefined) ?? null,
reason: (reason as string | undefined) ?? null,
timezone: exception.timezone,
};
}
/** Shape-validate the submitted editor payload; deep validation stays server-side. */
export function parseAvailabilityPayload(json: string): ParsedAvailabilityPayload {
let raw: unknown;
try {
raw = JSON.parse(json);
} catch {
return { ok: false, errorKey: "bookings.availability.error.invalidPayload" };
}
if (typeof raw !== "object" || raw === null) {
return { ok: false, errorKey: "bookings.availability.error.invalidPayload" };
}
const { rules, exceptions } = raw as Record<string, unknown>;
if (!Array.isArray(rules) || !Array.isArray(exceptions)) {
return { ok: false, errorKey: "bookings.availability.error.invalidPayload" };
}
const parsedRules: AvailabilityRulePayload[] = [];
for (const rule of rules) {
const parsed = parseRule(rule);
if (!parsed) {
return { ok: false, errorKey: "bookings.availability.error.invalidPayload" };
}
parsedRules.push(parsed);
}
const parsedExceptions: AvailabilityExceptionPayload[] = [];
for (const exception of exceptions) {
const parsed = parseException(exception);
if (!parsed) {
return { ok: false, errorKey: "bookings.availability.error.invalidPayload" };
}
parsedExceptions.push(parsed);
}
return { ok: true, payload: { rules: parsedRules, exceptions: parsedExceptions } };
}