This commit is contained in:
Linus Flood
2024-11-05 13:51:10 +01:00
parent 7e4bbfb3e6
commit ae2601bef8
4 changed files with 29 additions and 81 deletions

View File

@@ -1,3 +1,4 @@
import { getHotelData } from "@/lib/trpc/memoizedRequests"
import { serverClient } from "@/lib/trpc/server"
import { getLang } from "@/i18n/serverContext"
@@ -15,7 +16,7 @@ export async function fetchAvailableHotels(
const language = getLang()
const hotels = availableHotels.availability.map(async (hotel) => {
const hotelData = await serverClient().hotel.hotelData.get({
const hotelData = await getHotelData({
hotelId: hotel.hotelId.toString(),
language,
})

View File

@@ -1,7 +1,11 @@
import { notFound } from "next/navigation"
import { dt } from "@/lib/dt"
import { getLocations, getProfileSafely } from "@/lib/trpc/memoizedRequests"
import {
getHotelData,
getLocations,
getProfileSafely,
} from "@/lib/trpc/memoizedRequests"
import { serverClient } from "@/lib/trpc/server"
import { HotelIncludeEnum } from "@/server/routers/hotels/input"
@@ -58,10 +62,7 @@ export default async function SelectRatePage({
: undefined // TODO: Handle multiple rooms
const [hotelData, roomsAvailability, packages, user] = await Promise.all([
serverClient().hotel.hotelData.get({
hotelId: searchParams.hotel,
language: params.lang,
}),
getHotelData({ hotelId: searchParams.hotel, language: params.lang }),
serverClient().hotel.availability.rooms({
hotelId: parseInt(searchParams.hotel, 10),
roomStayStartDate: validFromDate,

View File

@@ -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({

View File

@@ -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,