Files
pfadi-bussle/pages/api/booking/[uuid].js
2020-08-25 23:56:41 +02:00

54 lines
1.3 KiB
JavaScript

import { getBookingByUUID, getBookingByUUIDAsJSON } from '../../../db/index'
import { BOOKING_STATUS } from '../../../db/bookingStatus'
export default async function userHandler(req, res) {
const {
method,
query: { uuid },
} = req
let booking
switch (method) {
case 'GET':
booking = await getBookingByUUIDAsJSON(uuid)
res.status(200).json(booking)
break
case 'PATCH':
booking = await getBookingByUUID(uuid)
const readonlyProps = Object.keys(req.body).filter(
(key) => key !== 'status'
)
if (readonlyProps.length) {
res
.status(400)
.end(
`The following attributes cannot be changed: ${readonlyProps.join(
', '
)}`
)
break
}
if (!Object.values(BOOKING_STATUS).includes(req.body.status)) {
res
.status(400)
.end(
`The attribute status can only be: ${Object.values(
BOOKING_STATUS
).join(', ')}`
)
break
}
booking.status = req.body.status
await booking.save()
res.status(200).json(booking.toJSON())
break
default:
res.setHeader('Allow', ['POST'])
res.status(405).end(`Method ${method} Not Allowed`)
}
}