import type { Payload } from "payload"; import { WEEKDAYS } from "@/collections/Availability"; export type TimeSlot = { start: Date; end: Date }; const WEEKDAY_BY_INDEX = [ "sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday", ] as const; function dateKey(date: Date): string { return date.toISOString().slice(0, 10); } function timeOfDayMinutes(date: Date): number { return date.getHours() * 60 + date.getMinutes(); } function atMinutesOfDay(day: Date, minutes: number): Date { const d = new Date(day); d.setHours(0, Math.round(minutes), 0, 0); return d; } function overlaps(aStart: Date, aEnd: Date, bStart: Date, bEnd: Date): boolean { return aStart < bEnd && bStart < aEnd; } /** * Computes bookable slots for one offer across a date range, from the * configured weekly Availability (plus per-date AvailabilityOverrides), * minus anything already pending/confirmed on Anna's schedule — she can * only do one thing at a time, regardless of which offer it's for. */ export async function getAvailableSlots( payload: Payload, args: { durationMinutes: number; from: Date; to: Date }, ): Promise> { const { durationMinutes, from, to } = args; const result = new Map(); const [{ docs: weekly }, { docs: overrides }, { docs: busyBookings }] = await Promise.all([ payload.find({ collection: "availability", where: { active: { equals: true } }, limit: 100 }), payload.find({ collection: "availability-overrides", where: { and: [{ date: { greater_than_equal: from.toISOString() } }, { date: { less_than_equal: to.toISOString() } }] }, limit: 200, }), payload.find({ collection: "booking-requests", where: { and: [ { status: { in: ["pending", "confirmed"] } }, { date: { greater_than_equal: from.toISOString() } }, { date: { less_than_equal: to.toISOString() } }, ], }, limit: 500, }), ]); const overridesByDate = new Map(overrides.map((o) => [dateKey(new Date(o.date)), o])); const now = new Date(); for (let day = new Date(from); day <= to; day = new Date(day.getTime() + 24 * 60 * 60 * 1000)) { const key = dateKey(day); const override = overridesByDate.get(key); let windows: { startMinutes: number; endMinutes: number }[] = []; if (override?.type === "unavailable") { windows = []; } else if (override?.type === "custom-hours" && override.startTime && override.endTime) { windows = [ { startMinutes: timeOfDayMinutes(new Date(override.startTime)), endMinutes: timeOfDayMinutes(new Date(override.endTime)), }, ]; } else { const weekdayName = WEEKDAY_BY_INDEX[day.getDay()]; windows = weekly .filter((w) => w.weekday === weekdayName) .map((w) => ({ startMinutes: timeOfDayMinutes(new Date(w.startTime)), endMinutes: timeOfDayMinutes(new Date(w.endTime)), })); } const dayBusy = busyBookings .filter((b) => dateKey(new Date(b.date)) === key) .map((b) => ({ start: new Date(b.startTime), end: new Date(b.endTime) })); const slots: TimeSlot[] = []; for (const window of windows) { for (let start = window.startMinutes; start + durationMinutes <= window.endMinutes; start += durationMinutes) { const slotStart = atMinutesOfDay(day, start); const slotEnd = atMinutesOfDay(day, start + durationMinutes); if (slotStart < now) continue; if (dayBusy.some((b) => overlaps(slotStart, slotEnd, b.start, b.end))) continue; slots.push({ start: slotStart, end: slotEnd }); } } if (slots.length > 0) result.set(key, slots); } return result; } export { WEEKDAYS };