Add WhatsApp API integration for automated receipt delivery Enforce country codes for all phone number inputs (defaults to +60)
49 lines
1.6 KiB
TypeScript
49 lines
1.6 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)
|
|
|
|
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)
|
|
|
|
return {
|
|
booking,
|
|
alreadyConfirmed: false,
|
|
ticketReceiptWhatsApp
|
|
}
|
|
})
|