SW-454 Use the API on select room page
This commit is contained in:
committed by
Pontus Dreij
parent
23953aeb61
commit
c2c1a676ca
@@ -17,6 +17,17 @@ export const getHotelsAvailabilityInputSchema = z.object({
|
||||
attachedProfileId: z.string().optional().default(""),
|
||||
})
|
||||
|
||||
export const getRoomsAvailabilityInputSchema = z.object({
|
||||
hotelId: z.number(),
|
||||
roomStayStartDate: z.string(),
|
||||
roomStayEndDate: z.string(),
|
||||
adults: z.number(),
|
||||
children: z.number().optional().default(0),
|
||||
promotionCode: z.string().optional(),
|
||||
reservationProfileType: z.string().optional().default(""),
|
||||
attachedProfileId: z.string().optional().default(""),
|
||||
})
|
||||
|
||||
export const getRatesInputSchema = z.object({
|
||||
hotelId: z.string(),
|
||||
})
|
||||
|
||||
@@ -572,6 +572,86 @@ export type HotelsAvailability = z.infer<typeof hotelsAvailabilitySchema>
|
||||
export type HotelsAvailabilityPrices =
|
||||
HotelsAvailability["data"][number]["attributes"]["bestPricePerNight"]
|
||||
|
||||
const productSchema = z.object({
|
||||
productType: z.object({
|
||||
public: z.object({
|
||||
rateCode: z.string(),
|
||||
rateType: z.string().optional(),
|
||||
localPrice: z.object({
|
||||
pricePerNight: z.string(),
|
||||
pricePerStay: z.string(),
|
||||
currency: z.string(),
|
||||
}),
|
||||
requestedPrice: z
|
||||
.object({
|
||||
pricePerNight: z.string(),
|
||||
pricePerStay: z.string(),
|
||||
currency: z.string(),
|
||||
})
|
||||
.optional(),
|
||||
}),
|
||||
member: z.object({
|
||||
rateCode: z.string(),
|
||||
rateType: z.string().optional(),
|
||||
localPrice: z.object({
|
||||
pricePerNight: z.string(),
|
||||
pricePerStay: z.string(),
|
||||
currency: z.string(),
|
||||
}),
|
||||
requestedPrice: z
|
||||
.object({
|
||||
pricePerNight: z.string(),
|
||||
pricePerStay: z.string(),
|
||||
currency: z.string(),
|
||||
})
|
||||
.optional(),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
|
||||
const roomConfigurationSchema = z.object({
|
||||
status: z.string(),
|
||||
bedType: z.string(),
|
||||
roomType: z.string(),
|
||||
roomsLeft: z.number(),
|
||||
features: z.array(z.object({ inventory: z.number(), code: z.string() })),
|
||||
products: z.array(productSchema),
|
||||
})
|
||||
|
||||
const rateDefinitionSchema = z.object({
|
||||
title: z.string(),
|
||||
breakfastIncluded: z.boolean(),
|
||||
rateType: z.string().optional(),
|
||||
rateCode: z.string(),
|
||||
generalTerms: z.array(z.string()),
|
||||
cancellationRule: z.string(),
|
||||
cancellationText: z.string(),
|
||||
mustBeGuaranteed: z.boolean(),
|
||||
})
|
||||
|
||||
const roomsAvailabilitySchema = z
|
||||
.object({
|
||||
data: z.object({
|
||||
attributes: z.object({
|
||||
checkInDate: z.string(),
|
||||
checkOutDate: z.string(),
|
||||
occupancy: occupancySchema.optional(),
|
||||
hotelId: z.number(),
|
||||
roomConfigurations: z.array(roomConfigurationSchema),
|
||||
rateDefinitions: z.array(rateDefinitionSchema),
|
||||
}),
|
||||
relationships: linksSchema.optional(),
|
||||
type: z.string().optional(),
|
||||
}),
|
||||
})
|
||||
.transform((o) => o.data.attributes)
|
||||
|
||||
export const getRoomsAvailabilitySchema = roomsAvailabilitySchema
|
||||
export type RoomsAvailability = z.infer<typeof roomsAvailabilitySchema>
|
||||
export type RoomConfiguration = z.infer<typeof roomConfigurationSchema>
|
||||
export type Product = z.infer<typeof productSchema>
|
||||
export type RateDefinition = z.infer<typeof rateDefinitionSchema>
|
||||
|
||||
const flexibilityPrice = z.object({
|
||||
standard: z.number(),
|
||||
member: z.number(),
|
||||
|
||||
@@ -24,11 +24,13 @@ import {
|
||||
getHotelsAvailabilityInputSchema,
|
||||
getlHotelDataInputSchema,
|
||||
getRatesInputSchema,
|
||||
getRoomsAvailabilityInputSchema,
|
||||
} from "./input"
|
||||
import {
|
||||
getHotelDataSchema,
|
||||
getHotelsAvailabilitySchema,
|
||||
getRatesSchema,
|
||||
getRoomsAvailabilitySchema,
|
||||
roomSchema,
|
||||
} from "./output"
|
||||
import tempRatesData from "./tempRatesData.json"
|
||||
@@ -61,6 +63,16 @@ const hotelsAvailabilityFailCounter = meter.createCounter(
|
||||
"trpc.hotel.availability.hotels-fail"
|
||||
)
|
||||
|
||||
const roomsAvailabilityCounter = meter.createCounter(
|
||||
"trpc.hotel.availability.rooms"
|
||||
)
|
||||
const roomsAvailabilitySuccessCounter = meter.createCounter(
|
||||
"trpc.hotel.availability.rooms-success"
|
||||
)
|
||||
const roomsAvailabilityFailCounter = meter.createCounter(
|
||||
"trpc.hotel.availability.rooms-fail"
|
||||
)
|
||||
|
||||
async function getContentstackData(
|
||||
locale: string,
|
||||
uid: string | null | undefined
|
||||
@@ -376,6 +388,123 @@ export const hotelQueryRouter = router({
|
||||
.flatMap((hotels) => hotels.attributes),
|
||||
}
|
||||
}),
|
||||
rooms: hotelServiceProcedure
|
||||
.input(getRoomsAvailabilityInputSchema)
|
||||
.query(async ({ input, ctx }) => {
|
||||
const {
|
||||
hotelId,
|
||||
roomStayStartDate,
|
||||
roomStayEndDate,
|
||||
adults,
|
||||
children,
|
||||
promotionCode,
|
||||
reservationProfileType,
|
||||
attachedProfileId,
|
||||
} = input
|
||||
|
||||
const params: Record<string, string | number | undefined> = {
|
||||
roomStayStartDate,
|
||||
roomStayEndDate,
|
||||
adults,
|
||||
children,
|
||||
promotionCode,
|
||||
reservationProfileType,
|
||||
attachedProfileId,
|
||||
}
|
||||
|
||||
roomsAvailabilityCounter.add(1, {
|
||||
hotelId,
|
||||
roomStayStartDate,
|
||||
roomStayEndDate,
|
||||
adults,
|
||||
children,
|
||||
promotionCode,
|
||||
reservationProfileType,
|
||||
})
|
||||
console.info(
|
||||
"api.hotels.roomsAvailability start",
|
||||
JSON.stringify({ query: { hotelId, params } })
|
||||
)
|
||||
const apiResponse = await api.get(
|
||||
`${api.endpoints.v1.roomsAvailability}/${hotelId}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${ctx.serviceToken}`,
|
||||
},
|
||||
},
|
||||
params
|
||||
)
|
||||
if (!apiResponse.ok) {
|
||||
const text = await apiResponse.text()
|
||||
roomsAvailabilityFailCounter.add(1, {
|
||||
hotelId,
|
||||
roomStayStartDate,
|
||||
roomStayEndDate,
|
||||
adults,
|
||||
children,
|
||||
promotionCode,
|
||||
reservationProfileType,
|
||||
error_type: "http_error",
|
||||
error: JSON.stringify({
|
||||
status: apiResponse.status,
|
||||
statusText: apiResponse.statusText,
|
||||
text,
|
||||
}),
|
||||
})
|
||||
console.error(
|
||||
"api.hotels.roomsAvailability error",
|
||||
JSON.stringify({
|
||||
query: { hotelId, params },
|
||||
error: {
|
||||
status: apiResponse.status,
|
||||
statusText: apiResponse.statusText,
|
||||
text,
|
||||
},
|
||||
})
|
||||
)
|
||||
return null
|
||||
}
|
||||
const apiJson = await apiResponse.json()
|
||||
const validateAvailabilityData =
|
||||
getRoomsAvailabilitySchema.safeParse(apiJson)
|
||||
if (!validateAvailabilityData.success) {
|
||||
roomsAvailabilityFailCounter.add(1, {
|
||||
hotelId,
|
||||
roomStayStartDate,
|
||||
roomStayEndDate,
|
||||
adults,
|
||||
children,
|
||||
promotionCode,
|
||||
reservationProfileType,
|
||||
error_type: "validation_error",
|
||||
error: JSON.stringify(validateAvailabilityData.error),
|
||||
})
|
||||
console.error(
|
||||
"api.hotels.roomsAvailability validation error",
|
||||
JSON.stringify({
|
||||
query: { hotelId, params },
|
||||
error: validateAvailabilityData.error,
|
||||
})
|
||||
)
|
||||
throw badRequestError()
|
||||
}
|
||||
roomsAvailabilitySuccessCounter.add(1, {
|
||||
hotelId,
|
||||
roomStayStartDate,
|
||||
roomStayEndDate,
|
||||
adults,
|
||||
children,
|
||||
promotionCode,
|
||||
reservationProfileType,
|
||||
})
|
||||
console.info(
|
||||
"api.hotels.roomsAvailability success",
|
||||
JSON.stringify({
|
||||
query: { hotelId, params: params },
|
||||
})
|
||||
)
|
||||
return validateAvailabilityData.data
|
||||
}),
|
||||
}),
|
||||
rates: router({
|
||||
get: publicProcedure
|
||||
|
||||
Reference in New Issue
Block a user