Files
dticket.tootaio.com/server/api/public/bookings.post.ts
xiaomai c214d643dd feat: send ticket receipts via WhatsApp and normalize phone numbers
Add WhatsApp API integration for automated receipt delivery
Enforce country codes for all phone number inputs (defaults to +60)
2026-04-27 13:12:25 +08:00

58 lines
2.0 KiB
TypeScript

import type { BookingMode, CreateBookingResponse, TicketType } from '~~/shared/booking'
import { getTicketCatalogItem, getSeatCount } from '~~/shared/booking'
import { buildAppUrl } from '../../utils/app-url'
import { createBooking } from '../../utils/booking-repository'
import { buildBookingMessage, parseCreateBookingInput } from '../../utils/bookings'
import { assertBadRequest } from '../../utils/http'
import { getPublicContactById } from '../../utils/user-repository'
import { buildWhatsAppDeepLink } from '../../utils/whatsapp'
export default defineEventHandler(async (event): Promise<CreateBookingResponse> => {
const body = await readBody<{
customerName?: string
customerPhone?: string
bookingMode?: BookingMode | string | null
quantity?: number
ticketType?: TicketType
personInChargeId?: string
}>(event)
const input = parseCreateBookingInput(body)
const personInCharge = await getPublicContactById(input.personInChargeId)
assertBadRequest(personInCharge, 'Selected person in charge is not available')
const ticket = getTicketCatalogItem(input.ticketType)
assertBadRequest(ticket, 'Ticket category is invalid')
const seatCount = getSeatCount(input.bookingMode, input.quantity)
const totalPrice = seatCount * ticket.price
const { booking, confirmationToken } = await createBooking({
customerName: input.customerName,
customerPhone: input.customerPhone,
bookingMode: input.bookingMode,
quantity: input.quantity,
seatCount,
ticketType: input.ticketType,
unitPrice: ticket.price,
totalPrice,
personInChargeId: personInCharge.id,
personInChargeName: personInCharge.fullName,
personInChargePhoneNumber: personInCharge.phoneNumber
})
const confirmationUrl = buildAppUrl(event, `/confirmation/${confirmationToken}`)
const whatsappMessage = buildBookingMessage(booking, confirmationUrl)
const whatsappUrl = buildWhatsAppDeepLink(booking.personInChargePhoneNumber, whatsappMessage)
return {
booking,
confirmationUrl,
whatsappUrl
}
})