import { cache } from "react"; import { getCMS } from "@/lib/payload/getPayload"; import type { Event, Offer, Post } from "@/payload-types"; // Wrapped in React's `cache()` so multiple components reading the same data // during one request (e.g. layout + page) only hit the database once. // // IMPORTANT: Payload's Local API defaults `overrideAccess` to `true`, which // SKIPS each collection's `access.read` check entirely (see // node_modules/payload/dist/collections/operations/find.js) — it does not // automatically hide drafts. Every fetcher that feeds a *public* page must // therefore pass `overrideAccess: false` so the collection's own // `publishedOrAdmin` access rule actually filters out unpublished documents. // ---- Angebote --------------------------------------------------------- export const getOffers = cache(async (): Promise => { const payload = await getCMS(); const result = await payload.find({ collection: "offers", sort: "order", limit: 100, depth: 1, overrideAccess: false, }); return result.docs; }); export const getOfferBySlug = cache(async (slug: string): Promise => { const payload = await getCMS(); const result = await payload.find({ collection: "offers", where: { slug: { equals: slug } }, limit: 1, depth: 1, overrideAccess: false, }); return result.docs[0] ?? null; }); // ---- Termine (Events) -------------------------------------------------- export const getUpcomingEvents = cache(async (limit = 6): Promise => { const payload = await getCMS(); const today = new Date(); today.setHours(0, 0, 0, 0); const result = await payload.find({ collection: "events", where: { date: { greater_than_equal: today.toISOString() } }, sort: "date", limit, depth: 1, overrideAccess: false, }); return result.docs; }); export const getAllEvents = cache(async (): Promise => { const payload = await getCMS(); const result = await payload.find({ collection: "events", sort: "-date", limit: 200, depth: 1, overrideAccess: false, }); return result.docs; }); export const getEventBySlug = cache(async (slug: string): Promise => { const payload = await getCMS(); const result = await payload.find({ collection: "events", where: { slug: { equals: slug } }, limit: 1, depth: 1, overrideAccess: false, }); return result.docs[0] ?? null; }); /** * Unlike `getEventBySlug`, this intentionally does NOT filter by publish * status. Confirmed private bookings create an unpublished/private linked * Event (see collections/BookingRequests.ts) that must still be reachable * for the one customer who received its join link + password — "unlisted", * not "hidden". Only used by the join/beitreten flow, never for listings. */ export const getEventForJoin = cache(async (slug: string): Promise => { const payload = await getCMS(); const result = await payload.find({ collection: "events", where: { slug: { equals: slug } }, limit: 1, depth: 1, }); return result.docs[0] ?? null; }); // ---- Aktuelles (Posts) -------------------------------------------------- export const getPosts = cache(async (limit = 50): Promise => { const payload = await getCMS(); const result = await payload.find({ collection: "posts", sort: "-publishDate", limit, depth: 1, overrideAccess: false, }); return result.docs; }); export const getPostBySlug = cache(async (slug: string): Promise => { const payload = await getCMS(); const result = await payload.find({ collection: "posts", where: { slug: { equals: slug } }, limit: 1, depth: 1, overrideAccess: false, }); return result.docs[0] ?? null; });