Secure API endpoints with requireBookingManager authorization check Update confirmation page to prompt for login if unauthorized Add safe redirect handling to login and guest middleware
37 lines
1.2 KiB
TypeScript
37 lines
1.2 KiB
TypeScript
import type { UpdateBookingDetailsResponse } from '~~/shared/booking'
|
|
|
|
import { requireBookingManager } from '../../../../utils/auth'
|
|
import {
|
|
clearBookingTransactionDocumentByConfirmationToken,
|
|
getBookingByConfirmationToken
|
|
} from '../../../../utils/booking-repository'
|
|
import { getRequiredRouteParam, httpError } from '../../../../utils/http'
|
|
import { deleteTransactionDocument } from '../../../../utils/transaction-documents'
|
|
|
|
export default defineEventHandler(async (event): Promise<UpdateBookingDetailsResponse> => {
|
|
const token = getRequiredRouteParam(event, 'token', 'Confirmation token')
|
|
const booking = await getBookingByConfirmationToken(token, { includeTransactionDocument: true })
|
|
|
|
if (!booking) {
|
|
httpError(404, 'Booking not found')
|
|
}
|
|
|
|
await requireBookingManager(event, booking)
|
|
|
|
if (booking.status !== 'pending') {
|
|
httpError(409, 'Transaction document can only be changed before confirmation')
|
|
}
|
|
|
|
const result = await clearBookingTransactionDocumentByConfirmationToken(token)
|
|
|
|
if (!result) {
|
|
httpError(404, 'Booking not found')
|
|
}
|
|
|
|
await deleteTransactionDocument(result.previousStorageName)
|
|
|
|
return {
|
|
booking: result.booking
|
|
}
|
|
})
|