Merged in revert-pr-1925 (pull request #1927)
Revert "Feat/sw 2323 find booking (pull request #1925)" Approved-by: Anton Gunnarsson
This commit is contained in:
@@ -1,5 +1,3 @@
|
||||
import { BookingStatusEnum } from "@/constants/booking"
|
||||
import { bookingConfirmation } from "@/constants/routes/hotelReservation"
|
||||
import * as api from "@/lib/api"
|
||||
import { badRequestError, serverErrorByStatus } from "@/server/errors/trpc"
|
||||
import { createCounter } from "@/server/telemetry"
|
||||
@@ -8,47 +6,38 @@ import {
|
||||
safeProtectedServiceProcedure,
|
||||
serviceProcedure,
|
||||
} from "@/server/trpc"
|
||||
import { getBookedHotelRoom } from "@/stores/my-stay"
|
||||
|
||||
import { calculateRefId, parseRefId } from "@/utils/refId"
|
||||
|
||||
import { getHotel } from "../hotels/utils"
|
||||
import { encrypt } from "../utils/encryption"
|
||||
import {
|
||||
bookingConfirmationInput,
|
||||
createRefIdInput,
|
||||
findBookingInput,
|
||||
getBookingConfirmationErrorInput,
|
||||
getBookingInput,
|
||||
getBookingStatusInput,
|
||||
getConfirmationCompletedInput,
|
||||
getLinkedReservationsInput,
|
||||
} from "./input"
|
||||
import { createBookingSchema } from "./output"
|
||||
import { findBooking, getBooking, getLinkedReservations } from "./utils"
|
||||
|
||||
import type { BookingSchema } from "@/types/trpc/routers/booking/confirmation"
|
||||
import { getBookedHotelRoom, getBooking } from "./utils"
|
||||
|
||||
export const bookingQueryRouter = router({
|
||||
confirmation: safeProtectedServiceProcedure
|
||||
.input(bookingConfirmationInput)
|
||||
get: safeProtectedServiceProcedure
|
||||
.input(getBookingInput)
|
||||
.use(async ({ ctx, input, next }) => {
|
||||
const lang = input.lang ?? ctx.lang
|
||||
const { confirmationNumber } = parseRefId(input.refId)
|
||||
const token = ctx.session?.token.access_token ?? ctx.serviceToken
|
||||
return next({
|
||||
ctx: {
|
||||
lang,
|
||||
confirmationNumber,
|
||||
token,
|
||||
},
|
||||
})
|
||||
})
|
||||
.query(async function ({
|
||||
ctx: { confirmationNumber, lang, serviceToken },
|
||||
}) {
|
||||
.query(async function ({ ctx, input: { confirmationNumber } }) {
|
||||
const getBookingCounter = createCounter("trpc.booking", "get")
|
||||
const metricsGetBooking = getBookingCounter.init({ confirmationNumber })
|
||||
|
||||
metricsGetBooking.start()
|
||||
|
||||
const booking = await getBooking(confirmationNumber, lang)
|
||||
const booking = await getBooking(confirmationNumber, ctx.lang, ctx.token)
|
||||
|
||||
if (!booking) {
|
||||
metricsGetBooking.dataError(
|
||||
@@ -58,80 +47,6 @@ export const bookingQueryRouter = router({
|
||||
return null
|
||||
}
|
||||
|
||||
const hotelData = await getHotel(
|
||||
{
|
||||
hotelId: booking.hotelId,
|
||||
isCardOnlyPayment: false,
|
||||
language: lang,
|
||||
},
|
||||
serviceToken
|
||||
)
|
||||
|
||||
if (!hotelData) {
|
||||
metricsGetBooking.dataError(
|
||||
`Failed to get hotel data for ${booking.hotelId}`,
|
||||
{
|
||||
hotelId: booking.hotelId,
|
||||
}
|
||||
)
|
||||
|
||||
throw serverErrorByStatus(404)
|
||||
}
|
||||
|
||||
const room = getBookedHotelRoom(
|
||||
hotelData.roomCategories,
|
||||
booking.roomTypeCode
|
||||
)
|
||||
|
||||
if (!room) {
|
||||
metricsGetBooking.dataError(
|
||||
`Failed to extract booked room ${booking.roomTypeCode} from room categories for ${booking.hotelId}`,
|
||||
{
|
||||
roomTypeCode: booking.roomTypeCode,
|
||||
hotelId: booking.hotelId,
|
||||
}
|
||||
)
|
||||
|
||||
throw serverErrorByStatus(404)
|
||||
}
|
||||
|
||||
metricsGetBooking.success()
|
||||
|
||||
return {
|
||||
hotelData,
|
||||
booking,
|
||||
room,
|
||||
}
|
||||
}),
|
||||
findBooking: safeProtectedServiceProcedure
|
||||
.input(findBookingInput)
|
||||
|
||||
.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.serviceToken,
|
||||
lastName,
|
||||
firstName,
|
||||
email
|
||||
)
|
||||
|
||||
if (!booking) {
|
||||
metricsFindBooking.dataError(
|
||||
`Fail to find booking data for ${confirmationNumber}`,
|
||||
{ confirmationNumber }
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
const hotelData = await getHotel(
|
||||
{
|
||||
hotelId: booking.hotelId,
|
||||
@@ -142,8 +57,8 @@ export const bookingQueryRouter = router({
|
||||
)
|
||||
|
||||
if (!hotelData) {
|
||||
metricsFindBooking.dataError(
|
||||
`Failed to find hotel data for ${booking.hotelId}`,
|
||||
metricsGetBooking.dataError(
|
||||
`Failed to get hotel data for ${booking.hotelId}`,
|
||||
{
|
||||
hotelId: booking.hotelId,
|
||||
}
|
||||
@@ -152,248 +67,109 @@ export const bookingQueryRouter = router({
|
||||
throw serverErrorByStatus(404)
|
||||
}
|
||||
|
||||
const room = getBookedHotelRoom(
|
||||
hotelData.roomCategories,
|
||||
booking.roomTypeCode
|
||||
)
|
||||
|
||||
if (!room) {
|
||||
metricsFindBooking.dataError(
|
||||
`Failed to extract booked room ${booking.roomTypeCode} from room categories for ${booking.hotelId}`,
|
||||
{
|
||||
roomTypeCode: booking.roomTypeCode,
|
||||
hotelId: booking.hotelId,
|
||||
}
|
||||
)
|
||||
|
||||
throw serverErrorByStatus(404)
|
||||
}
|
||||
|
||||
metricsFindBooking.success()
|
||||
metricsGetBooking.success()
|
||||
|
||||
return {
|
||||
hotelData,
|
||||
...hotelData,
|
||||
booking,
|
||||
room,
|
||||
room: getBookedHotelRoom(
|
||||
hotelData.roomCategories,
|
||||
booking.roomTypeCode
|
||||
),
|
||||
}
|
||||
}),
|
||||
linkedReservations: safeProtectedServiceProcedure
|
||||
.input(getLinkedReservationsInput)
|
||||
.use(async ({ ctx, input, next }) => {
|
||||
const lang = input.lang ?? ctx.lang
|
||||
const { confirmationNumber } = parseRefId(input.refId)
|
||||
|
||||
const token = ctx.session?.token.access_token ?? ctx.serviceToken
|
||||
return next({
|
||||
ctx: {
|
||||
lang,
|
||||
confirmationNumber,
|
||||
token,
|
||||
},
|
||||
})
|
||||
})
|
||||
.query(async function ({ ctx: { confirmationNumber, lang } }) {
|
||||
const linkedReservationsCounter = createCounter(
|
||||
.query(async function ({ ctx, input: { rooms } }) {
|
||||
const getLinkedReservationsCounter = createCounter(
|
||||
"trpc.booking",
|
||||
"linkedReservations"
|
||||
)
|
||||
const metricsLinkedReservations = linkedReservationsCounter.init({
|
||||
confirmationNumber,
|
||||
const metricsGetLinkedReservations = getLinkedReservationsCounter.init({
|
||||
confirmationNumbers: rooms,
|
||||
})
|
||||
|
||||
metricsLinkedReservations.start()
|
||||
metricsGetLinkedReservations.start()
|
||||
|
||||
const linkedReservations = await getLinkedReservations(
|
||||
confirmationNumber,
|
||||
lang
|
||||
const linkedReservationsResult = await Promise.allSettled(
|
||||
rooms.map((room) =>
|
||||
getBooking(room.confirmationNumber, ctx.lang, ctx.token)
|
||||
)
|
||||
)
|
||||
|
||||
if (!linkedReservations) {
|
||||
metricsLinkedReservations.noDataError()
|
||||
return null
|
||||
}
|
||||
|
||||
const validLinkedReservations = linkedReservations.reduce<
|
||||
BookingSchema[]
|
||||
>((acc, linkedReservation) => {
|
||||
if ("error" in linkedReservation) {
|
||||
metricsLinkedReservations.dataError(
|
||||
`Failed to get linked reservations ${linkedReservation.confirmationNumber}`,
|
||||
{
|
||||
linkedReservationConfirmationNumber:
|
||||
linkedReservation.confirmationNumber,
|
||||
}
|
||||
const linkedReservations = []
|
||||
for (const booking of linkedReservationsResult) {
|
||||
if (booking.status === "fulfilled") {
|
||||
if (booking.value) {
|
||||
linkedReservations.push(booking.value)
|
||||
} else {
|
||||
metricsGetLinkedReservations.dataError(
|
||||
`Unexpected value for linked reservation`
|
||||
)
|
||||
}
|
||||
} else {
|
||||
metricsGetLinkedReservations.dataError(
|
||||
`Failed to get linked reservation`
|
||||
)
|
||||
return acc
|
||||
}
|
||||
}
|
||||
|
||||
acc.push(linkedReservation)
|
||||
return acc
|
||||
}, [])
|
||||
metricsGetLinkedReservations.success()
|
||||
|
||||
metricsLinkedReservations.success()
|
||||
|
||||
return validLinkedReservations
|
||||
return linkedReservations
|
||||
}),
|
||||
status: serviceProcedure
|
||||
.input(getBookingStatusInput)
|
||||
.use(async ({ input, next }) => {
|
||||
const { confirmationNumber } = parseRefId(input.refId)
|
||||
status: serviceProcedure.input(getBookingStatusInput).query(async function ({
|
||||
ctx,
|
||||
input,
|
||||
}) {
|
||||
const { confirmationNumber } = input
|
||||
|
||||
return next({
|
||||
ctx: {
|
||||
confirmationNumber,
|
||||
},
|
||||
})
|
||||
const getBookingStatusCounter = createCounter("trpc.booking", "status")
|
||||
const metricsGetBookingStatus = getBookingStatusCounter.init({
|
||||
confirmationNumber,
|
||||
})
|
||||
.query(async function ({ ctx: { confirmationNumber, serviceToken } }) {
|
||||
const getBookingStatusCounter = createCounter("trpc.booking", "status")
|
||||
const metricsGetBookingStatus = getBookingStatusCounter.init({
|
||||
confirmationNumber,
|
||||
})
|
||||
|
||||
metricsGetBookingStatus.start()
|
||||
metricsGetBookingStatus.start()
|
||||
|
||||
const apiResponse = await api.get(
|
||||
api.endpoints.v1.Booking.status(confirmationNumber),
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${serviceToken}`,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
await metricsGetBookingStatus.httpError(apiResponse)
|
||||
throw serverErrorByStatus(apiResponse.status, apiResponse)
|
||||
}
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
const verifiedData = createBookingSchema.safeParse(apiJson)
|
||||
if (!verifiedData.success) {
|
||||
metricsGetBookingStatus.validationError(verifiedData.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
metricsGetBookingStatus.success()
|
||||
|
||||
return verifiedData.data
|
||||
}),
|
||||
|
||||
confirmationCompleted: serviceProcedure
|
||||
.input(getConfirmationCompletedInput)
|
||||
.use(async ({ input, next }) => {
|
||||
const { confirmationNumber } = parseRefId(input.refId)
|
||||
|
||||
return next({
|
||||
ctx: {
|
||||
confirmationNumber,
|
||||
const apiResponse = await api.get(
|
||||
api.endpoints.v1.Booking.status(confirmationNumber),
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${ctx.serviceToken}`,
|
||||
},
|
||||
})
|
||||
})
|
||||
.query(async function ({ ctx, input }) {
|
||||
const { confirmationNumber } = ctx
|
||||
|
||||
const confirmationCompletedCounter = createCounter(
|
||||
"trpc.booking",
|
||||
"confirmationCompleted"
|
||||
)
|
||||
const metricsConfirmationCompleted = confirmationCompletedCounter.init({
|
||||
confirmationNumber,
|
||||
})
|
||||
|
||||
metricsConfirmationCompleted.start()
|
||||
|
||||
const apiResponse = await api.get(
|
||||
api.endpoints.v1.Booking.status(confirmationNumber),
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${ctx.serviceToken}`,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
await metricsConfirmationCompleted.httpError(apiResponse)
|
||||
throw serverErrorByStatus(apiResponse.status, apiResponse)
|
||||
}
|
||||
)
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
const verifiedData = createBookingSchema.safeParse(apiJson)
|
||||
if (!verifiedData.success) {
|
||||
metricsConfirmationCompleted.validationError(verifiedData.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
if (!apiResponse.ok) {
|
||||
await metricsGetBookingStatus.httpError(apiResponse)
|
||||
throw serverErrorByStatus(apiResponse.status, apiResponse)
|
||||
}
|
||||
|
||||
const confirmationUrl =
|
||||
verifiedData.data.reservationStatus ===
|
||||
BookingStatusEnum.BookingCompleted
|
||||
? `${bookingConfirmation(input.lang)}?RefId=${verifiedData.data.rooms[0].refId}`
|
||||
: ""
|
||||
const apiJson = await apiResponse.json()
|
||||
const verifiedData = createBookingSchema.safeParse(apiJson)
|
||||
if (!verifiedData.success) {
|
||||
metricsGetBookingStatus.validationError(verifiedData.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
const result = {
|
||||
...verifiedData.data,
|
||||
redirectUrl: verifiedData.data.paymentUrl || confirmationUrl,
|
||||
}
|
||||
|
||||
metricsConfirmationCompleted.success()
|
||||
|
||||
return result
|
||||
}),
|
||||
|
||||
confirmationError: serviceProcedure
|
||||
.input(getBookingConfirmationErrorInput)
|
||||
.use(async ({ input, next }) => {
|
||||
const { confirmationNumber } = parseRefId(input.refId)
|
||||
|
||||
return next({
|
||||
ctx: {
|
||||
confirmationNumber,
|
||||
},
|
||||
})
|
||||
})
|
||||
.query(async function ({ ctx }) {
|
||||
const { confirmationNumber } = ctx
|
||||
|
||||
const confirmationErrorCounter = createCounter(
|
||||
"trpc.booking",
|
||||
"confirmationError"
|
||||
)
|
||||
const metricsConfirmationError = confirmationErrorCounter.init({
|
||||
confirmationNumber,
|
||||
})
|
||||
|
||||
metricsConfirmationError.start()
|
||||
|
||||
const apiResponse = await api.get(
|
||||
api.endpoints.v1.Booking.status(confirmationNumber),
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${ctx.serviceToken}`,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
await metricsConfirmationError.httpError(apiResponse)
|
||||
throw serverErrorByStatus(apiResponse.status, apiResponse)
|
||||
}
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
const verifiedData = createBookingSchema.safeParse(apiJson)
|
||||
if (!verifiedData.success) {
|
||||
metricsConfirmationError.validationError(verifiedData.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
metricsConfirmationError.success()
|
||||
|
||||
return verifiedData.data
|
||||
}),
|
||||
metricsGetBookingStatus.success()
|
||||
|
||||
return verifiedData.data
|
||||
}),
|
||||
createRefId: serviceProcedure
|
||||
.input(createRefIdInput)
|
||||
.mutation(async function ({ input }) {
|
||||
const { confirmationNumber, lastName } = input
|
||||
const encryptedRefId = calculateRefId(confirmationNumber, lastName)
|
||||
const encryptedRefId = encrypt(`${confirmationNumber},${lastName}`)
|
||||
|
||||
if (!encryptedRefId) {
|
||||
throw serverErrorByStatus(422, "Was not able to encrypt ref id")
|
||||
|
||||
Reference in New Issue
Block a user