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:
@@ -84,13 +84,14 @@ export async function createBooking({
|
|||||||
export async function patchBooking(
|
export async function patchBooking(
|
||||||
bookingUUID: string,
|
bookingUUID: string,
|
||||||
bookingData: Booking
|
bookingData: Booking
|
||||||
): Promise<Booking> {
|
): Promise<{ current: Booking; previous: Booking }> {
|
||||||
await connect()
|
await connect()
|
||||||
const booking = await getBookingByUUID(bookingUUID)
|
const booking = await getBookingByUUID(bookingUUID)
|
||||||
|
const oldBooking = booking.toJSON()
|
||||||
booking.set(bookingData)
|
booking.set(bookingData)
|
||||||
await booking.save()
|
await booking.save()
|
||||||
|
|
||||||
return booking.toJSON()
|
return { current: booking.toJSON(), previous: oldBooking }
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createBill(
|
export async function createBill(
|
||||||
|
|||||||
@@ -1,6 +1,30 @@
|
|||||||
import { NextApiRequest, NextApiResponse } from 'next'
|
import { NextApiRequest, NextApiResponse } from 'next'
|
||||||
|
import Book from '../../../../components/book'
|
||||||
|
import { Booking } from '../../../../db/booking'
|
||||||
import { BOOKING_STATUS } from '../../../../db/enums'
|
import { BOOKING_STATUS } from '../../../../db/enums'
|
||||||
import { patchBooking } from '../../../../db/index'
|
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(
|
export default async function userHandler(
|
||||||
req: NextApiRequest,
|
req: NextApiRequest,
|
||||||
@@ -27,8 +51,16 @@ export default async function userHandler(
|
|||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const booking = await patchBooking(uuid, req.body)
|
const { current, previous } = await patchBooking(uuid, req.body)
|
||||||
res.status(200).json(booking)
|
|
||||||
|
// 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) {
|
} catch (error) {
|
||||||
console.error('failed patch booking', error)
|
console.error('failed patch booking', error)
|
||||||
res.status(400).end(`Failed to save booking: ${error.message}`)
|
res.status(400).end(`Failed to save booking: ${error.message}`)
|
||||||
|
|||||||
Reference in New Issue
Block a user