feat(SW-201): Refactoring how we fetch hotel page data
This commit is contained in:
@@ -1,13 +1,9 @@
|
||||
import { metrics } from "@opentelemetry/api"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
import * as api from "@/lib/api"
|
||||
import { dt } from "@/lib/dt"
|
||||
import { GetHotelPage } from "@/lib/graphql/Query/HotelPage/HotelPage.graphql"
|
||||
import { request } from "@/lib/graphql/request"
|
||||
import { badRequestError, notFound } from "@/server/errors/trpc"
|
||||
import { badRequestError } from "@/server/errors/trpc"
|
||||
import {
|
||||
contentStackUidWithServiceProcedure,
|
||||
publicProcedure,
|
||||
router,
|
||||
safeProtectedServiceProcedure,
|
||||
@@ -17,13 +13,6 @@ import { toApiLang } from "@/server/utils"
|
||||
|
||||
import { cache } from "@/utils/cache"
|
||||
|
||||
import { hotelPageSchema } from "../contentstack/hotelPage/output"
|
||||
import {
|
||||
fetchHotelPageRefs,
|
||||
generatePageTags,
|
||||
getHotelPageCounter,
|
||||
validateHotelPageRefs,
|
||||
} from "../contentstack/hotelPage/utils"
|
||||
import { getVerifiedUser, parsedUser } from "../user/query"
|
||||
import {
|
||||
getBreakfastPackageInputSchema,
|
||||
@@ -52,13 +41,10 @@ import {
|
||||
TWENTYFOUR_HOURS,
|
||||
} from "./utils"
|
||||
|
||||
import { FacilityCardTypeEnum } from "@/types/components/hotelPage/facilities"
|
||||
import type { BedTypeSelection } from "@/types/components/hotelReservation/enterDetails/bedType"
|
||||
import { BreakfastPackageEnum } from "@/types/enums/breakfast"
|
||||
import { HotelTypeEnum } from "@/types/enums/hotelType"
|
||||
import type { RequestOptionsWithOutBody } from "@/types/fetch"
|
||||
import type { Facility } from "@/types/hotel"
|
||||
import type { GetHotelPageData } from "@/types/trpc/routers/contentstack/hotelPage"
|
||||
|
||||
const meter = metrics.getMeter("trpc.hotels")
|
||||
const getHotelCounter = meter.createCounter("trpc.hotel.get")
|
||||
@@ -111,55 +97,6 @@ const breakfastPackagesFailCounter = meter.createCounter(
|
||||
"trpc.package.breakfast-fail"
|
||||
)
|
||||
|
||||
async function getContentstackData(lang: Lang, uid?: string | null) {
|
||||
if (!uid) {
|
||||
return null
|
||||
}
|
||||
const contentPageRefsData = await fetchHotelPageRefs(lang, uid)
|
||||
const contentPageRefs = validateHotelPageRefs(contentPageRefsData, lang, uid)
|
||||
if (!contentPageRefs) {
|
||||
return null
|
||||
}
|
||||
|
||||
const tags = generatePageTags(contentPageRefs, lang)
|
||||
|
||||
getHotelPageCounter.add(1, { lang, uid })
|
||||
console.info(
|
||||
"contentstack.hotelPage start",
|
||||
JSON.stringify({
|
||||
query: { lang, uid },
|
||||
})
|
||||
)
|
||||
const response = await request<GetHotelPageData>(
|
||||
GetHotelPage,
|
||||
{
|
||||
locale: lang,
|
||||
uid,
|
||||
},
|
||||
{
|
||||
cache: "force-cache",
|
||||
next: {
|
||||
tags,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
if (!response.data) {
|
||||
throw notFound(response)
|
||||
}
|
||||
|
||||
const hotelPageData = hotelPageSchema.safeParse(response.data)
|
||||
if (!hotelPageData.success) {
|
||||
console.error(
|
||||
`Failed to validate Hotel Page - (uid: ${uid}, lang: ${lang})`
|
||||
)
|
||||
console.error(hotelPageData.error)
|
||||
return null
|
||||
}
|
||||
|
||||
return hotelPageData.data.hotel_page
|
||||
}
|
||||
|
||||
export const getHotelData = cache(
|
||||
async (input: HotelDataInput, serviceToken: string) => {
|
||||
const { hotelId, language, isCardOnlyPayment } = input
|
||||
@@ -273,90 +210,6 @@ export const getHotelData = cache(
|
||||
)
|
||||
|
||||
export const hotelQueryRouter = router({
|
||||
get: contentStackUidWithServiceProcedure.query(async ({ ctx }) => {
|
||||
const { lang, uid } = ctx
|
||||
|
||||
const contentstackData = await getContentstackData(lang, uid)
|
||||
const hotelId = contentstackData?.hotel_page_id
|
||||
|
||||
if (!hotelId) {
|
||||
throw notFound(`Hotel not found for uid: ${uid}`)
|
||||
}
|
||||
|
||||
const hotelData = await getHotelData(
|
||||
{
|
||||
hotelId,
|
||||
language: ctx.lang,
|
||||
},
|
||||
ctx.serviceToken
|
||||
)
|
||||
|
||||
if (!hotelData) {
|
||||
throw notFound()
|
||||
}
|
||||
|
||||
const included = hotelData.included || []
|
||||
|
||||
const hotelAttributes = hotelData.data.attributes
|
||||
const images = hotelAttributes.gallery?.smallerImages
|
||||
const hotelAlerts = hotelAttributes.specialAlerts
|
||||
|
||||
const roomCategories = included
|
||||
? included.filter((item) => item.type === "roomcategories")
|
||||
: []
|
||||
|
||||
const activities = contentstackData?.content
|
||||
? contentstackData?.content[0]
|
||||
: null
|
||||
|
||||
const facilities: Facility[] = [
|
||||
{
|
||||
...hotelData.data.attributes.restaurantImages,
|
||||
id: FacilityCardTypeEnum.restaurant,
|
||||
headingText:
|
||||
hotelData?.data.attributes.restaurantImages?.headingText ?? "",
|
||||
heroImages:
|
||||
hotelData?.data.attributes.restaurantImages?.heroImages ?? [],
|
||||
},
|
||||
{
|
||||
...hotelData.data.attributes.conferencesAndMeetings,
|
||||
id: FacilityCardTypeEnum.conference,
|
||||
headingText:
|
||||
hotelData?.data.attributes.conferencesAndMeetings?.headingText ?? "",
|
||||
heroImages:
|
||||
hotelData?.data.attributes.conferencesAndMeetings?.heroImages ?? [],
|
||||
},
|
||||
{
|
||||
...hotelData.data.attributes.healthAndWellness,
|
||||
id: FacilityCardTypeEnum.wellness,
|
||||
headingText:
|
||||
hotelData?.data.attributes.healthAndWellness?.headingText ?? "",
|
||||
heroImages:
|
||||
hotelData?.data.attributes.healthAndWellness?.heroImages ?? [],
|
||||
},
|
||||
]
|
||||
|
||||
return {
|
||||
hotelId,
|
||||
hotelName: hotelAttributes.name,
|
||||
hotelDescriptions: hotelAttributes.hotelContent.texts,
|
||||
hotelLocation: hotelAttributes.location,
|
||||
hotelAddress: hotelAttributes.address,
|
||||
hotelRatings: hotelAttributes.ratings,
|
||||
hotelDetailedFacilities: hotelAttributes.detailedFacilities,
|
||||
hotelImages: images,
|
||||
pointsOfInterest: hotelAttributes.pointsOfInterest,
|
||||
roomCategories,
|
||||
activitiesCard: activities?.upcoming_activities_card,
|
||||
facilities,
|
||||
alerts: hotelAlerts,
|
||||
faq: contentstackData?.faq,
|
||||
healthFacilities: hotelAttributes.healthFacilities,
|
||||
contact: hotelAttributes.contactInformation,
|
||||
socials: hotelAttributes.socialMedia,
|
||||
ecoLabels: hotelAttributes.hotelFacts.ecoLabels,
|
||||
}
|
||||
}),
|
||||
availability: router({
|
||||
hotels: serviceProcedure
|
||||
.input(getHotelsAvailabilityInputSchema)
|
||||
|
||||
Reference in New Issue
Block a user