Allow users to select payment method and upload receipts before confirming. Add public API endpoints for payment updates and document management.
50 lines
1.7 KiB
TypeScript
50 lines
1.7 KiB
TypeScript
import type { ConfirmBookingResponse } from '~~/shared/booking'
|
|
|
|
import { confirmBookingByConfirmationToken, getBookingByConfirmationToken, getBookingInventorySummary } from '../../../../utils/booking-repository'
|
|
import { getRequiredRouteParam, httpError } from '../../../../utils/http'
|
|
import { sendBookingTicketReceiptViaWhatsApp } from '../../../../utils/whatsapp'
|
|
|
|
export default defineEventHandler(async (event): Promise<ConfirmBookingResponse> => {
|
|
const token = getRequiredRouteParam(event, 'token', 'Confirmation token')
|
|
const existingBooking = await getBookingByConfirmationToken(token, { includeTransactionDocument: true })
|
|
|
|
if (!existingBooking) {
|
|
httpError(404, 'Booking not found')
|
|
}
|
|
|
|
if (existingBooking.status === 'confirmed') {
|
|
return {
|
|
booking: existingBooking,
|
|
alreadyConfirmed: true,
|
|
ticketReceiptWhatsApp: {
|
|
sent: false,
|
|
skipped: true,
|
|
recipientPhone: existingBooking.customerPhone,
|
|
apiRecipientPhone: existingBooking.customerPhone.replace(/\D/g, ''),
|
|
error: 'Booking was already confirmed earlier.'
|
|
}
|
|
}
|
|
}
|
|
|
|
const summary = await getBookingInventorySummary()
|
|
|
|
if (summary.leftSeats !== null && existingBooking.seatCount > summary.leftSeats) {
|
|
httpError(409, 'Not enough capacity left to confirm this booking')
|
|
}
|
|
|
|
const booking = await confirmBookingByConfirmationToken(token)
|
|
|
|
if (!booking) {
|
|
httpError(404, 'Booking not found')
|
|
}
|
|
|
|
const ticketReceiptWhatsApp = await sendBookingTicketReceiptViaWhatsApp(event, booking)
|
|
const refreshedBooking = await getBookingByConfirmationToken(token, { includeTransactionDocument: true })
|
|
|
|
return {
|
|
booking: refreshedBooking || booking,
|
|
alreadyConfirmed: false,
|
|
ticketReceiptWhatsApp
|
|
}
|
|
})
|