feat(bookings): add payment and document upload to confirmation page
Allow users to select payment method and upload receipts before confirming. Add public API endpoints for payment updates and document management.
This commit is contained in:
51
server/api/public/bookings/[token]/payment.patch.ts
Normal file
51
server/api/public/bookings/[token]/payment.patch.ts
Normal file
@@ -0,0 +1,51 @@
|
||||
import type { UpdateBookingDetailsResponse } from '~~/shared/booking'
|
||||
|
||||
import {
|
||||
clearBookingTransactionDocumentByConfirmationToken,
|
||||
getBookingByConfirmationToken,
|
||||
updateBookingPaymentMethodByConfirmationToken
|
||||
} from '../../../../utils/booking-repository'
|
||||
import { parsePaymentMethodInput } from '../../../../utils/bookings'
|
||||
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 existingBooking = await getBookingByConfirmationToken(token, { includeTransactionDocument: true })
|
||||
|
||||
if (!existingBooking) {
|
||||
httpError(404, 'Booking not found')
|
||||
}
|
||||
|
||||
if (existingBooking.status !== 'pending') {
|
||||
httpError(409, 'Payment details can only be changed before confirmation')
|
||||
}
|
||||
|
||||
const body = await readBody<{ paymentMethod?: string | null }>(event)
|
||||
const input = parsePaymentMethodInput(body)
|
||||
|
||||
const booking = await updateBookingPaymentMethodByConfirmationToken({
|
||||
confirmationToken: token,
|
||||
paymentMethod: input.paymentMethod
|
||||
})
|
||||
|
||||
if (!booking) {
|
||||
httpError(404, 'Booking not found')
|
||||
}
|
||||
|
||||
if (input.paymentMethod === 'cash') {
|
||||
const cleared = await clearBookingTransactionDocumentByConfirmationToken(token)
|
||||
|
||||
if (cleared) {
|
||||
await deleteTransactionDocument(cleared.previousStorageName)
|
||||
|
||||
return {
|
||||
booking: cleared.booking
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
booking
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user