import { NextApiRequest, NextApiResponse } from 'next' import { BOOKING_STATUS } from '../../../../db/enums' import { patchBooking } from '../../../../db/index' export default async function userHandler( req: NextApiRequest, res: NextApiResponse ): Promise { const { method, query: { uuid: uuids }, } = req const uuid = Array.isArray(uuids) ? uuids[0] : uuids switch (method) { case 'PATCH': 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 } try { const booking = await patchBooking(uuid, req.body) res.status(200).json(booking) } catch (error) { console.error('failed patch booking', error) res.status(400).end(`Failed to save booking: ${error.message}`) } break default: res.setHeader('Allow', ['PATCH']) res.status(405).end(`Method ${method} Not Allowed`) } }