extract getServerSideBooking

This commit is contained in:
Thomas Ruoff
2020-10-28 23:35:31 +01:00
parent e1ebc86ca1
commit 84026c2576
3 changed files with 34 additions and 40 deletions

View File

@@ -7,16 +7,17 @@ import Select from '../../../../components/select'
import { AdditionalCost, BillDocument } from '../../../../db/bill'
import { BookingDocument } from '../../../../db/booking'
import { BILL_STATUS, MILAGE_TARIFS } from '../../../../db/enums'
import { getBookingByUUID, getMilageMax } from '../../../../db/index'
import { getMilageMax } from '../../../../db/index'
import { dateFormatFrontend } from '../../../../helpers/date'
import { getBillTotal } from '../../../../helpers/bill'
import { getBookingStatus } from '../../../../helpers/booking'
import authenticate from '../../../../lib/authenticate'
import withSession from '../../../../lib/session'
import { getServerSideBooking } from '../../../../lib/getServerSideProps'
export const getServerSideProps: GetServerSideProps = withSession(
async ({ req, res, params }) => {
const { uuid: uuids } = params
async (context) => {
const { req, res, params } = context
const authenticatedUser = authenticate(req, res)
if (!authenticatedUser) {
@@ -28,22 +29,11 @@ export const getServerSideProps: GetServerSideProps = withSession(
req.session.set('user', authenticatedUser)
await req.session.save()
const uuid = Array.isArray(uuids) ? uuids[0] : uuids
const booking = await getBookingByUUID(uuid)
if (!booking) {
res.statusCode = 404
res.end()
return { props: {} }
}
const milageMax = await getMilageMax()
await booking.populate('booker').populate('bill').execPopulate()
// TODO: hack, not sure why _id is not serilizable
const bookingJSON = JSON.parse(JSON.stringify(booking.toJSON()))
const result = await getServerSideBooking(context)
return {
props: { booking: bookingJSON, milageMax },
...result,
props: { ...result.props, milageMax },
}
}
)