mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 22:47:15 +01:00
It also brings the problem of consolidating bookers over multiple bookings. The amount of data is not justifying having it in an own entity
59 lines
1.3 KiB
TypeScript
59 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 },
|
|
}
|
|
}
|