feat(SW-251): use availability to get hotel
This commit is contained in:
@@ -21,8 +21,10 @@ export const getRatesInputSchema = z.object({
|
||||
hotelId: z.string(),
|
||||
})
|
||||
|
||||
export const getFiltersInputSchema = z.object({
|
||||
export const getPlaceholderInputSchema = z.object({
|
||||
hotelId: z.string(),
|
||||
language: z.string(),
|
||||
country: z.string(),
|
||||
city: z.string(),
|
||||
include: z
|
||||
.array(z.enum(["RoomCategories", "NearbyHotels", "Restaurants", "City"]))
|
||||
.optional(),
|
||||
})
|
||||
|
||||
@@ -552,33 +552,3 @@ const rate = z.object({
|
||||
|
||||
export const getRatesSchema = z.array(rate)
|
||||
export type Rate = z.infer<typeof rate>
|
||||
|
||||
const hotelFilterSchema = z.object({
|
||||
data: z.array(
|
||||
z.object({
|
||||
attributes: z.object({
|
||||
name: z.string(),
|
||||
operaId: z.string(),
|
||||
isPublished: z.boolean(),
|
||||
cityId: z.string(),
|
||||
cityName: z.string(),
|
||||
ratings: ratingsSchema,
|
||||
address: addressSchema,
|
||||
location: locationSchema,
|
||||
hotelContent: hotelContentSchema.optional(),
|
||||
detailedFacilities: z.array(detailedFacilitySchema),
|
||||
isActive: z.boolean(),
|
||||
}),
|
||||
id: z.string(),
|
||||
language: z.unknown(),
|
||||
hotelInformationSystemId: z.number(),
|
||||
type: z.string(),
|
||||
})
|
||||
),
|
||||
})
|
||||
|
||||
export const getHotelFilterSchema = hotelFilterSchema
|
||||
export type HotelFilter = z.infer<typeof hotelFilterSchema>
|
||||
export type HotelId = HotelFilter["data"][number]["id"]
|
||||
export type HotelFilters =
|
||||
HotelFilter["data"][number]["attributes"]["detailedFacilities"]
|
||||
|
||||
@@ -27,16 +27,14 @@ import {
|
||||
} from "../contentstack/hotelPage/output"
|
||||
import {
|
||||
getAvailabilityInputSchema,
|
||||
getFiltersInputSchema,
|
||||
getHotelInputSchema,
|
||||
getPlaceholderInputSchema,
|
||||
getRatesInputSchema,
|
||||
} from "./input"
|
||||
import {
|
||||
getAvailabilitySchema,
|
||||
getHotelDataSchema,
|
||||
getHotelFilterSchema,
|
||||
getRatesSchema,
|
||||
HotelFilters,
|
||||
roomSchema,
|
||||
} from "./output"
|
||||
import tempRatesData from "./tempRatesData.json"
|
||||
@@ -423,31 +421,33 @@ export const hotelQueryRouter = router({
|
||||
return validatedHotelData.data
|
||||
}),
|
||||
}),
|
||||
filters: router({
|
||||
hotel: router({
|
||||
get: serviceProcedure
|
||||
.input(getFiltersInputSchema)
|
||||
.query(async ({ input, ctx }) => {
|
||||
const { language, country, city } = input
|
||||
.input(getPlaceholderInputSchema)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const { hotelId, language, include } = input
|
||||
|
||||
const params: Record<string, string> = {
|
||||
hotelId,
|
||||
language,
|
||||
country,
|
||||
city,
|
||||
}
|
||||
|
||||
filterCounter.add(1, {
|
||||
if (include) {
|
||||
params.include = include.join(",")
|
||||
}
|
||||
|
||||
getHotelCounter.add(1, {
|
||||
hotelId,
|
||||
language,
|
||||
country,
|
||||
city,
|
||||
include,
|
||||
})
|
||||
console.info(
|
||||
"api.hotels.filters start",
|
||||
JSON.stringify({
|
||||
query: { params },
|
||||
})
|
||||
"api.hotels.hotel start",
|
||||
JSON.stringify({ query: { hotelId, params } })
|
||||
)
|
||||
|
||||
const apiResponse = await api.get(
|
||||
api.endpoints.v1.hotels,
|
||||
`${api.endpoints.v1.hotels}/${hotelId}`,
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${ctx.serviceToken}`,
|
||||
@@ -458,10 +458,10 @@ export const hotelQueryRouter = router({
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
const text = await apiResponse.text()
|
||||
filterFailCounter.add(1, {
|
||||
getHotelFailCounter.add(1, {
|
||||
hotelId,
|
||||
language,
|
||||
country,
|
||||
city,
|
||||
include,
|
||||
error_type: "http_error",
|
||||
error: JSON.stringify({
|
||||
status: apiResponse.status,
|
||||
@@ -470,11 +470,9 @@ export const hotelQueryRouter = router({
|
||||
}),
|
||||
})
|
||||
console.error(
|
||||
"api.hotels.filters error",
|
||||
"api.hotels.hotel error",
|
||||
JSON.stringify({
|
||||
query: {
|
||||
params,
|
||||
},
|
||||
query: { hotelId, params },
|
||||
error: {
|
||||
status: apiResponse.status,
|
||||
statusText: apiResponse.statusText,
|
||||
@@ -486,47 +484,39 @@ export const hotelQueryRouter = router({
|
||||
}
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
const validateFilterData = getHotelFilterSchema.safeParse(apiJson)
|
||||
const validateHotelData = getHotelDataSchema.safeParse(apiJson)
|
||||
|
||||
if (!validateFilterData.success) {
|
||||
filterFailCounter.add(1, {
|
||||
if (!validateHotelData.success) {
|
||||
getHotelFailCounter.add(1, {
|
||||
hotelId,
|
||||
language,
|
||||
country,
|
||||
city,
|
||||
include,
|
||||
error_type: "validation_error",
|
||||
error: JSON.stringify(validateFilterData.error),
|
||||
error: JSON.stringify(validateHotelData.error),
|
||||
})
|
||||
|
||||
console.error(
|
||||
"api.hotels.filters validation error",
|
||||
"api.hotels.hotel validation error",
|
||||
JSON.stringify({
|
||||
query: { params },
|
||||
error: validateFilterData.error,
|
||||
query: { hotelId, params },
|
||||
error: validateHotelData.error,
|
||||
})
|
||||
)
|
||||
throw badRequestError()
|
||||
}
|
||||
|
||||
filterSuccessCounter.add(1, {
|
||||
getHotelSuccessCounter.add(1, {
|
||||
hotelId,
|
||||
language,
|
||||
country,
|
||||
city,
|
||||
include,
|
||||
})
|
||||
console.info(
|
||||
"api.hotels.fuilters success",
|
||||
JSON.stringify({ query: { params: params } })
|
||||
"api.hotels.hotel success",
|
||||
JSON.stringify({
|
||||
query: { hotelId, params: params },
|
||||
})
|
||||
)
|
||||
|
||||
const hotelData = validateFilterData.data.data
|
||||
const filters = hotelData.flatMap(
|
||||
(data) => data.attributes.detailedFacilities
|
||||
)
|
||||
const unieqeFilters = [...new Set(filters.map((data) => data.id))]
|
||||
|
||||
return {
|
||||
filters: unieqeFilters
|
||||
.map((data) => filters.find((find) => find.id === data))
|
||||
.filter((filter) => filter !== undefined) as HotelFilters,
|
||||
}
|
||||
return validateHotelData.data
|
||||
}),
|
||||
}),
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user