import type { Payload } from "payload"; import { EVENT_CATEGORIES } from "@/collections/Events"; import type { ICSEvent } from "./ics"; import type { BookingRequest, Customer, Event, Offer } from "@/payload-types"; // A rolling window rather than "everything ever": calendar apps refetch a // subscribed feed periodically on their own, so this just needs to cover a // sensible range each time, not paginate an entire history. const WINDOW_PAST_MS = 30 * 24 * 60 * 60 * 1000; const WINDOW_FUTURE_MS = 400 * 24 * 60 * 60 * 1000; /** * Builds the calendar entries for Anna's personal ICS feed: confirmed * bookings (never pending ones) plus standalone public events — onsite and * online alike. Deliberately never includes the meeting password anywhere * in the output, only a link to the protected join page. * * Local API is used with the default overrideAccess: true because this is * only ever reached after the caller already verified the feed's secret * token (see app/(frontend)/calendar/[tokenFile]/route.ts) — equivalent * trust to the existing admin calendar view. */ export async function getAdminFeedICSEvents(payload: Payload): Promise { const from = new Date(Date.now() - WINDOW_PAST_MS); const to = new Date(Date.now() + WINDOW_FUTURE_MS); const serverUrl = process.env.NEXT_PUBLIC_SERVER_URL || "http://localhost:3000"; const domainHost = new URL(serverUrl).host; const [{ docs: bookings }, { docs: events }, settings] = await Promise.all([ payload.find({ collection: "booking-requests", where: { and: [ { status: { equals: "confirmed" } }, { date: { greater_than_equal: from.toISOString() } }, { date: { less_than_equal: to.toISOString() } }, ], }, depth: 2, limit: 1000, }), payload.find({ collection: "events", where: { and: [ { date: { greater_than_equal: from.toISOString() } }, { date: { less_than_equal: to.toISOString() } }, { isPrivateBooking: { not_equals: true } }, ], }, depth: 0, limit: 1000, }), payload.findGlobal({ slug: "booking-settings" }), ]); const addressParts = [settings.locationName, settings.street, [settings.postalCode, settings.city].filter(Boolean).join(" ")].filter(Boolean); const fullAddress = addressParts.join(", "); const bookingEvents: ICSEvent[] = (bookings as BookingRequest[]).map((b) => { const offer = typeof b.offer === "object" ? (b.offer as Offer) : null; const customer = typeof b.user === "object" ? (b.user as Customer) : null; const isOnline = b.appointmentType === "online"; const description = [ customer?.name ? `Mit: ${customer.name}` : null, isOnline ? "Online-Termin" : "Vor-Ort-Termin", b.userMessage ? `Nachricht: ${b.userMessage}` : null, ] .filter(Boolean) .join("\n"); let url: string | undefined; if (isOnline && b.linkedEvent && typeof b.linkedEvent === "object") { url = `${serverUrl}/termine/${(b.linkedEvent as Event).slug}/beitreten`; } else if (!isOnline && fullAddress) { url = `https://www.google.com/maps/search/?api=1&query=${encodeURIComponent(fullAddress)}`; } return { uid: `booking-${b.id}@${domainHost}`, start: new Date(b.startTime), end: new Date(b.endTime), summary: offer?.title ?? "Termin", description: description || undefined, // Vor Ort: vollständige Adresse. Online: literal "Online" — never the // meeting password, which is intentionally not part of this feed. location: isOnline ? "Online" : fullAddress || undefined, url, }; }); const eventEvents: ICSEvent[] = (events as Event[]).map((e) => { const isOnline = Boolean(e.isOnline); const start = new Date(e.startTime || e.date); const end = new Date(e.endTime || e.startTime || e.date); return { uid: `event-${e.id}@${domainHost}`, start, end, summary: e.title, description: EVENT_CATEGORIES.find((c) => c.value === e.category)?.label, location: isOnline ? "Online" : e.location || fullAddress || undefined, url: e.slug ? `${serverUrl}/termine/${e.slug}` : undefined, }; }); return [...bookingEvents, ...eventEvents]; }