mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
58 lines
1.5 KiB
TypeScript
58 lines
1.5 KiB
TypeScript
import { BookingDocument } from '../../../../../db/booking'
|
|
import { getBookingByUUID } from '../../../../../db/index'
|
|
import withSession, { isAdminSession } from '../../../../../lib/session'
|
|
import {
|
|
sendBookingConfirmed,
|
|
sendBookingRejected,
|
|
} from '../../../../../helpers/mail'
|
|
import { BOOKING_STATUS } from '../../../../../db/enums'
|
|
|
|
export default withSession(async function bookingHandler(req, res): Promise<void> {
|
|
if (!isAdminSession(req)) {
|
|
res.status(403).send({ message: 'Not Authorized' })
|
|
return
|
|
}
|
|
|
|
const {
|
|
method,
|
|
query: { uuid: uuids },
|
|
} = req
|
|
|
|
const uuid = Array.isArray(uuids) ? uuids[0] : uuids
|
|
|
|
let booking: BookingDocument
|
|
|
|
switch (method) {
|
|
case 'PATCH':
|
|
booking = await getBookingByUUID(uuid)
|
|
|
|
// FIXME: validate all the things
|
|
booking.set(req.body)
|
|
|
|
const bookingStatusChanged = booking.isModified('status')
|
|
|
|
await booking.save()
|
|
|
|
await booking.populate('booker').execPopulate()
|
|
|
|
res.status(200).json(booking.toJSON())
|
|
|
|
if (!bookingStatusChanged) {
|
|
return
|
|
}
|
|
|
|
if (booking.status === BOOKING_STATUS.CONFIRMED) {
|
|
sendBookingConfirmed(booking)
|
|
console.log(`Booking ${booking.uuid} confirm sent`)
|
|
} else if (booking.status === BOOKING_STATUS.REJECTED) {
|
|
sendBookingRejected(booking)
|
|
console.log(`Booking ${booking.uuid} rejected sent`)
|
|
}
|
|
|
|
break
|
|
default:
|
|
res.setHeader('Allow', ['PATCH'])
|
|
res.status(405).end(`Method ${method} Not Allowed`)
|
|
}
|
|
})
|