Files
pfadi-bussle/lib/getServerSideProps.ts
2022-03-01 23:41:08 +01:00

58 lines
1.3 KiB
TypeScript

import { startOfYear } from 'date-fns'
import { nowInTz } from '../helpers/date'
import { getBookingByUUID, getBookings } from '../db/index'
export type ServerSideBooking = {
props: {
booking: object
}
}
export type ServerSideRecentBooking = {
props: {
bookings: object[]
}
}
export const getServerSideRecentBookings =
async (): Promise<ServerSideRecentBooking> => {
const bookings = await getBookings({
startDateGreaterThan: startOfYear(nowInTz()).toISOString(),
})
// TODO: hack, not sure why _id is not serilizable
const bookingsJSON = JSON.parse(
JSON.stringify(bookings.map((b) => b.toJSON()))
) as object[]
return {
props: {
bookings: bookingsJSON,
},
}
}
export const getServerSideBooking = async (
context: any
): Promise<ServerSideBooking> => {
const {
res,
params: { uuid: uuids },
} = context
const uuid = Array.isArray(uuids) ? uuids[0] : uuids
const booking = await getBookingByUUID(uuid)
if (!booking) {
res.statusCode = 404
res.end()
return { props: { booking: null } }
}
await booking.populate('bill').execPopulate()
// TODO: hack, not sure why _id is not serilizable
const bookingJSON = JSON.parse(JSON.stringify(booking.toJSON())) as object
return {
props: { booking: bookingJSON },
}
}