Add database tables and repository for managing bookings Create API endpoints for booking submission and capacity management Update landing page to persist bookings before WhatsApp redirection
36 lines
1.0 KiB
TypeScript
36 lines
1.0 KiB
TypeScript
import { confirmBookingByConfirmationToken, getBookingByConfirmationToken, getBookingInventorySummary } from '../../../../utils/booking-repository'
|
|
import { getRequiredRouteParam, httpError } from '../../../../utils/http'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const token = getRequiredRouteParam(event, 'token', 'Confirmation token')
|
|
const existingBooking = await getBookingByConfirmationToken(token)
|
|
|
|
if (!existingBooking) {
|
|
httpError(404, 'Booking not found')
|
|
}
|
|
|
|
if (existingBooking.status === 'confirmed') {
|
|
return {
|
|
booking: existingBooking,
|
|
alreadyConfirmed: true
|
|
}
|
|
}
|
|
|
|
const summary = await getBookingInventorySummary()
|
|
|
|
if (summary.leftCapacitySeats !== null && existingBooking.seatCount > summary.leftCapacitySeats) {
|
|
httpError(409, 'Not enough capacity left to confirm this booking')
|
|
}
|
|
|
|
const booking = await confirmBookingByConfirmationToken(token)
|
|
|
|
if (!booking) {
|
|
httpError(404, 'Booking not found')
|
|
}
|
|
|
|
return {
|
|
booking,
|
|
alreadyConfirmed: false
|
|
}
|
|
})
|