feat(SW-2116): RefId instead of confirmationNumber
This commit is contained in:
committed by
Michael Zetterberg
parent
7eeb0bbcac
commit
74d37dad93
@@ -103,7 +103,7 @@ export const createBookingInput = z.object({
|
||||
})
|
||||
|
||||
export const addPackageInput = z.object({
|
||||
confirmationNumber: z.string(),
|
||||
refId: z.string(),
|
||||
ancillaryComment: z.string(),
|
||||
ancillaryDeliveryTime: z.string().nullish(),
|
||||
packages: z.array(
|
||||
@@ -117,22 +117,22 @@ export const addPackageInput = z.object({
|
||||
})
|
||||
|
||||
export const removePackageInput = z.object({
|
||||
confirmationNumber: z.string(),
|
||||
refId: z.string(),
|
||||
codes: z.array(z.string()),
|
||||
language: z.nativeEnum(Lang).transform((val) => langToApiLang[val]),
|
||||
})
|
||||
|
||||
export const priceChangeInput = z.object({
|
||||
confirmationNumber: z.string(),
|
||||
refId: z.string(),
|
||||
})
|
||||
|
||||
export const cancelBookingsInput = z.object({
|
||||
confirmationNumbers: z.array(z.string()),
|
||||
language: z.nativeEnum(Lang),
|
||||
refIds: z.array(z.string()),
|
||||
lang: z.nativeEnum(Lang),
|
||||
})
|
||||
|
||||
export const guaranteeBookingInput = z.object({
|
||||
confirmationNumber: z.string(),
|
||||
refId: z.string(),
|
||||
card: z
|
||||
.object({
|
||||
alias: z.string(),
|
||||
@@ -156,7 +156,7 @@ export const createRefIdInput = z.object({
|
||||
})
|
||||
|
||||
export const updateBookingInput = z.object({
|
||||
confirmationNumber: z.string(),
|
||||
refId: z.string(),
|
||||
checkInDate: z.string().optional(),
|
||||
checkOutDate: z.string().optional(),
|
||||
guest: z
|
||||
@@ -168,22 +168,27 @@ export const updateBookingInput = z.object({
|
||||
.optional(),
|
||||
})
|
||||
|
||||
// Query
|
||||
const confirmationNumberInput = z.object({
|
||||
confirmationNumber: z.string(),
|
||||
export const bookingConfirmationInput = z.object({
|
||||
refId: z.string(),
|
||||
lang: z.nativeEnum(Lang).optional(),
|
||||
})
|
||||
|
||||
export const getBookingInput = confirmationNumberInput
|
||||
export const getLinkedReservationsInput = z.object({
|
||||
refId: z.string(),
|
||||
lang: z.nativeEnum(Lang).optional(),
|
||||
rooms: z.array(
|
||||
z.object({
|
||||
confirmationNumber: z.string(),
|
||||
})
|
||||
),
|
||||
})
|
||||
|
||||
export type LinkedReservationsInput = z.input<typeof getLinkedReservationsInput>
|
||||
|
||||
export const getBookingStatusInput = confirmationNumberInput
|
||||
export const getBookingStatusInput = z.object({
|
||||
refId: z.string(),
|
||||
})
|
||||
|
||||
export const getBookingConfirmationErrorInput = z.object({
|
||||
refId: z.string(),
|
||||
})
|
||||
|
||||
export const getConfirmationCompletedInput = z.object({
|
||||
refId: z.string(),
|
||||
lang: z.nativeEnum(Lang),
|
||||
})
|
||||
|
||||
@@ -1,8 +1,11 @@
|
||||
import * as api from "@/lib/api"
|
||||
import { getMembershipNumber } from "@/server/routers/user/utils"
|
||||
import { createCounter } from "@/server/telemetry"
|
||||
import { getUserOrServiceToken } from "@/server/tokenManager"
|
||||
import { router, safeProtectedServiceProcedure } from "@/server/trpc"
|
||||
|
||||
import { parseRefId } from "@/utils/refId"
|
||||
|
||||
import {
|
||||
addPackageInput,
|
||||
cancelBookingsInput,
|
||||
@@ -12,7 +15,7 @@ import {
|
||||
removePackageInput,
|
||||
updateBookingInput,
|
||||
} from "./input"
|
||||
import { bookingConfirmationSchema, createBookingSchema } from "./output"
|
||||
import { bookingSchema, createBookingSchema } from "./output"
|
||||
import { cancelBooking } from "./utils"
|
||||
|
||||
export const bookingMutationRouter = router({
|
||||
@@ -73,8 +76,17 @@ export const bookingMutationRouter = router({
|
||||
}),
|
||||
priceChange: safeProtectedServiceProcedure
|
||||
.input(priceChangeInput)
|
||||
.use(async ({ input, next }) => {
|
||||
const { confirmationNumber } = parseRefId(input.refId)
|
||||
|
||||
return next({
|
||||
ctx: {
|
||||
confirmationNumber,
|
||||
},
|
||||
})
|
||||
})
|
||||
.mutation(async function ({ ctx, input }) {
|
||||
const { confirmationNumber } = input
|
||||
const { confirmationNumber } = ctx
|
||||
|
||||
const priceChangeCounter = createCounter("trpc.booking", "price-change")
|
||||
const metricsPriceChange = priceChangeCounter.init({ confirmationNumber })
|
||||
@@ -109,17 +121,29 @@ export const bookingMutationRouter = router({
|
||||
|
||||
metricsPriceChange.success()
|
||||
|
||||
return verifiedData.data
|
||||
return verifiedData.data.id
|
||||
}),
|
||||
cancel: safeProtectedServiceProcedure
|
||||
.input(cancelBookingsInput)
|
||||
.use(async ({ input, next }) => {
|
||||
const confirmationNumbers = input.refIds.map((refId) => {
|
||||
const { confirmationNumber } = parseRefId(refId)
|
||||
return confirmationNumber
|
||||
})
|
||||
|
||||
return next({
|
||||
ctx: {
|
||||
confirmationNumbers,
|
||||
},
|
||||
})
|
||||
})
|
||||
.mutation(async function ({ ctx, input }) {
|
||||
const token = ctx.session?.token.access_token ?? ctx.serviceToken
|
||||
const { confirmationNumbers, language } = input
|
||||
const { confirmationNumbers } = ctx
|
||||
const { lang } = input
|
||||
|
||||
const responses = await Promise.allSettled(
|
||||
confirmationNumbers.map((confirmationNumber) =>
|
||||
cancelBooking(confirmationNumber, language, token)
|
||||
cancelBooking(confirmationNumber, lang)
|
||||
)
|
||||
)
|
||||
|
||||
@@ -144,10 +168,19 @@ export const bookingMutationRouter = router({
|
||||
}),
|
||||
packages: safeProtectedServiceProcedure
|
||||
.input(addPackageInput)
|
||||
.use(async ({ input, next }) => {
|
||||
const { confirmationNumber } = parseRefId(input.refId)
|
||||
|
||||
return next({
|
||||
ctx: {
|
||||
confirmationNumber,
|
||||
},
|
||||
})
|
||||
})
|
||||
.mutation(async function ({ ctx, input }) {
|
||||
const accessToken = ctx.session?.token.access_token ?? ctx.serviceToken
|
||||
const { confirmationNumber, ...body } = input
|
||||
|
||||
const { refId, ...body } = input
|
||||
const { confirmationNumber } = ctx
|
||||
const addPackageCounter = createCounter("trpc.booking", "package.add")
|
||||
const metricsAddPackage = addPackageCounter.init({ confirmationNumber })
|
||||
|
||||
@@ -183,10 +216,19 @@ export const bookingMutationRouter = router({
|
||||
}),
|
||||
guarantee: safeProtectedServiceProcedure
|
||||
.input(guaranteeBookingInput)
|
||||
.use(async ({ input, next }) => {
|
||||
const { confirmationNumber } = parseRefId(input.refId)
|
||||
|
||||
return next({
|
||||
ctx: {
|
||||
confirmationNumber,
|
||||
},
|
||||
})
|
||||
})
|
||||
.mutation(async function ({ ctx, input }) {
|
||||
const accessToken = ctx.session?.token.access_token ?? ctx.serviceToken
|
||||
const { confirmationNumber, language, ...body } = input
|
||||
|
||||
const { refId, language, ...body } = input
|
||||
const { confirmationNumber } = ctx
|
||||
const guaranteeBookingCounter = createCounter("trpc.booking", "guarantee")
|
||||
const metricsGuaranteeBooking = guaranteeBookingCounter.init({
|
||||
confirmationNumber,
|
||||
@@ -225,10 +267,16 @@ export const bookingMutationRouter = router({
|
||||
}),
|
||||
update: safeProtectedServiceProcedure
|
||||
.input(updateBookingInput)
|
||||
.use(async ({ input, next }) => {
|
||||
const { confirmationNumber } = parseRefId(input.refId)
|
||||
return next({
|
||||
ctx: {
|
||||
confirmationNumber,
|
||||
},
|
||||
})
|
||||
})
|
||||
.mutation(async function ({ ctx, input }) {
|
||||
const accessToken = ctx.session?.token.access_token || ctx.serviceToken
|
||||
const { confirmationNumber, ...body } = input
|
||||
|
||||
const { confirmationNumber } = ctx
|
||||
const updateBookingCounter = createCounter("trpc.booking", "update")
|
||||
const metricsUpdateBooking = updateBookingCounter.init({
|
||||
confirmationNumber,
|
||||
@@ -236,12 +284,17 @@ export const bookingMutationRouter = router({
|
||||
|
||||
metricsUpdateBooking.start()
|
||||
|
||||
const token = getUserOrServiceToken()
|
||||
const apiResponse = await api.put(
|
||||
api.endpoints.v1.Booking.booking(confirmationNumber),
|
||||
{
|
||||
body,
|
||||
body: {
|
||||
checkInDate: input.checkInDate,
|
||||
checkOutDate: input.checkOutDate,
|
||||
guest: input.guest,
|
||||
},
|
||||
headers: {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
}
|
||||
)
|
||||
@@ -253,7 +306,7 @@ export const bookingMutationRouter = router({
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
|
||||
const verifiedData = bookingConfirmationSchema.safeParse(apiJson)
|
||||
const verifiedData = bookingSchema.safeParse(apiJson)
|
||||
if (!verifiedData.success) {
|
||||
metricsUpdateBooking.validationError(verifiedData.error)
|
||||
return null
|
||||
@@ -261,14 +314,23 @@ export const bookingMutationRouter = router({
|
||||
|
||||
metricsUpdateBooking.success()
|
||||
|
||||
return verifiedData.data
|
||||
return verifiedData.data.refId
|
||||
}),
|
||||
removePackage: safeProtectedServiceProcedure
|
||||
.input(removePackageInput)
|
||||
.use(async ({ input, next }) => {
|
||||
const { confirmationNumber } = parseRefId(input.refId)
|
||||
|
||||
return next({
|
||||
ctx: {
|
||||
confirmationNumber,
|
||||
},
|
||||
})
|
||||
})
|
||||
.mutation(async function ({ ctx, input }) {
|
||||
const accessToken = ctx.session?.token.access_token ?? ctx.serviceToken
|
||||
const { confirmationNumber, codes, language } = input
|
||||
|
||||
const { codes, language } = input
|
||||
const { confirmationNumber } = ctx
|
||||
const removePackageCounter = createCounter(
|
||||
"trpc.booking",
|
||||
"package.remove"
|
||||
|
||||
@@ -2,6 +2,7 @@ import { z } from "zod"
|
||||
|
||||
import { BookingStatusEnum, ChildBedTypeEnum } from "@/constants/booking"
|
||||
|
||||
import { calculateRefId } from "@/utils/refId"
|
||||
import { nullableArrayObjectValidator } from "@/utils/zod/arrayValidator"
|
||||
import { nullableIntValidator } from "@/utils/zod/numberValidator"
|
||||
import {
|
||||
@@ -78,7 +79,13 @@ export const createBookingSchema = z
|
||||
type: d.data.type,
|
||||
reservationStatus: d.data.attributes.reservationStatus,
|
||||
paymentUrl: d.data.attributes.paymentUrl,
|
||||
rooms: d.data.attributes.rooms,
|
||||
rooms: d.data.attributes.rooms.map((room) => {
|
||||
const lastName = d.data.attributes.guest?.lastName || ""
|
||||
return {
|
||||
...room,
|
||||
refId: calculateRefId(room.confirmationNumber, lastName),
|
||||
}
|
||||
}),
|
||||
errors: d.data.attributes.errors,
|
||||
guest: d.data.attributes.guest,
|
||||
}))
|
||||
@@ -195,7 +202,7 @@ const linksSchema = z.object({
|
||||
.nullable(),
|
||||
})
|
||||
|
||||
export const bookingConfirmationSchema = z
|
||||
export const bookingSchema = z
|
||||
.object({
|
||||
data: z.object({
|
||||
attributes: z.object({
|
||||
@@ -248,6 +255,19 @@ export const bookingConfirmationSchema = z
|
||||
})
|
||||
.transform(({ data }) => ({
|
||||
...data.attributes,
|
||||
refId: calculateRefId(
|
||||
data.attributes.confirmationNumber,
|
||||
data.attributes.guest.lastName
|
||||
),
|
||||
linkedReservations: data.attributes.linkedReservations.map(
|
||||
(linkedReservation) => {
|
||||
const lastName = data.attributes.guest.lastName
|
||||
return {
|
||||
...linkedReservation,
|
||||
refId: calculateRefId(linkedReservation.confirmationNumber, lastName),
|
||||
}
|
||||
}
|
||||
),
|
||||
packages: data.attributes.packages.filter((p) => p.type !== "Ancillary"),
|
||||
ancillaries: data.attributes.packages.filter((p) => p.type === "Ancillary"),
|
||||
extraBedTypes: data.attributes.childBedPreferences,
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
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"
|
||||
@@ -6,39 +8,46 @@ import {
|
||||
safeProtectedServiceProcedure,
|
||||
serviceProcedure,
|
||||
} from "@/server/trpc"
|
||||
import { getBookedHotelRoom } from "@/stores/my-stay"
|
||||
|
||||
import { calculateRefId } from "@/utils/refId"
|
||||
import { calculateRefId, parseRefId } from "@/utils/refId"
|
||||
|
||||
import { getHotel } from "../hotels/utils"
|
||||
import {
|
||||
bookingConfirmationInput,
|
||||
createRefIdInput,
|
||||
getBookingInput,
|
||||
getBookingConfirmationErrorInput,
|
||||
getBookingStatusInput,
|
||||
getConfirmationCompletedInput,
|
||||
getLinkedReservationsInput,
|
||||
} from "./input"
|
||||
import { createBookingSchema } from "./output"
|
||||
import { getBookedHotelRoom, getBooking } from "./utils"
|
||||
import { getBooking, getLinkedReservations } from "./utils"
|
||||
|
||||
import type { BookingSchema } from "@/types/trpc/routers/booking/confirmation"
|
||||
|
||||
export const bookingQueryRouter = router({
|
||||
get: safeProtectedServiceProcedure
|
||||
.input(getBookingInput)
|
||||
confirmation: safeProtectedServiceProcedure
|
||||
.input(bookingConfirmationInput)
|
||||
.use(async ({ ctx, input, next }) => {
|
||||
const lang = input.lang ?? ctx.lang
|
||||
const token = ctx.session?.token.access_token ?? ctx.serviceToken
|
||||
const { confirmationNumber } = parseRefId(input.refId)
|
||||
return next({
|
||||
ctx: {
|
||||
lang,
|
||||
token,
|
||||
confirmationNumber,
|
||||
},
|
||||
})
|
||||
})
|
||||
.query(async function ({ ctx, input: { confirmationNumber } }) {
|
||||
.query(async function ({
|
||||
ctx: { confirmationNumber, lang, serviceToken },
|
||||
}) {
|
||||
const getBookingCounter = createCounter("trpc.booking", "get")
|
||||
const metricsGetBooking = getBookingCounter.init({ confirmationNumber })
|
||||
|
||||
metricsGetBooking.start()
|
||||
|
||||
const booking = await getBooking(confirmationNumber, ctx.lang, ctx.token)
|
||||
const booking = await getBooking(confirmationNumber, lang)
|
||||
|
||||
if (!booking) {
|
||||
metricsGetBooking.dataError(
|
||||
@@ -52,9 +61,9 @@ export const bookingQueryRouter = router({
|
||||
{
|
||||
hotelId: booking.hotelId,
|
||||
isCardOnlyPayment: false,
|
||||
language: ctx.lang,
|
||||
language: lang,
|
||||
},
|
||||
ctx.serviceToken
|
||||
serviceToken
|
||||
)
|
||||
|
||||
if (!hotelData) {
|
||||
@@ -68,104 +77,243 @@ export const bookingQueryRouter = router({
|
||||
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,
|
||||
hotelData,
|
||||
booking,
|
||||
room: getBookedHotelRoom(
|
||||
hotelData.roomCategories,
|
||||
booking.roomTypeCode
|
||||
),
|
||||
room,
|
||||
}
|
||||
}),
|
||||
linkedReservations: safeProtectedServiceProcedure
|
||||
.input(getLinkedReservationsInput)
|
||||
.use(async ({ ctx, input, next }) => {
|
||||
const lang = input.lang ?? ctx.lang
|
||||
const token = ctx.session?.token.access_token ?? ctx.serviceToken
|
||||
const { confirmationNumber } = parseRefId(input.refId)
|
||||
|
||||
return next({
|
||||
ctx: {
|
||||
lang,
|
||||
token,
|
||||
confirmationNumber,
|
||||
},
|
||||
})
|
||||
})
|
||||
.query(async function ({ ctx, input: { rooms } }) {
|
||||
const getLinkedReservationsCounter = createCounter(
|
||||
.query(async function ({ ctx: { confirmationNumber, lang } }) {
|
||||
const linkedReservationsCounter = createCounter(
|
||||
"trpc.booking",
|
||||
"linkedReservations"
|
||||
)
|
||||
const metricsGetLinkedReservations = getLinkedReservationsCounter.init({
|
||||
confirmationNumbers: rooms,
|
||||
const metricsLinkedReservations = linkedReservationsCounter.init({
|
||||
confirmationNumber,
|
||||
})
|
||||
|
||||
metricsGetLinkedReservations.start()
|
||||
metricsLinkedReservations.start()
|
||||
|
||||
const linkedReservationsResult = await Promise.allSettled(
|
||||
rooms.map((room) =>
|
||||
getBooking(room.confirmationNumber, ctx.lang, ctx.token)
|
||||
)
|
||||
const linkedReservations = await getLinkedReservations(
|
||||
confirmationNumber,
|
||||
lang
|
||||
)
|
||||
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`
|
||||
|
||||
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,
|
||||
}
|
||||
)
|
||||
return acc
|
||||
}
|
||||
}
|
||||
|
||||
metricsGetLinkedReservations.success()
|
||||
acc.push(linkedReservation)
|
||||
return acc
|
||||
}, [])
|
||||
|
||||
return linkedReservations
|
||||
metricsLinkedReservations.success()
|
||||
|
||||
return validLinkedReservations
|
||||
}),
|
||||
status: serviceProcedure.input(getBookingStatusInput).query(async function ({
|
||||
ctx,
|
||||
input,
|
||||
}) {
|
||||
const { confirmationNumber } = input
|
||||
status: serviceProcedure
|
||||
.input(getBookingStatusInput)
|
||||
.use(async ({ input, next }) => {
|
||||
const { confirmationNumber } = parseRefId(input.refId)
|
||||
|
||||
const getBookingStatusCounter = createCounter("trpc.booking", "status")
|
||||
const metricsGetBookingStatus = getBookingStatusCounter.init({
|
||||
confirmationNumber,
|
||||
})
|
||||
|
||||
metricsGetBookingStatus.start()
|
||||
|
||||
const apiResponse = await api.get(
|
||||
api.endpoints.v1.Booking.status(confirmationNumber),
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${ctx.serviceToken}`,
|
||||
return next({
|
||||
ctx: {
|
||||
confirmationNumber,
|
||||
},
|
||||
})
|
||||
})
|
||||
.query(async function ({ ctx: { confirmationNumber, serviceToken } }) {
|
||||
const getBookingStatusCounter = createCounter("trpc.booking", "status")
|
||||
const metricsGetBookingStatus = getBookingStatusCounter.init({
|
||||
confirmationNumber,
|
||||
})
|
||||
|
||||
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)
|
||||
}
|
||||
)
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
const verifiedData = createBookingSchema.safeParse(apiJson)
|
||||
if (!verifiedData.success) {
|
||||
metricsGetBookingStatus.validationError(verifiedData.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
metricsGetBookingStatus.success()
|
||||
|
||||
metricsGetBookingStatus.success()
|
||||
return verifiedData.data
|
||||
}),
|
||||
|
||||
confirmationCompleted: serviceProcedure
|
||||
.input(getConfirmationCompletedInput)
|
||||
.use(async ({ input, next }) => {
|
||||
const { confirmationNumber } = parseRefId(input.refId)
|
||||
|
||||
return next({
|
||||
ctx: {
|
||||
confirmationNumber,
|
||||
},
|
||||
})
|
||||
})
|
||||
.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()
|
||||
}
|
||||
|
||||
const confirmationUrl =
|
||||
verifiedData.data.reservationStatus ===
|
||||
BookingStatusEnum.BookingCompleted
|
||||
? `${bookingConfirmation(input.lang)}?RefId=${verifiedData.data.rooms[0].refId}`
|
||||
: ""
|
||||
|
||||
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
|
||||
}),
|
||||
|
||||
return verifiedData.data
|
||||
}),
|
||||
createRefId: serviceProcedure
|
||||
.input(createRefIdInput)
|
||||
.mutation(async function ({ input }) {
|
||||
|
||||
@@ -1,101 +1,134 @@
|
||||
import * as api from "@/lib/api"
|
||||
import { badRequestError, serverErrorByStatus } from "@/server/errors/trpc"
|
||||
import { createCounter } from "@/server/telemetry"
|
||||
import { getUserOrServiceToken } from "@/server/tokenManager"
|
||||
import { toApiLang } from "@/server/utils"
|
||||
|
||||
import { bookingConfirmationSchema, createBookingSchema } from "./output"
|
||||
import { getCacheClient } from "@/services/dataCache"
|
||||
|
||||
import type { Room } from "@/types/hotel"
|
||||
import type { BookingConfirmation } from "@/types/trpc/routers/booking/confirmation"
|
||||
import { bookingSchema, createBookingSchema } from "./output"
|
||||
|
||||
import type { BookingSchema } from "@/types/trpc/routers/booking/confirmation"
|
||||
import type { Lang } from "@/constants/languages"
|
||||
|
||||
export function getBookedHotelRoom(
|
||||
rooms: Room[] | undefined,
|
||||
roomTypeCode: BookingConfirmation["booking"]["roomTypeCode"]
|
||||
) {
|
||||
if (!rooms?.length || !roomTypeCode) {
|
||||
return null
|
||||
}
|
||||
const room = rooms?.find((r) => {
|
||||
return r.roomTypes.find((roomType) => roomType.code === roomTypeCode)
|
||||
})
|
||||
if (!room) {
|
||||
return null
|
||||
}
|
||||
const bedType = room.roomTypes.find(
|
||||
(roomType) => roomType.code === roomTypeCode
|
||||
)
|
||||
if (!bedType) {
|
||||
return null
|
||||
}
|
||||
return {
|
||||
...room,
|
||||
bedType,
|
||||
}
|
||||
}
|
||||
|
||||
export async function getBooking(
|
||||
confirmationNumber: string,
|
||||
lang: Lang,
|
||||
token: string
|
||||
) {
|
||||
export async function getBooking(confirmationNumber: string, lang: Lang) {
|
||||
const getBookingCounter = createCounter("booking", "get")
|
||||
const metricsGetBooking = getBookingCounter.init({ confirmationNumber })
|
||||
|
||||
metricsGetBooking.start()
|
||||
|
||||
const apiResponse = await api.get(
|
||||
api.endpoints.v1.Booking.booking(confirmationNumber),
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
const cacheKey = `${lang}:booking:${confirmationNumber}`
|
||||
const cache = await getCacheClient()
|
||||
|
||||
const result: BookingSchema | null = await cache.cacheOrGet(
|
||||
cacheKey,
|
||||
async () => {
|
||||
const token = getUserOrServiceToken()
|
||||
|
||||
const apiResponse = await api.get(
|
||||
api.endpoints.v1.Booking.booking(confirmationNumber),
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${token}`,
|
||||
},
|
||||
},
|
||||
{ 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 = bookingSchema.safeParse(apiJson)
|
||||
if (!booking.success) {
|
||||
metricsGetBooking.validationError(booking.error)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
return booking.data
|
||||
},
|
||||
{ language: toApiLang(lang) }
|
||||
"1h"
|
||||
)
|
||||
|
||||
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
|
||||
return result
|
||||
}
|
||||
|
||||
export async function cancelBooking(
|
||||
export async function getBookings(confirmationNumbers: string[], lang: Lang) {
|
||||
const results = await Promise.allSettled(
|
||||
confirmationNumbers.map((confirmationNumber) => {
|
||||
return getBooking(confirmationNumber, lang)
|
||||
})
|
||||
)
|
||||
|
||||
return results.map((result) => {
|
||||
if (result.status === "fulfilled" && result.value) {
|
||||
return result.value
|
||||
}
|
||||
return null
|
||||
})
|
||||
}
|
||||
|
||||
export async function getLinkedReservations(
|
||||
confirmationNumber: string,
|
||||
language: Lang,
|
||||
token: string
|
||||
lang: Lang
|
||||
) {
|
||||
const booking = await getBooking(confirmationNumber, lang)
|
||||
|
||||
if (!booking) {
|
||||
return null
|
||||
}
|
||||
|
||||
if (booking.linkedReservations.length > 0) {
|
||||
const confirmationNumbers = booking.linkedReservations.map(
|
||||
(linkedReservation) => {
|
||||
return linkedReservation.confirmationNumber
|
||||
}
|
||||
)
|
||||
|
||||
const bookings = await getBookings(confirmationNumbers, lang)
|
||||
|
||||
const linkedReservations = bookings.map((booking, i) => {
|
||||
if (booking === null) {
|
||||
return {
|
||||
confirmationNumber: confirmationNumbers[i],
|
||||
error: true,
|
||||
} as const
|
||||
}
|
||||
return booking
|
||||
})
|
||||
|
||||
return linkedReservations
|
||||
}
|
||||
|
||||
return []
|
||||
}
|
||||
|
||||
export async function cancelBooking(confirmationNumber: string, lang: Lang) {
|
||||
const cancelBookingCounter = createCounter("booking", "cancel")
|
||||
const metricsCancelBooking = cancelBookingCounter.init({
|
||||
confirmationNumber,
|
||||
language,
|
||||
lang,
|
||||
})
|
||||
|
||||
metricsCancelBooking.start()
|
||||
|
||||
const token = getUserOrServiceToken()
|
||||
const headers = {
|
||||
Authorization: `Bearer ${token}`,
|
||||
}
|
||||
|
||||
const booking = await getBooking(confirmationNumber, language, token)
|
||||
const booking = await getBooking(confirmationNumber, lang)
|
||||
if (!booking) {
|
||||
metricsCancelBooking.noDataError({ confirmationNumber })
|
||||
return null
|
||||
@@ -107,7 +140,7 @@ export async function cancelBooking(
|
||||
headers,
|
||||
body: { firstName, lastName, email },
|
||||
},
|
||||
{ language: toApiLang(language) }
|
||||
{ language: toApiLang(lang) }
|
||||
)
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
|
||||
@@ -3,7 +3,9 @@ import { trace, type Tracer } from "@opentelemetry/api"
|
||||
import { env } from "@/env/server"
|
||||
import { createCounter } from "@/server/telemetry"
|
||||
|
||||
import { auth } from "@/auth"
|
||||
import { getCacheClient } from "@/services/dataCache"
|
||||
import { isValidSession } from "@/utils/session"
|
||||
|
||||
import type { ServiceTokenResponse } from "@/types/tokens"
|
||||
|
||||
@@ -117,3 +119,12 @@ async function fetchServiceToken(scopes: string[]) {
|
||||
function getServiceTokenCacheKey(scopes: string[]): string {
|
||||
return `serviceToken:${scopes.join(",")}`
|
||||
}
|
||||
|
||||
export async function getUserOrServiceToken() {
|
||||
const serviceToken = await getServiceToken()
|
||||
const session = await auth()
|
||||
|
||||
return isValidSession(session)
|
||||
? session.token.access_token
|
||||
: serviceToken.access_token
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user