From 36ecb86fccd0cc547466b80dc44a79e82bba4111 Mon Sep 17 00:00:00 2001 From: Benno Tielen Date: Thu, 16 Jul 2026 09:39:52 +0200 Subject: [PATCH] fix: update event fetching logic --- src/fetch/eventOccurrences.ts | 10 ++++- src/fetch/events.ts | 82 +---------------------------------- 2 files changed, 10 insertions(+), 82 deletions(-) diff --git a/src/fetch/eventOccurrences.ts b/src/fetch/eventOccurrences.ts index c536189..89cef8b 100644 --- a/src/fetch/eventOccurrences.ts +++ b/src/fetch/eventOccurrences.ts @@ -43,7 +43,15 @@ const getUpcomingOccurrences = unstable_cache( const query: any = { and: [ - { date: { greater_than_equal: fromDate } }, + // Overlap semantics: an occurrence stays "upcoming" while it is still + // running, not only before it starts. endDateTime is null for + // occurrences without an end; those fall back to the start date. + { + or: [ + { date: { greater_than_equal: fromDate } }, + { endDateTime: { greater_than_equal: fromDate } }, + ], + }, { 'event._status': { equals: 'published' } }, ], } diff --git a/src/fetch/events.ts b/src/fetch/events.ts index 6129b53..713b112 100644 --- a/src/fetch/events.ts +++ b/src/fetch/events.ts @@ -1,87 +1,7 @@ -import { getPayload, PaginatedDocs } from 'payload' +import { getPayload } from 'payload' import config from '@/payload.config' import { Event } from '@/payload-types' -type Args = { - parishId?: string - groupId?: string - limit?: number - page?: number - fromDate?: Date - toDate?: Date -} - -/** - * Fetch a list of events - */ -export async function fetchEvents( - args?: Args, -): Promise> { - const { - parishId, - groupId, - limit = 30, - page = 0, - fromDate = new Date(), - toDate, - } = args || {} - - const query: any = { - and: [ - { - '_status': { - equals: 'published', - } - }, - { - date: { - greater_than_equal: fromDate.toISOString(), - }, - }, - ], - } - - if (toDate) { - query.and.push({ - date: { - less_than: toDate.toISOString(), - }, - }) - } - - if (parishId) { - query.and.push({ - parish: { - equals: parishId, - }, - }) - } - - if (groupId) { - query.and.push({ - group: { - equals: groupId, - }, - }) - } - - const payload = await getPayload({ config }) - return payload.find({ - collection: 'event', - sort: 'date', - where: query, - select: { - location: true, - date: true, - title: true, - cancelled: true, - }, - depth: 1, - limit, - page, - }) as Promise> -} - /** * Fetch a single event by ID */