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
35 lines
1.1 KiB
TypeScript
35 lines
1.1 KiB
TypeScript
import type { CancelBookingConfirmationResponse } from '~~/shared/booking'
|
|
|
|
import { requireBookingManager } from '../../../../utils/auth'
|
|
import { cancelBookingConfirmationByConfirmationToken, getBookingByConfirmationToken } from '../../../../utils/booking-repository'
|
|
import { getRequiredRouteParam, httpError } from '../../../../utils/http'
|
|
|
|
export default defineEventHandler(async (event): Promise<CancelBookingConfirmationResponse> => {
|
|
const token = getRequiredRouteParam(event, 'token', 'Confirmation token')
|
|
const existingBooking = await getBookingByConfirmationToken(token, { includeTransactionDocument: true })
|
|
|
|
if (!existingBooking) {
|
|
httpError(404, 'Booking not found')
|
|
}
|
|
|
|
await requireBookingManager(event, existingBooking)
|
|
|
|
if (existingBooking.status === 'pending') {
|
|
return {
|
|
booking: existingBooking,
|
|
alreadyPending: true
|
|
}
|
|
}
|
|
|
|
const booking = await cancelBookingConfirmationByConfirmationToken(token)
|
|
|
|
if (!booking) {
|
|
httpError(404, 'Booking not found')
|
|
}
|
|
|
|
return {
|
|
booking: await getBookingByConfirmationToken(token, { includeTransactionDocument: true }) || booking,
|
|
alreadyPending: false
|
|
}
|
|
})
|