145 lines
4.7 KiB
TypeScript
145 lines
4.7 KiB
TypeScript
import { z } from "zod";
|
|
|
|
const daySchema = z.enum([
|
|
"monday",
|
|
"tuesday",
|
|
"wednesday",
|
|
"thursday",
|
|
"friday",
|
|
"saturday",
|
|
"sunday",
|
|
]);
|
|
|
|
export const adminBookingSettingsVMSchema = z.object({
|
|
meetingTypes: z.array(z.object({
|
|
id: z.string(),
|
|
name: z.string(),
|
|
slug: z.string(),
|
|
durationMinutes: z.number().int(),
|
|
active: z.boolean(),
|
|
hostAdminUserIds: z.array(z.string()),
|
|
})),
|
|
pages: z.array(z.object({
|
|
id: z.string(),
|
|
meetingTypeId: z.string(),
|
|
title: z.string(),
|
|
slug: z.string(),
|
|
published: z.boolean(),
|
|
})),
|
|
hosts: z.array(z.object({
|
|
id: z.string(),
|
|
label: z.string(),
|
|
})),
|
|
availability: z.object({
|
|
timeZone: z.string(),
|
|
rules: z.array(z.object({
|
|
dayOfWeek: daySchema,
|
|
startMinutes: z.number().int(),
|
|
endMinutes: z.number().int(),
|
|
})),
|
|
overrides: z.array(z.object({
|
|
date: z.string(),
|
|
kind: z.enum(["unavailable", "custom_window"]),
|
|
startMinutes: z.number().int().nullable(),
|
|
endMinutes: z.number().int().nullable(),
|
|
reason: z.string().nullable(),
|
|
})),
|
|
}),
|
|
});
|
|
|
|
export type AdminBookingSettingsVM = z.infer<
|
|
typeof adminBookingSettingsVMSchema
|
|
>;
|
|
|
|
export const adminBookingSettingsActionSchema = z.discriminatedUnion(
|
|
"intent",
|
|
[
|
|
z.object({
|
|
intent: z.literal("save-meeting-type"),
|
|
id: z.string().nullable().optional(),
|
|
name: z.string().min(1).max(120),
|
|
slug: z.string().min(1).max(120),
|
|
description: z.string().max(2000).nullable().optional(),
|
|
durationMinutes: z.number().int().positive().max(480),
|
|
bufferBeforeMinutes: z.number().int().nonnegative().max(240),
|
|
bufferAfterMinutes: z.number().int().nonnegative().max(240),
|
|
slotIncrementMinutes: z.number().int().positive().max(240),
|
|
minimumNoticeMinutes: z.number().int().nonnegative().max(43200),
|
|
bookingHorizonDays: z.number().int().positive().max(365),
|
|
hostAdminUserIds: z.array(z.string().min(1)).min(1),
|
|
reminderOffsetsMinutes: z.array(z.number().int().positive()),
|
|
active: z.boolean(),
|
|
}),
|
|
z.object({
|
|
intent: z.literal("save-booking-page"),
|
|
id: z.string().nullable().optional(),
|
|
meetingTypeId: z.string().min(1),
|
|
slug: z.string().min(1).max(120),
|
|
title: z.string().min(1).max(120),
|
|
description: z.string().max(2000).nullable().optional(),
|
|
published: z.boolean(),
|
|
maxBookingsPerEmailPerDay: z.number().int().positive().max(100),
|
|
}),
|
|
z.object({
|
|
intent: z.literal("replace-availability"),
|
|
timeZone: z.string().min(1).max(100),
|
|
rules: z.array(z.object({
|
|
dayOfWeek: daySchema,
|
|
startMinutes: z.number().int().nonnegative().max(1439),
|
|
endMinutes: z.number().int().positive().max(1440),
|
|
})),
|
|
overrides: z.array(z.object({
|
|
date: z.string().date(),
|
|
kind: z.enum(["unavailable", "custom_window"]),
|
|
startMinutes: z.number().int().nonnegative().max(1439).nullable().optional(),
|
|
endMinutes: z.number().int().positive().max(1440).nullable().optional(),
|
|
reason: z.string().max(500).nullable().optional(),
|
|
})),
|
|
}),
|
|
]
|
|
);
|
|
|
|
export function buildAdminBookingSettingsVM(input: {
|
|
readonly meetingTypes: readonly {
|
|
readonly id: string; readonly name: string; readonly slug: string;
|
|
readonly durationMinutes: number; readonly active: boolean;
|
|
readonly hostAdminUserIds: readonly string[];
|
|
}[];
|
|
readonly pages: readonly {
|
|
readonly id: string; readonly meetingTypeId: string;
|
|
readonly title: string; readonly slug: string; readonly published: boolean;
|
|
}[];
|
|
readonly hosts: readonly {
|
|
readonly id: string; readonly firstName: string; readonly lastName: string; readonly email: string;
|
|
}[];
|
|
readonly rules: readonly {
|
|
readonly dayOfWeek: z.infer<typeof daySchema>;
|
|
readonly startMinutes: number; readonly endMinutes: number; readonly timeZone: string;
|
|
}[];
|
|
readonly overrides: readonly {
|
|
readonly date: Date; readonly kind: "unavailable" | "custom_window";
|
|
readonly startMinutes: number | null; readonly endMinutes: number | null;
|
|
readonly reason: string | null; readonly timeZone: string;
|
|
}[];
|
|
}): AdminBookingSettingsVM {
|
|
return adminBookingSettingsVMSchema.parse({
|
|
meetingTypes: input.meetingTypes,
|
|
pages: input.pages,
|
|
hosts: input.hosts.map((host) => ({
|
|
id: host.id,
|
|
label: `${host.firstName} ${host.lastName}`.trim() || host.email,
|
|
})),
|
|
availability: {
|
|
timeZone:
|
|
input.rules[0]?.timeZone ??
|
|
input.overrides[0]?.timeZone ??
|
|
"Europe/Berlin",
|
|
rules: input.rules,
|
|
overrides: input.overrides.map((override) => ({
|
|
...override,
|
|
date: override.date.toISOString().slice(0, 10),
|
|
})),
|
|
},
|
|
});
|
|
}
|