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' export default defineEventHandler(async (event): Promise => { 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 = `https://wa.me/${booking.personInChargePhoneNumber}?text=${encodeURIComponent(whatsappMessage)}` return { booking, confirmationUrl, whatsappUrl } })