mirror of
https://github.com/tomru/pfadi-bussle.git
synced 2026-03-03 14:37:13 +01:00
47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
import { NextApiRequest, NextApiResponse } from 'next'
|
|
import { Booking, Prisma } from '@prisma/client';
|
|
import { createBooking } from '../../../db/index'
|
|
import { log } from '../../../helpers/log'
|
|
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)
|
|
} catch (e) {
|
|
// TODO: add validation for booking on same day
|
|
log.error('Failed to store booking with', e.toString())
|
|
res.status(500).end(`Internal Server Error...Guru is meditating...`)
|
|
return
|
|
}
|
|
|
|
log.info(
|
|
`received booking ${booking.uuid} from ${booking.email}`,
|
|
booking
|
|
)
|
|
await sendReceivedBookingAdminMail(booking)
|
|
log.info(`send booking ${booking.uuid} received to admin`, booking)
|
|
await sendReceivedBookingBookerMail(booking)
|
|
log.info(
|
|
`send booking ${booking.uuid} received to {booking.email}`,
|
|
booking
|
|
)
|
|
res.status(200).json(booking)
|
|
break
|
|
default:
|
|
res.setHeader('Allow', ['POST'])
|
|
res.status(405).end(`Method ${method} Not Allowed`)
|
|
}
|
|
}
|