49 lines
No EOL
1.1 KiB
TypeScript
49 lines
No EOL
1.1 KiB
TypeScript
import { stringify } from 'qs-esm'
|
|
import { PaginatedDocs } from 'payload'
|
|
import { Calendar } from '@/payload-types'
|
|
|
|
/**
|
|
* Fetch last calendar for a parish
|
|
*/
|
|
export const fetchLastCalendar = async (parishId: string): Promise<Calendar | undefined> => {
|
|
const date = new Date();
|
|
date.setDate(date.getDate() - 14);
|
|
const tomorrow = new Date();
|
|
tomorrow.setDate(tomorrow.getDate() + 1);
|
|
tomorrow.setHours(23,59,59,59);
|
|
|
|
const query: any = {
|
|
and: [
|
|
{
|
|
parish: {
|
|
equals: parishId
|
|
}
|
|
},
|
|
{
|
|
date: {
|
|
greater_than_equal: date.toISOString(),
|
|
}
|
|
},
|
|
{
|
|
date: {
|
|
less_than_equal: tomorrow.toISOString()
|
|
}
|
|
}
|
|
]
|
|
|
|
}
|
|
|
|
const stringifiedQuery = stringify(
|
|
{
|
|
sort: "-date",
|
|
where: query,
|
|
limit: 1,
|
|
},
|
|
{ addQueryPrefix: true },
|
|
)
|
|
|
|
const response = await fetch(`http://localhost:3000/api/calendar${stringifiedQuery}`)
|
|
if (!response.ok) return undefined
|
|
const announcements = await response.json() as PaginatedDocs<Calendar>
|
|
return announcements.docs[0]
|
|
} |