Add UI modal and button to reassign bookings to different contacts Create API endpoint and repository method to handle PIC transfers
37 lines
1.2 KiB
TypeScript
37 lines
1.2 KiB
TypeScript
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<TransferBookingPicResponse> => {
|
|
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
|
|
}
|
|
})
|