Refactor
This commit is contained in:
@@ -50,9 +50,9 @@ export const getRatesInputSchema = z.object({
|
||||
|
||||
export const HotelIncludeEnum = z.enum([
|
||||
"RoomCategories",
|
||||
"NearbyHotels",
|
||||
"Restaurants",
|
||||
"City",
|
||||
//"NearbyHotels",
|
||||
//"Restaurants",
|
||||
//"City",
|
||||
])
|
||||
|
||||
export const getHotelDataInputSchema = z.object({
|
||||
|
||||
@@ -279,77 +279,21 @@ export const hotelQueryRouter = router({
|
||||
throw notFound(`Hotel not found for uid: ${uid}`)
|
||||
}
|
||||
|
||||
const apiLang = toApiLang(lang)
|
||||
const params: Record<string, string> = {
|
||||
hotelId,
|
||||
language: apiLang,
|
||||
}
|
||||
|
||||
params.include = HotelIncludeEnum.options.join(",")
|
||||
|
||||
getHotelCounter.add(1, { hotelId, lang })
|
||||
console.info(
|
||||
"api.hotels.hotel start",
|
||||
JSON.stringify({
|
||||
query: { hotelId, params },
|
||||
})
|
||||
)
|
||||
const apiResponse = await api.get(
|
||||
api.endpoints.v1.Hotel.Hotels.hotel(hotelId),
|
||||
const hotelData = await getHotelData(
|
||||
{
|
||||
headers: {
|
||||
Authorization: `Bearer ${ctx.serviceToken}`,
|
||||
},
|
||||
// needs to clear default option as only
|
||||
// cache or next.revalidate is permitted
|
||||
cache: undefined,
|
||||
next: {
|
||||
revalidate: 60 * 30, // 30 minutes
|
||||
},
|
||||
hotelId,
|
||||
language: ctx.lang,
|
||||
},
|
||||
params
|
||||
ctx.serviceToken
|
||||
)
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
const text = await apiResponse.text()
|
||||
getHotelFailCounter.add(1, {
|
||||
hotelId,
|
||||
lang,
|
||||
error_type: "http_error",
|
||||
error: JSON.stringify({
|
||||
status: apiResponse.status,
|
||||
statusText: apiResponse.statusText,
|
||||
text,
|
||||
}),
|
||||
})
|
||||
console.error(
|
||||
"api.hotels.hotel error",
|
||||
JSON.stringify({
|
||||
query: { hotelId, params },
|
||||
error: {
|
||||
status: apiResponse.status,
|
||||
statusText: apiResponse.statusText,
|
||||
text,
|
||||
},
|
||||
})
|
||||
)
|
||||
throw serverErrorByStatus(apiResponse.status, apiResponse)
|
||||
}
|
||||
const apiJson = await apiResponse.json()
|
||||
const validatedHotelData = getHotelDataSchema.safeParse(apiJson)
|
||||
const validatedHotelData = getHotelDataSchema.safeParse(hotelData)
|
||||
|
||||
if (!validatedHotelData.success) {
|
||||
getHotelFailCounter.add(1, {
|
||||
hotelId,
|
||||
lang,
|
||||
error_type: "validation_error",
|
||||
error: JSON.stringify(validatedHotelData.error),
|
||||
})
|
||||
|
||||
console.error(
|
||||
"api.hotels.hotel validation error",
|
||||
JSON.stringify({
|
||||
query: { hotelId, params },
|
||||
query: { hotelId },
|
||||
error: validatedHotelData.error,
|
||||
})
|
||||
)
|
||||
@@ -372,26 +316,28 @@ export const hotelQueryRouter = router({
|
||||
|
||||
const facilities: Facility[] = [
|
||||
{
|
||||
...apiJson.data.attributes.restaurantImages,
|
||||
id: FacilityCardTypeEnum.restaurant,
|
||||
headingText:
|
||||
hotelData?.data.attributes.restaurantImages?.headingText ?? "",
|
||||
heroImages:
|
||||
hotelData?.data.attributes.restaurantImages?.heroImages ?? [],
|
||||
},
|
||||
{
|
||||
...apiJson.data.attributes.conferencesAndMeetings,
|
||||
id: FacilityCardTypeEnum.conference,
|
||||
headingText:
|
||||
hotelData?.data.attributes.conferencesAndMeetings?.headingText ?? "",
|
||||
heroImages:
|
||||
hotelData?.data.attributes.conferencesAndMeetings?.heroImages ?? [],
|
||||
},
|
||||
{
|
||||
...apiJson.data.attributes.healthAndWellness,
|
||||
id: FacilityCardTypeEnum.wellness,
|
||||
headingText:
|
||||
hotelData?.data.attributes.healthAndWellness?.headingText ?? "",
|
||||
heroImages:
|
||||
hotelData?.data.attributes.healthAndWellness?.heroImages ?? [],
|
||||
},
|
||||
]
|
||||
|
||||
getHotelSuccessCounter.add(1, { hotelId, lang })
|
||||
console.info(
|
||||
"api.hotels.hotel success",
|
||||
JSON.stringify({
|
||||
query: { hotelId, params: params },
|
||||
})
|
||||
)
|
||||
return {
|
||||
hotelName: hotelAttributes.name,
|
||||
hotelDescription: hotelAttributes.hotelContent.texts.descriptions.short,
|
||||
|
||||
Reference in New Issue
Block a user