mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
fix sending reject and confirmation mails
This commit is contained in:
@@ -1,6 +1,30 @@
|
||||
import { NextApiRequest, NextApiResponse } from 'next'
|
||||
import Book from '../../../../components/book'
|
||||
import { Booking } from '../../../../db/booking'
|
||||
import { BOOKING_STATUS } from '../../../../db/enums'
|
||||
import { patchBooking } from '../../../../db/index'
|
||||
import {
|
||||
sendBookingConfirmed,
|
||||
sendBookingRejected,
|
||||
} from '../../../../helpers/mail'
|
||||
|
||||
function changedStatus(
|
||||
booking: Booking,
|
||||
updates: Partial<Booking>,
|
||||
status: BOOKING_STATUS
|
||||
): boolean {
|
||||
return (
|
||||
[BOOKING_STATUS.REQUESTED].includes(booking.status) &&
|
||||
updates.status === status
|
||||
)
|
||||
}
|
||||
function wasRejected(booking: Booking, updates: Partial<Booking>): boolean {
|
||||
return changedStatus(booking, updates, BOOKING_STATUS.REJECTED)
|
||||
}
|
||||
|
||||
function wasConfirmed(booking: Booking, updates: Partial<Booking>): boolean {
|
||||
return changedStatus(booking, updates, BOOKING_STATUS.CONFIRMED)
|
||||
}
|
||||
|
||||
export default async function userHandler(
|
||||
req: NextApiRequest,
|
||||
@@ -27,8 +51,16 @@ export default async function userHandler(
|
||||
}
|
||||
|
||||
try {
|
||||
const booking = await patchBooking(uuid, req.body)
|
||||
res.status(200).json(booking)
|
||||
const { current, previous } = await patchBooking(uuid, req.body)
|
||||
|
||||
// TODO: this should really go into the schema
|
||||
if (wasRejected(previous, current)) {
|
||||
sendBookingRejected(current)
|
||||
} else if (wasConfirmed(previous, current)) {
|
||||
sendBookingConfirmed(current)
|
||||
}
|
||||
|
||||
res.status(200).json(current)
|
||||
} catch (error) {
|
||||
console.error('failed patch booking', error)
|
||||
res.status(400).end(`Failed to save booking: ${error.message}`)
|
||||
|
||||
Reference in New Issue
Block a user