fix: update event fetching logic

This commit is contained in:
Benno Tielen 2026-07-16 09:39:52 +02:00
parent 0703704307
commit 36ecb86fcc
2 changed files with 10 additions and 82 deletions

View file

@ -43,7 +43,15 @@ const getUpcomingOccurrences = unstable_cache(
const query: any = {
and: [
// 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' } },
],
}

View file

@ -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<PaginatedDocs<Event>> {
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<PaginatedDocs<Event>>
}
/**
* Fetch a single event by ID
*/