send mail when booking is confirmed or rejected

This commit is contained in:
Thomas Ruoff
2020-11-03 21:50:12 +01:00
parent acf0366d9b
commit b283ffe476
2 changed files with 151 additions and 67 deletions

View File

@@ -1,6 +1,11 @@
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) {
if (!isAdminSession(req, res)) {
@@ -22,11 +27,25 @@ export default withSession(async function bookingHandler(req, res) {
// 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)
} else if (booking.status === BOOKING_STATUS.REJECTED) {
sendBookingRejected(booking)
}
break
default:
res.setHeader('Allow', ['PATCH'])