Refactor
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import { getHotelData } from "@/lib/trpc/memoizedRequests"
|
||||||
import { serverClient } from "@/lib/trpc/server"
|
import { serverClient } from "@/lib/trpc/server"
|
||||||
|
|
||||||
import { getLang } from "@/i18n/serverContext"
|
import { getLang } from "@/i18n/serverContext"
|
||||||
@@ -15,7 +16,7 @@ export async function fetchAvailableHotels(
|
|||||||
|
|
||||||
const language = getLang()
|
const language = getLang()
|
||||||
const hotels = availableHotels.availability.map(async (hotel) => {
|
const hotels = availableHotels.availability.map(async (hotel) => {
|
||||||
const hotelData = await serverClient().hotel.hotelData.get({
|
const hotelData = await getHotelData({
|
||||||
hotelId: hotel.hotelId.toString(),
|
hotelId: hotel.hotelId.toString(),
|
||||||
language,
|
language,
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,7 +1,11 @@
|
|||||||
import { notFound } from "next/navigation"
|
import { notFound } from "next/navigation"
|
||||||
|
|
||||||
import { dt } from "@/lib/dt"
|
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 { serverClient } from "@/lib/trpc/server"
|
||||||
import { HotelIncludeEnum } from "@/server/routers/hotels/input"
|
import { HotelIncludeEnum } from "@/server/routers/hotels/input"
|
||||||
|
|
||||||
@@ -58,10 +62,7 @@ export default async function SelectRatePage({
|
|||||||
: undefined // TODO: Handle multiple rooms
|
: undefined // TODO: Handle multiple rooms
|
||||||
|
|
||||||
const [hotelData, roomsAvailability, packages, user] = await Promise.all([
|
const [hotelData, roomsAvailability, packages, user] = await Promise.all([
|
||||||
serverClient().hotel.hotelData.get({
|
getHotelData({ hotelId: searchParams.hotel, language: params.lang }),
|
||||||
hotelId: searchParams.hotel,
|
|
||||||
language: params.lang,
|
|
||||||
}),
|
|
||||||
serverClient().hotel.availability.rooms({
|
serverClient().hotel.availability.rooms({
|
||||||
hotelId: parseInt(searchParams.hotel, 10),
|
hotelId: parseInt(searchParams.hotel, 10),
|
||||||
roomStayStartDate: validFromDate,
|
roomStayStartDate: validFromDate,
|
||||||
|
|||||||
@@ -50,9 +50,9 @@ export const getRatesInputSchema = z.object({
|
|||||||
|
|
||||||
export const HotelIncludeEnum = z.enum([
|
export const HotelIncludeEnum = z.enum([
|
||||||
"RoomCategories",
|
"RoomCategories",
|
||||||
"NearbyHotels",
|
//"NearbyHotels",
|
||||||
"Restaurants",
|
//"Restaurants",
|
||||||
"City",
|
//"City",
|
||||||
])
|
])
|
||||||
|
|
||||||
export const getHotelDataInputSchema = z.object({
|
export const getHotelDataInputSchema = z.object({
|
||||||
|
|||||||
@@ -279,77 +279,21 @@ export const hotelQueryRouter = router({
|
|||||||
throw notFound(`Hotel not found for uid: ${uid}`)
|
throw notFound(`Hotel not found for uid: ${uid}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
const apiLang = toApiLang(lang)
|
const hotelData = await getHotelData(
|
||||||
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),
|
|
||||||
{
|
{
|
||||||
headers: {
|
hotelId,
|
||||||
Authorization: `Bearer ${ctx.serviceToken}`,
|
language: ctx.lang,
|
||||||
},
|
},
|
||||||
// needs to clear default option as only
|
ctx.serviceToken
|
||||||
// cache or next.revalidate is permitted
|
|
||||||
cache: undefined,
|
|
||||||
next: {
|
|
||||||
revalidate: 60 * 30, // 30 minutes
|
|
||||||
},
|
|
||||||
},
|
|
||||||
params
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if (!apiResponse.ok) {
|
const validatedHotelData = getHotelDataSchema.safeParse(hotelData)
|
||||||
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)
|
|
||||||
|
|
||||||
if (!validatedHotelData.success) {
|
if (!validatedHotelData.success) {
|
||||||
getHotelFailCounter.add(1, {
|
|
||||||
hotelId,
|
|
||||||
lang,
|
|
||||||
error_type: "validation_error",
|
|
||||||
error: JSON.stringify(validatedHotelData.error),
|
|
||||||
})
|
|
||||||
|
|
||||||
console.error(
|
console.error(
|
||||||
"api.hotels.hotel validation error",
|
"api.hotels.hotel validation error",
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
query: { hotelId, params },
|
query: { hotelId },
|
||||||
error: validatedHotelData.error,
|
error: validatedHotelData.error,
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
@@ -372,26 +316,28 @@ export const hotelQueryRouter = router({
|
|||||||
|
|
||||||
const facilities: Facility[] = [
|
const facilities: Facility[] = [
|
||||||
{
|
{
|
||||||
...apiJson.data.attributes.restaurantImages,
|
|
||||||
id: FacilityCardTypeEnum.restaurant,
|
id: FacilityCardTypeEnum.restaurant,
|
||||||
|
headingText:
|
||||||
|
hotelData?.data.attributes.restaurantImages?.headingText ?? "",
|
||||||
|
heroImages:
|
||||||
|
hotelData?.data.attributes.restaurantImages?.heroImages ?? [],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
...apiJson.data.attributes.conferencesAndMeetings,
|
|
||||||
id: FacilityCardTypeEnum.conference,
|
id: FacilityCardTypeEnum.conference,
|
||||||
|
headingText:
|
||||||
|
hotelData?.data.attributes.conferencesAndMeetings?.headingText ?? "",
|
||||||
|
heroImages:
|
||||||
|
hotelData?.data.attributes.conferencesAndMeetings?.heroImages ?? [],
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
...apiJson.data.attributes.healthAndWellness,
|
|
||||||
id: FacilityCardTypeEnum.wellness,
|
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 {
|
return {
|
||||||
hotelName: hotelAttributes.name,
|
hotelName: hotelAttributes.name,
|
||||||
hotelDescription: hotelAttributes.hotelContent.texts.descriptions.short,
|
hotelDescription: hotelAttributes.hotelContent.texts.descriptions.short,
|
||||||
|
|||||||
Reference in New Issue
Block a user