fix: harden booking and meeting flows

This commit is contained in:
2026-09-17 02:42:27 +02:00
parent cb30ecade9
commit 66710fc65c
42 changed files with 683 additions and 608 deletions
+11 -8
View File
@@ -1,4 +1,4 @@
import type { Payload } from "payload";
import type { Payload, PayloadRequest } from "payload";
import { WEEKDAYS } from "@/collections/Availability";
import { berlinParts, berlinWallTimeToUTC, type CalendarDate } from "@/lib/calendar/timezone";
@@ -63,17 +63,20 @@ function overlaps(aStart: Date, aEnd: Date, bStart: Date, bEnd: Date): boolean {
*/
export async function getAvailableSlots(
payload: Payload,
args: { durationMinutes: number; from: Date; to: Date },
args: { durationMinutes: number; from: Date; to: Date; req?: PayloadRequest },
): Promise<Map<string, TimeSlot[]>> {
const { durationMinutes, from, to } = args;
const { durationMinutes, from, to, req } = args;
const result = new Map<string, TimeSlot[]>();
if (!Number.isInteger(durationMinutes) || durationMinutes <= 0 || durationMinutes > 1440 ||
!Number.isFinite(from.getTime()) || !Number.isFinite(to.getTime()) || to < from ||
to.getTime() - from.getTime() > 63 * 86400000) return result;
const [{ docs: weekly }, { docs: overrides }, { docs: busyBookings }] = await Promise.all([
payload.find({ collection: "availability", where: { active: { equals: true } }, limit: 100 }),
payload.find({ collection: "availability", where: { active: { equals: true } }, pagination: false, req }),
payload.find({
collection: "availability-overrides",
where: { and: [{ date: { greater_than_equal: from.toISOString() } }, { date: { less_than_equal: to.toISOString() } }] },
limit: 200,
pagination: false, req,
}),
payload.find({
collection: "booking-requests",
@@ -84,7 +87,7 @@ export async function getAvailableSlots(
{ date: { less_than_equal: to.toISOString() } },
],
},
limit: 500,
pagination: false, req,
}),
]);
@@ -131,11 +134,11 @@ export async function getAvailableSlots(
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.some((slot) => slot.start.getTime() === slotStart.getTime())) slots.push({ start: slotStart, end: slotEnd });
}
}
if (slots.length > 0) result.set(key, slots);
if (slots.length > 0) result.set(key, slots.sort((a, b) => a.start.getTime() - b.start.getTime()));
}
return result;