mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 06:27:11 +01:00
It also brings the problem of consolidating bookers over multiple bookings. The amount of data is not justifying having it in an own entity
44 lines
1.3 KiB
TypeScript
44 lines
1.3 KiB
TypeScript
import { Error } from 'mongoose'
|
|
import { NextApiRequest, NextApiResponse } from 'next'
|
|
import { Booking } from '../../../db/booking'
|
|
import { createBooking } from '../../../db/index'
|
|
import {
|
|
sendReceivedBookingAdminMail,
|
|
sendReceivedBookingBookerMail,
|
|
} from '../../../helpers/mail'
|
|
|
|
export default async function userHandler(
|
|
req: NextApiRequest,
|
|
res: NextApiResponse
|
|
): Promise<void> {
|
|
const { method } = req
|
|
|
|
let booking: Booking
|
|
|
|
switch (method) {
|
|
case 'POST':
|
|
try {
|
|
booking = await createBooking(req.body)
|
|
res.status(200).json(booking)
|
|
} catch (e) {
|
|
console.error(e)
|
|
if (e instanceof Error.ValidationError) {
|
|
res.status(400).json({ message: e.message, errors: e.errors })
|
|
return
|
|
}
|
|
res.status(500).end(`Internal Server Error...Guru is meditating...`)
|
|
return
|
|
}
|
|
|
|
console.log(`received booking ${booking.uuid} from {booking.email}`)
|
|
await sendReceivedBookingAdminMail(booking)
|
|
console.log(`send booking ${booking.uuid} received to admin`)
|
|
await sendReceivedBookingBookerMail(booking)
|
|
console.log(`send booking ${booking.uuid} received to {booking.email}`)
|
|
break
|
|
default:
|
|
res.setHeader('Allow', ['POST'])
|
|
res.status(405).end(`Method ${method} Not Allowed`)
|
|
}
|
|
}
|