fix: all announcements and calendars
This commit is contained in:
parent
ed4ce47ee4
commit
b8ed9ea538
3 changed files with 51 additions and 83 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
import { getPayload } from 'payload'
|
import { getPayload } from 'payload'
|
||||||
import config from '@/payload.config'
|
import config from '@/payload.config'
|
||||||
import { Announcement } from '@/payload-types'
|
import { Announcement, Parish } from '@/payload-types'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch last announcement for a parish
|
* 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 () => {
|
export const fetchLastAnnouncements = async (): Promise<
|
||||||
const date = new Date()
|
Array<{ parish: Parish; doc: Announcement }>
|
||||||
date.setDate(date.getDate() - 14)
|
> => {
|
||||||
const tomorrow = new Date()
|
|
||||||
tomorrow.setDate(tomorrow.getDate() + 1)
|
|
||||||
tomorrow.setHours(23, 59, 59, 59)
|
|
||||||
|
|
||||||
const payload = await getPayload({ config })
|
const payload = await getPayload({ config })
|
||||||
return payload.find({
|
const parishes = await payload.find({ collection: 'parish', limit: 100 })
|
||||||
collection: 'announcement',
|
|
||||||
sort: '-date',
|
const results = await Promise.all(
|
||||||
where: {
|
parishes.docs.map(async (parish) => {
|
||||||
and: [
|
const doc = await fetchLastAnnouncement(parish.id)
|
||||||
{
|
return doc ? { parish, doc } : null
|
||||||
date: {
|
}),
|
||||||
greater_than_equal: date.toISOString(),
|
)
|
||||||
},
|
|
||||||
},
|
return results.filter(
|
||||||
{
|
(r): r is { parish: Parish; doc: Announcement } => !!r,
|
||||||
date: {
|
)
|
||||||
less_than_equal: tomorrow.toISOString(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
limit: 3,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
import { getPayload } from 'payload'
|
import { getPayload } from 'payload'
|
||||||
import config from '@/payload.config'
|
import config from '@/payload.config'
|
||||||
import { Calendar } from '@/payload-types'
|
import { Calendar, Parish } from '@/payload-types'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Fetch last calendar for a parish
|
* 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 () => {
|
export const fetchLastCalendars = async (): Promise<
|
||||||
const date = new Date()
|
Array<{ parish: Parish; doc: Calendar }>
|
||||||
date.setDate(date.getDate() - 14)
|
> => {
|
||||||
const tomorrow = new Date()
|
|
||||||
tomorrow.setDate(tomorrow.getDate() + 1)
|
|
||||||
tomorrow.setHours(23, 59, 59, 59)
|
|
||||||
|
|
||||||
const payload = await getPayload({ config })
|
const payload = await getPayload({ config })
|
||||||
return payload.find({
|
const parishes = await payload.find({ collection: 'parish', limit: 100 })
|
||||||
collection: 'calendar',
|
|
||||||
sort: '-date',
|
const results = await Promise.all(
|
||||||
where: {
|
parishes.docs.map(async (parish) => {
|
||||||
and: [
|
const doc = await fetchLastCalendar(parish.id)
|
||||||
{
|
return doc ? { parish, doc } : null
|
||||||
date: {
|
}),
|
||||||
greater_than_equal: date.toISOString(),
|
)
|
||||||
},
|
|
||||||
},
|
return results.filter(
|
||||||
{
|
(r): r is { parish: Parish; doc: Calendar } => !!r,
|
||||||
date: {
|
)
|
||||||
less_than_equal: tomorrow.toISOString(),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
},
|
|
||||||
limit: 3,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,38 +1,28 @@
|
||||||
import { PaginatedDocs } from 'payload'
|
import { Announcement, Calendar, Parish } from '@/payload-types'
|
||||||
import { Announcement, Calendar } from '@/payload-types'
|
|
||||||
|
|
||||||
type AnnouncementLink = {
|
type AnnouncementLink = {
|
||||||
id: string,
|
id: string
|
||||||
text: string,
|
text: string
|
||||||
href: string
|
href: string
|
||||||
}
|
}
|
||||||
|
|
||||||
export const perParish = (data: PaginatedDocs<Announcement | Calendar>) => {
|
type PerParishEntry = {
|
||||||
let links: AnnouncementLink[] = []
|
parish: Parish
|
||||||
let set = new Set();
|
doc: Announcement | Calendar
|
||||||
|
}
|
||||||
|
|
||||||
for (const announcement of data.docs) {
|
export const perParish = (data: PerParishEntry[]) => {
|
||||||
for (const parish of announcement.parish) {
|
const links: AnnouncementLink[] = []
|
||||||
if (typeof announcement.document === "string")
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (typeof parish === 'string') {
|
for (const { parish, doc } of data) {
|
||||||
continue;
|
if (typeof doc.document === 'string') continue
|
||||||
}
|
|
||||||
|
|
||||||
if (set.has(parish.id)) {
|
links.push({
|
||||||
continue;
|
id: parish.id,
|
||||||
}
|
text: parish.name,
|
||||||
|
href: doc.document.url || 'undefined',
|
||||||
set.add(parish.id)
|
})
|
||||||
links.push({
|
|
||||||
id: parish.id,
|
|
||||||
text: parish.name,
|
|
||||||
href: announcement.document.url || "undefined"
|
|
||||||
})
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return links
|
return links
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue