fix: all announcements and calendars

This commit is contained in:
Benno Tielen 2026-04-17 10:32:50 +02:00
parent ed4ce47ee4
commit b8ed9ea538
3 changed files with 51 additions and 83 deletions

View file

@ -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,
)
}

View file

@ -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,
)
}

View file

@ -1,37 +1,27 @@
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<Announcement | Calendar>) => {
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"
href: doc.document.url || 'undefined',
})
}
}
return links