Files
pfadi-bussle/lib/getServerSideProps.ts
2022-10-11 12:02:06 +02:00

57 lines
1.1 KiB
TypeScript

import { Booking } from '@prisma/client';
import { startOfYear } from 'date-fns'
import { nowInTz } from '../helpers/date'
import { getBookingByUUID, getBookings } from '../db/index'
export type ServerSideBooking = {
props: {
booking: Booking
}
}
export type ServerSideRecentBooking = {
props: {
bookings: Booking[]
}
}
export const getServerSideRecentBookings =
async () => {
const bookings = await getBookings({
startDateGreaterThan: startOfYear(nowInTz()).toISOString(),
})
return {
props: {
bookings: bookings,
},
}
}
export const getServerSideBooking = async (
context: any
) => {
const {
res,
params: { uuid: uuids },
} = context
const uuid = Array.isArray(uuids) ? uuids[0] : uuids
const booking = await getBookingByUUID(uuid)
console.log(booking.createdAt);
if (!booking) {
res.statusCode = 404
res.end()
return { props: { booking: null } }
}
return {
props: { booking: {
...booking,
createdAt: booking.createdAt.toISOString(),
updatedAt: booking.updatedAt.toISOString(),
}},
}
}