wip
This commit is contained in:
@@ -189,6 +189,14 @@ export const getLinkedReservationsInput = z.object({
|
||||
),
|
||||
})
|
||||
|
||||
export const findBookingInput = z.object({
|
||||
confirmationNumber: z.string(),
|
||||
firstName: z.string(),
|
||||
lastName: z.string(),
|
||||
email: z.string(),
|
||||
lang: z.nativeEnum(Lang).optional(),
|
||||
})
|
||||
|
||||
export type LinkedReservationsInput = z.input<typeof getLinkedReservationsInput>
|
||||
|
||||
export const getBookingStatusInput = confirmationNumberInput
|
||||
|
||||
@@ -11,12 +11,13 @@ import { getHotel } from "../hotels/utils"
|
||||
import { encrypt } from "../utils/encryption"
|
||||
import {
|
||||
createRefIdInput,
|
||||
findBookingInput,
|
||||
getBookingInput,
|
||||
getBookingStatusInput,
|
||||
getLinkedReservationsInput,
|
||||
} from "./input"
|
||||
import { createBookingSchema } from "./output"
|
||||
import { getBookedHotelRoom, getBooking } from "./utils"
|
||||
import { findBooking, getBookedHotelRoom, getBooking } from "./utils"
|
||||
|
||||
export const bookingQueryRouter = router({
|
||||
get: safeProtectedServiceProcedure
|
||||
@@ -69,6 +70,74 @@ export const bookingQueryRouter = router({
|
||||
|
||||
metricsGetBooking.success()
|
||||
|
||||
return {
|
||||
...hotelData,
|
||||
booking,
|
||||
room: getBookedHotelRoom(
|
||||
hotelData.roomCategories,
|
||||
booking.roomTypeCode
|
||||
),
|
||||
}
|
||||
}),
|
||||
findBooking: safeProtectedServiceProcedure
|
||||
.input(findBookingInput)
|
||||
.use(async ({ ctx, input, next }) => {
|
||||
const lang = input.lang ?? ctx.lang
|
||||
const token = ctx.session?.token.access_token ?? ctx.serviceToken
|
||||
return next({
|
||||
ctx: {
|
||||
lang,
|
||||
token,
|
||||
},
|
||||
})
|
||||
})
|
||||
.query(async function ({
|
||||
ctx,
|
||||
input: { confirmationNumber, lastName, firstName, email },
|
||||
}) {
|
||||
const findBookingCounter = createCounter("trpc.booking", "findBooking")
|
||||
const metricsFindBooking = findBookingCounter.init({ confirmationNumber })
|
||||
|
||||
metricsFindBooking.start()
|
||||
const booking = await findBooking(
|
||||
confirmationNumber,
|
||||
ctx.lang,
|
||||
ctx.token,
|
||||
lastName,
|
||||
firstName,
|
||||
email
|
||||
)
|
||||
|
||||
if (!booking) {
|
||||
metricsFindBooking.dataError(
|
||||
`Fail to find booking data for ${confirmationNumber}`,
|
||||
{ confirmationNumber }
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
const hotelData = await getHotel(
|
||||
{
|
||||
hotelId: booking.hotelId,
|
||||
isCardOnlyPayment: false,
|
||||
language: ctx.lang,
|
||||
},
|
||||
ctx.serviceToken
|
||||
)
|
||||
|
||||
if (!hotelData) {
|
||||
metricsFindBooking.dataError(
|
||||
`Failed to find hotel data for ${booking.hotelId}`,
|
||||
{
|
||||
hotelId: booking.hotelId,
|
||||
}
|
||||
)
|
||||
|
||||
throw serverErrorByStatus(404)
|
||||
}
|
||||
|
||||
metricsFindBooking.success()
|
||||
|
||||
return {
|
||||
...hotelData,
|
||||
booking,
|
||||
|
||||
@@ -78,6 +78,63 @@ export async function getBooking(
|
||||
return booking.data
|
||||
}
|
||||
|
||||
export async function findBooking(
|
||||
confirmationNumber: string,
|
||||
lang: Lang,
|
||||
token: string,
|
||||
lastName?: string,
|
||||
firstName?: string,
|
||||
email?: string
|
||||
) {
|
||||
const findBookingCounter = createCounter("booking", "find")
|
||||
const metricsGetBooking = findBookingCounter.init({
|
||||
confirmationNumber,
|
||||
lastName,
|
||||
firstName,
|
||||
email,
|
||||
})
|
||||
|
||||
metricsGetBooking.start()
|
||||
|
||||
const apiResponse = await api.post(
|
||||
api.endpoints.v1.Booking.find(confirmationNumber),
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
body: {
|
||||
lastName,
|
||||
firstName,
|
||||
email,
|
||||
},
|
||||
},
|
||||
{ language: toApiLang(lang) }
|
||||
)
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
await metricsGetBooking.httpError(apiResponse)
|
||||
|
||||
// If the booking is not found, return null.
|
||||
// This scenario is expected to happen when a logged in user trying to access a booking that doesn't belong to them.
|
||||
if (apiResponse.status === 400) {
|
||||
return null
|
||||
}
|
||||
|
||||
throw serverErrorByStatus(apiResponse.status, apiResponse)
|
||||
}
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
const booking = bookingConfirmationSchema.safeParse(apiJson)
|
||||
if (!booking.success) {
|
||||
metricsGetBooking.validationError(booking.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
metricsGetBooking.success()
|
||||
|
||||
return booking.data
|
||||
}
|
||||
|
||||
export async function cancelBooking(
|
||||
confirmationNumber: string,
|
||||
language: string,
|
||||
|
||||
Reference in New Issue
Block a user