From b8ed9ea5387c0c48a1b84eea5696a0a58d46c9c2 Mon Sep 17 00:00:00 2001 From: Benno Tielen Date: Fri, 17 Apr 2026 10:32:50 +0200 Subject: [PATCH] fix: all announcements and calendars --- src/fetch/announcement.ts | 45 ++++++++++++++------------------------ src/fetch/calendar.ts | 45 ++++++++++++++------------------------ src/utils/dto/perParish.ts | 44 ++++++++++++++----------------------- 3 files changed, 51 insertions(+), 83 deletions(-) diff --git a/src/fetch/announcement.ts b/src/fetch/announcement.ts index 6b2eb6e..14680d3 100644 --- a/src/fetch/announcement.ts +++ b/src/fetch/announcement.ts @@ -1,6 +1,6 @@ import { getPayload } from 'payload' import config from '@/payload.config' -import { Announcement } from '@/payload-types' +import { Announcement, Parish } from '@/payload-types' /** * Fetch last announcement for a parish @@ -43,33 +43,22 @@ export const fetchLastAnnouncement = async ( } /** - * Fetch the last few announcements + * Fetch the last announcement for each parish */ -export const fetchLastAnnouncements = async () => { - const date = new Date() - date.setDate(date.getDate() - 14) - const tomorrow = new Date() - tomorrow.setDate(tomorrow.getDate() + 1) - tomorrow.setHours(23, 59, 59, 59) - +export const fetchLastAnnouncements = async (): Promise< + Array<{ parish: Parish; doc: Announcement }> +> => { const payload = await getPayload({ config }) - return payload.find({ - collection: 'announcement', - sort: '-date', - where: { - and: [ - { - date: { - greater_than_equal: date.toISOString(), - }, - }, - { - date: { - less_than_equal: tomorrow.toISOString(), - }, - }, - ], - }, - limit: 3, - }) + const parishes = await payload.find({ collection: 'parish', limit: 100 }) + + const results = await Promise.all( + parishes.docs.map(async (parish) => { + const doc = await fetchLastAnnouncement(parish.id) + return doc ? { parish, doc } : null + }), + ) + + return results.filter( + (r): r is { parish: Parish; doc: Announcement } => !!r, + ) } diff --git a/src/fetch/calendar.ts b/src/fetch/calendar.ts index 9354520..7686415 100644 --- a/src/fetch/calendar.ts +++ b/src/fetch/calendar.ts @@ -1,6 +1,6 @@ import { getPayload } from 'payload' import config from '@/payload.config' -import { Calendar } from '@/payload-types' +import { Calendar, Parish } from '@/payload-types' /** * Fetch last calendar for a parish @@ -43,33 +43,22 @@ export const fetchLastCalendar = async ( } /** - * Fetch last calendars + * Fetch the last calendar for each parish */ -export const fetchLastCalendars = async () => { - const date = new Date() - date.setDate(date.getDate() - 14) - const tomorrow = new Date() - tomorrow.setDate(tomorrow.getDate() + 1) - tomorrow.setHours(23, 59, 59, 59) - +export const fetchLastCalendars = async (): Promise< + Array<{ parish: Parish; doc: Calendar }> +> => { const payload = await getPayload({ config }) - return payload.find({ - collection: 'calendar', - sort: '-date', - where: { - and: [ - { - date: { - greater_than_equal: date.toISOString(), - }, - }, - { - date: { - less_than_equal: tomorrow.toISOString(), - }, - }, - ], - }, - limit: 3, - }) + const parishes = await payload.find({ collection: 'parish', limit: 100 }) + + const results = await Promise.all( + parishes.docs.map(async (parish) => { + const doc = await fetchLastCalendar(parish.id) + return doc ? { parish, doc } : null + }), + ) + + return results.filter( + (r): r is { parish: Parish; doc: Calendar } => !!r, + ) } diff --git a/src/utils/dto/perParish.ts b/src/utils/dto/perParish.ts index dbec01c..ec211f6 100644 --- a/src/utils/dto/perParish.ts +++ b/src/utils/dto/perParish.ts @@ -1,38 +1,28 @@ -import { PaginatedDocs } from 'payload' -import { Announcement, Calendar } from '@/payload-types' +import { Announcement, Calendar, Parish } from '@/payload-types' type AnnouncementLink = { - id: string, - text: string, + id: string + text: string href: string } -export const perParish = (data: PaginatedDocs) => { - let links: AnnouncementLink[] = [] - let set = new Set(); +type PerParishEntry = { + parish: Parish + doc: Announcement | Calendar +} - for (const announcement of data.docs) { - for (const parish of announcement.parish) { - if (typeof announcement.document === "string") - continue; +export const perParish = (data: PerParishEntry[]) => { + const links: AnnouncementLink[] = [] - if (typeof parish === 'string') { - continue; - } + for (const { parish, doc } of data) { + if (typeof doc.document === 'string') continue - if (set.has(parish.id)) { - continue; - } - - set.add(parish.id) - links.push({ - id: parish.id, - text: parish.name, - href: announcement.document.url || "undefined" - }) - - } + links.push({ + id: parish.id, + text: parish.name, + href: doc.document.url || 'undefined', + }) } return links -} \ No newline at end of file +}