import type { TransferBookingPicResponse } from '~~/shared/booking' import { requireAuth } from '../../../utils/auth' import { updateBookingPersonInCharge } from '../../../utils/booking-repository' import { parseBookingPicTransferInput } from '../../../utils/bookings' import { getRequiredRouteParam, httpError } from '../../../utils/http' import { getPublicContactById } from '../../../utils/user-repository' export default defineEventHandler(async (event): Promise => { const auth = await requireAuth(event) const bookingId = getRequiredRouteParam(event, 'id', 'Booking ID') const body = await readBody<{ personInChargeId?: string | null }>(event) const input = parseBookingPicTransferInput(body) const nextPersonInCharge = await getPublicContactById(input.personInChargeId) if (!nextPersonInCharge) { httpError(404, 'Selected person in charge is not available') } const booking = await updateBookingPersonInCharge({ bookingId, nextPersonInChargeId: nextPersonInCharge.id, currentPersonInChargeId: auth.user.role === 'super_admin' ? undefined : auth.user.id }) if (!booking) { httpError(404, 'Booking not found') } return { booking } })