Merged in fix/SW-1754-overview-page-rate-limit (pull request #1412)
fix(SW-1754): Fix rate limit issue on Destination Overview Page * fix(SW-1754): Fix rate limit issue on Destination Overview Page Approved-by: Matilda Landström
This commit is contained in:
@@ -185,6 +185,56 @@ export const destinationCityPageSchema = z
|
||||
}
|
||||
})
|
||||
|
||||
export const cityPageUrlsSchema = z
|
||||
.object({
|
||||
all_destination_city_page: z.object({
|
||||
items: z.array(
|
||||
z
|
||||
.object({
|
||||
url: z.string(),
|
||||
destination_settings: z
|
||||
.object({
|
||||
city_denmark: z.string().optional().nullable(),
|
||||
city_finland: z.string().optional().nullable(),
|
||||
city_germany: z.string().optional().nullable(),
|
||||
city_poland: z.string().optional().nullable(),
|
||||
city_norway: z.string().optional().nullable(),
|
||||
city_sweden: z.string().optional().nullable(),
|
||||
})
|
||||
.transform(
|
||||
({
|
||||
city_denmark,
|
||||
city_finland,
|
||||
city_germany,
|
||||
city_norway,
|
||||
city_poland,
|
||||
city_sweden,
|
||||
}) => {
|
||||
const cities = [
|
||||
city_denmark,
|
||||
city_finland,
|
||||
city_germany,
|
||||
city_poland,
|
||||
city_norway,
|
||||
city_sweden,
|
||||
].filter((city): city is string => Boolean(city))
|
||||
|
||||
return { city: cities[0] }
|
||||
}
|
||||
),
|
||||
system: systemSchema,
|
||||
})
|
||||
.transform((data) => {
|
||||
return {
|
||||
city: data.destination_settings.city,
|
||||
url: removeMultipleSlashes(`/${data.system.locale}/${data.url}`),
|
||||
}
|
||||
})
|
||||
),
|
||||
}),
|
||||
})
|
||||
.transform(({ all_destination_city_page }) => all_destination_city_page.items)
|
||||
|
||||
/** REFS */
|
||||
const destinationCityPageContentRefs = z
|
||||
.object({
|
||||
|
||||
@@ -21,3 +21,13 @@ export const getDestinationCityPageSuccessCounter = meter.createCounter(
|
||||
export const getDestinationCityPageFailCounter = meter.createCounter(
|
||||
"trpc.contentstack.destinationCityPage.get-fail"
|
||||
)
|
||||
|
||||
export const getCityPageUrlsCounter = meter.createCounter(
|
||||
"trpc.contentstack.cityPageUrls.get"
|
||||
)
|
||||
export const getCityPageUrlsSuccessCounter = meter.createCounter(
|
||||
"trpc.contentstack.cityPageUrls.get-success"
|
||||
)
|
||||
export const getCityPageUrlsFailCounter = meter.createCounter(
|
||||
"trpc.contentstack.cityPageUrls.get-fail"
|
||||
)
|
||||
|
||||
@@ -1,8 +1,21 @@
|
||||
import { GetCityPageUrls } from "@/lib/graphql/Query/DestinationCityPage/DestinationCityPageUrl.graphql"
|
||||
import { request } from "@/lib/graphql/request"
|
||||
|
||||
import { generateTag, generateTagsFromSystem } from "@/utils/generateTag"
|
||||
|
||||
import { cityPageUrlsSchema } from "./output"
|
||||
import {
|
||||
getCityPageUrlsCounter,
|
||||
getCityPageUrlsFailCounter,
|
||||
getCityPageUrlsSuccessCounter,
|
||||
} from "./telemetry"
|
||||
|
||||
import { DestinationCityPageEnum } from "@/types/enums/destinationCityPage"
|
||||
import type { System } from "@/types/requests/system"
|
||||
import type { DestinationCityPageRefs } from "@/types/trpc/routers/contentstack/destinationCityPage"
|
||||
import type {
|
||||
DestinationCityPageRefs,
|
||||
GetCityPageUrlsData,
|
||||
} from "@/types/trpc/routers/contentstack/destinationCityPage"
|
||||
import type { Lang } from "@/constants/languages"
|
||||
|
||||
export function generatePageTags(
|
||||
@@ -51,3 +64,62 @@ export function getConnections({
|
||||
|
||||
return connections
|
||||
}
|
||||
|
||||
export async function getCityPageUrls(lang: Lang) {
|
||||
getCityPageUrlsCounter.add(1, { lang })
|
||||
console.info(
|
||||
"contentstack.cityPageUrls start",
|
||||
JSON.stringify({ query: { lang } })
|
||||
)
|
||||
const tag = `${lang}:city_page_urls`
|
||||
const response = await request<GetCityPageUrlsData>(
|
||||
GetCityPageUrls,
|
||||
{
|
||||
locale: lang,
|
||||
},
|
||||
{
|
||||
cache: "force-cache",
|
||||
next: {
|
||||
tags: [tag],
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
if (!response.data) {
|
||||
getCityPageUrlsFailCounter.add(1, {
|
||||
lang,
|
||||
error_type: "not_found",
|
||||
error: `Destination city pages not found for lang: ${lang}`,
|
||||
})
|
||||
console.error(
|
||||
"contentstack.cityPageUrls not found error",
|
||||
JSON.stringify({ query: { lang } })
|
||||
)
|
||||
return []
|
||||
}
|
||||
|
||||
const validatedResponse = cityPageUrlsSchema.safeParse(response.data)
|
||||
|
||||
if (!validatedResponse.success) {
|
||||
getCityPageUrlsFailCounter.add(1, {
|
||||
lang,
|
||||
error_type: "validation_error",
|
||||
error: JSON.stringify(validatedResponse.error),
|
||||
})
|
||||
console.error(
|
||||
"contentstack.cityPageUrls validation error",
|
||||
JSON.stringify({
|
||||
query: { lang },
|
||||
error: validatedResponse.error,
|
||||
})
|
||||
)
|
||||
return []
|
||||
}
|
||||
getCityPageUrlsSuccessCounter.add(1, { lang })
|
||||
console.info(
|
||||
"contentstack.cityPageUrls success",
|
||||
JSON.stringify({ query: { lang } })
|
||||
)
|
||||
|
||||
return validatedResponse.data
|
||||
}
|
||||
|
||||
@@ -2,6 +2,8 @@ import { z } from "zod"
|
||||
|
||||
import { discriminatedUnionArray } from "@/lib/discriminatedUnion"
|
||||
|
||||
import { removeMultipleSlashes } from "@/utils/url"
|
||||
|
||||
import {
|
||||
accordionRefsSchema,
|
||||
accordionSchema,
|
||||
@@ -120,6 +122,31 @@ export const destinationCountryPageSchema = z
|
||||
}
|
||||
})
|
||||
|
||||
export const countryPageUrlsSchema = z
|
||||
.object({
|
||||
all_destination_country_page: z.object({
|
||||
items: z.array(
|
||||
z
|
||||
.object({
|
||||
url: z.string(),
|
||||
destination_settings: z.object({
|
||||
country: z.string(),
|
||||
}),
|
||||
system: systemSchema,
|
||||
})
|
||||
.transform((data) => {
|
||||
return {
|
||||
country: data.destination_settings.country,
|
||||
url: removeMultipleSlashes(`/${data.system.locale}/${data.url}`),
|
||||
}
|
||||
})
|
||||
),
|
||||
}),
|
||||
})
|
||||
.transform(
|
||||
({ all_destination_country_page }) => all_destination_country_page.items
|
||||
)
|
||||
|
||||
/** REFS */
|
||||
const destinationCountryPageContentRefs = z
|
||||
.object({
|
||||
|
||||
@@ -31,3 +31,15 @@ export const getCityListDataSuccessCounter = meter.createCounter(
|
||||
export const getCityListDataFailCounter = meter.createCounter(
|
||||
"trpc.contentstack.cityListData.get-fail"
|
||||
)
|
||||
|
||||
export const getCountryPageUrlsCounter = meter.createCounter(
|
||||
"trpc.contentstack.getCountryPageUrls"
|
||||
)
|
||||
|
||||
export const getCountryPageUrlsSuccessCounter = meter.createCounter(
|
||||
"trpc.contentstack.getCountryPageUrls-success"
|
||||
)
|
||||
|
||||
export const getCountryPageUrlsFailCounter = meter.createCounter(
|
||||
"trpc.contentstack.getCountryPageUrls-fail"
|
||||
)
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { env } from "@/env/server"
|
||||
import { GetDestinationCityListData } from "@/lib/graphql/Query/DestinationCityPage/DestinationCityListData.graphql"
|
||||
import { GetCountryPageUrls } from "@/lib/graphql/Query/DestinationCountryPage/DestinationCountryPageUrl.graphql"
|
||||
import { request } from "@/lib/graphql/request"
|
||||
import { toApiLang } from "@/server/utils"
|
||||
|
||||
@@ -7,10 +8,14 @@ import { generateTag, generateTagsFromSystem } from "@/utils/generateTag"
|
||||
|
||||
import { getCitiesByCountry } from "../../hotels/utils"
|
||||
import { destinationCityListDataSchema } from "../destinationCityPage/output"
|
||||
import { countryPageUrlsSchema } from "./output"
|
||||
import {
|
||||
getCityListDataCounter,
|
||||
getCityListDataFailCounter,
|
||||
getCityListDataSuccessCounter,
|
||||
getCountryPageUrlsCounter,
|
||||
getCountryPageUrlsFailCounter,
|
||||
getCountryPageUrlsSuccessCounter,
|
||||
} from "./telemetry"
|
||||
|
||||
import { ApiCountry, type Country } from "@/types/enums/country"
|
||||
@@ -18,7 +23,10 @@ import { DestinationCountryPageEnum } from "@/types/enums/destinationCountryPage
|
||||
import type { RequestOptionsWithOutBody } from "@/types/fetch"
|
||||
import type { System } from "@/types/requests/system"
|
||||
import type { GetDestinationCityListDataResponse } from "@/types/trpc/routers/contentstack/destinationCityPage"
|
||||
import type { DestinationCountryPageRefs } from "@/types/trpc/routers/contentstack/destinationCountryPage"
|
||||
import type {
|
||||
DestinationCountryPageRefs,
|
||||
GetCountryPageUrlsData,
|
||||
} from "@/types/trpc/routers/contentstack/destinationCountryPage"
|
||||
import type { Lang } from "@/constants/languages"
|
||||
|
||||
export function generatePageTags(
|
||||
@@ -178,3 +186,66 @@ export async function getCityPages(
|
||||
.flat()
|
||||
.filter((city): city is NonNullable<typeof city> => !!city)
|
||||
}
|
||||
|
||||
export async function getCountryPageUrls(lang: Lang) {
|
||||
getCountryPageUrlsCounter.add(1, { lang })
|
||||
console.info(
|
||||
"contentstack.countryPageUrls start",
|
||||
JSON.stringify({ query: { lang } })
|
||||
)
|
||||
|
||||
const tag = `${lang}:country_page_urls`
|
||||
const response = await request<GetCountryPageUrlsData>(
|
||||
GetCountryPageUrls,
|
||||
{
|
||||
locale: lang,
|
||||
},
|
||||
{
|
||||
cache: "force-cache",
|
||||
next: {
|
||||
tags: [tag],
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
if (!response.data) {
|
||||
getCountryPageUrlsFailCounter.add(1, {
|
||||
lang,
|
||||
error_type: "not_found",
|
||||
error: `Country pages not found for lang: ${lang}`,
|
||||
})
|
||||
console.error(
|
||||
"contentstack.countryPageUrls not found error",
|
||||
JSON.stringify({ query: { lang } })
|
||||
)
|
||||
return []
|
||||
}
|
||||
|
||||
const validatedCountryPageUrls = countryPageUrlsSchema.safeParse(
|
||||
response.data
|
||||
)
|
||||
|
||||
if (!validatedCountryPageUrls.success) {
|
||||
getCountryPageUrlsFailCounter.add(1, {
|
||||
lang,
|
||||
error_type: "validation_error",
|
||||
error: JSON.stringify(validatedCountryPageUrls.error),
|
||||
})
|
||||
console.error(
|
||||
"contentstack.countryPageUrls validation error",
|
||||
JSON.stringify({
|
||||
query: { lang },
|
||||
error: validatedCountryPageUrls.error,
|
||||
})
|
||||
)
|
||||
return []
|
||||
}
|
||||
|
||||
getCountryPageUrlsSuccessCounter.add(1, { lang })
|
||||
console.info(
|
||||
"contentstack.countryPageUrls success",
|
||||
JSON.stringify({ query: { lang } })
|
||||
)
|
||||
|
||||
return validatedCountryPageUrls.data
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@ import { z } from "zod"
|
||||
|
||||
import { discriminatedUnionArray } from "@/lib/discriminatedUnion"
|
||||
|
||||
import { removeMultipleSlashes } from "@/utils/url"
|
||||
|
||||
import {
|
||||
cardGalleryRefsSchema,
|
||||
cardGallerySchema,
|
||||
@@ -40,27 +38,6 @@ export const destinationOverviewPageSchema = z.object({
|
||||
}),
|
||||
})
|
||||
|
||||
export const countryPageUrlSchema = z
|
||||
.object({
|
||||
all_destination_country_page: z.object({
|
||||
items: z.array(
|
||||
z
|
||||
.object({
|
||||
url: z.string(),
|
||||
system: systemSchema,
|
||||
})
|
||||
.transform((data) => {
|
||||
return {
|
||||
url: removeMultipleSlashes(`/${data.system.locale}/${data.url}`),
|
||||
}
|
||||
})
|
||||
),
|
||||
}),
|
||||
})
|
||||
.transform(
|
||||
({ all_destination_country_page }) => all_destination_country_page.items[0]
|
||||
)
|
||||
|
||||
/** REFS */
|
||||
const destinationOverviewPageCardGalleryRef = z
|
||||
.object({
|
||||
|
||||
@@ -19,7 +19,8 @@ import {
|
||||
getCountries,
|
||||
getHotelIdsByCityId,
|
||||
} from "../../hotels/utils"
|
||||
import { getCityListDataByCityIdentifier } from "../destinationCountryPage/utils"
|
||||
import { getCityPageUrls } from "../destinationCityPage/utils"
|
||||
import { getCountryPageUrls } from "../destinationCountryPage/utils"
|
||||
import {
|
||||
destinationOverviewPageRefsSchema,
|
||||
destinationOverviewPageSchema,
|
||||
@@ -32,7 +33,6 @@ import {
|
||||
getDestinationOverviewPageRefsSuccessCounter,
|
||||
getDestinationOverviewPageSuccessCounter,
|
||||
} from "./telemetry"
|
||||
import { getCountryPageUrl } from "./utils"
|
||||
|
||||
import type { DestinationsData } from "@/types/components/destinationOverviewPage/destinationsList/destinationsData"
|
||||
import {
|
||||
@@ -220,8 +220,8 @@ export const destinationOverviewPageQueryRouter = router({
|
||||
revalidate: env.CACHE_TIME_HOTELS,
|
||||
},
|
||||
}
|
||||
|
||||
const countries = await getCountries(options, params, ctx.lang)
|
||||
const countryPages = await getCountryPageUrls(ctx.lang)
|
||||
|
||||
if (!countries) {
|
||||
return null
|
||||
@@ -237,6 +237,8 @@ export const destinationOverviewPageQueryRouter = router({
|
||||
true
|
||||
)
|
||||
|
||||
const cityPages = await getCityPageUrls(ctx.lang)
|
||||
|
||||
const destinations: DestinationsData = await Promise.all(
|
||||
Object.entries(citiesByCountry).map(async ([country, cities]) => {
|
||||
const citiesWithHotelCount = await Promise.all(
|
||||
@@ -252,29 +254,27 @@ export const destinationOverviewPageQueryRouter = router({
|
||||
hotelIdsParams
|
||||
)
|
||||
|
||||
let cityUrl
|
||||
if (city.cityIdentifier) {
|
||||
cityUrl = await getCityListDataByCityIdentifier(
|
||||
ctx.lang,
|
||||
city.cityIdentifier
|
||||
)
|
||||
}
|
||||
const cityPage = cityPages.find(
|
||||
(cityPage) => cityPage.city === city.cityIdentifier
|
||||
)
|
||||
|
||||
return {
|
||||
id: city.id,
|
||||
name: city.name,
|
||||
hotelIds: hotels,
|
||||
hotelCount: hotels?.length ?? 0,
|
||||
url: cityUrl?.url,
|
||||
url: cityPage?.url,
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
const countryUrl = await getCountryPageUrl(ctx.lang, country)
|
||||
const countryPage = countryPages.find(
|
||||
(countryPage) => countryPage.country === country
|
||||
)
|
||||
|
||||
return {
|
||||
country,
|
||||
countryUrl: countryUrl?.url,
|
||||
countryUrl: countryPage?.url,
|
||||
numberOfHotels: citiesWithHotelCount.reduce(
|
||||
(acc, city) => acc + city.hotelCount,
|
||||
0
|
||||
|
||||
@@ -21,15 +21,3 @@ export const getDestinationOverviewPageSuccessCounter = meter.createCounter(
|
||||
export const getDestinationOverviewPageFailCounter = meter.createCounter(
|
||||
"trpc.contentstack.destinationOverviewPage.get-fail"
|
||||
)
|
||||
|
||||
export const getCountryPageUrlCounter = meter.createCounter(
|
||||
"trpc.contentstack.getCountryPageUrl"
|
||||
)
|
||||
|
||||
export const getCountryPageUrlSuccessCounter = meter.createCounter(
|
||||
"trpc.contentstack.getCountryPageUrl-success"
|
||||
)
|
||||
|
||||
export const getCountryPageUrlFailCounter = meter.createCounter(
|
||||
"trpc.contentstack.getCountryPageUrl-fail"
|
||||
)
|
||||
|
||||
@@ -1,76 +0,0 @@
|
||||
import { GetCountryPageUrl } from "@/lib/graphql/Query/DestinationOverviewPage/DestinationOverviewPage.graphql"
|
||||
import { request } from "@/lib/graphql/request"
|
||||
|
||||
import { countryPageUrlSchema } from "./output"
|
||||
import {
|
||||
getCountryPageUrlCounter,
|
||||
getCountryPageUrlFailCounter,
|
||||
getCountryPageUrlSuccessCounter,
|
||||
} from "./telemetry"
|
||||
|
||||
import type { GetCountryPageUrlData } from "@/types/trpc/routers/contentstack/destinationOverviewPage"
|
||||
import type { Lang } from "@/constants/languages"
|
||||
|
||||
export async function getCountryPageUrl(lang: Lang, country: string) {
|
||||
getCountryPageUrlCounter.add(1, { lang, country })
|
||||
console.info(
|
||||
"contentstack.countryPageUrl start",
|
||||
JSON.stringify({ query: { lang, country } })
|
||||
)
|
||||
|
||||
const tag = `${lang}:country_page_url:${country}`
|
||||
const response = await request<GetCountryPageUrlData>(
|
||||
GetCountryPageUrl,
|
||||
{
|
||||
locale: lang,
|
||||
country,
|
||||
},
|
||||
{
|
||||
cache: "force-cache",
|
||||
next: {
|
||||
tags: [tag],
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
if (!response.data) {
|
||||
getCountryPageUrlFailCounter.add(1, {
|
||||
lang,
|
||||
country,
|
||||
error_type: "not_found",
|
||||
error: `Country page not found for country: ${country}`,
|
||||
})
|
||||
console.error(
|
||||
"contentstack.countryPageUrl not found error",
|
||||
JSON.stringify({ query: { lang, country } })
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
const validatedCountryPageUrl = countryPageUrlSchema.safeParse(response.data)
|
||||
|
||||
if (!validatedCountryPageUrl.success) {
|
||||
getCountryPageUrlFailCounter.add(1, {
|
||||
lang,
|
||||
country,
|
||||
error_type: "validation_error",
|
||||
error: JSON.stringify(validatedCountryPageUrl.error),
|
||||
})
|
||||
console.error(
|
||||
"contentstack.countryPageUrl validation error",
|
||||
JSON.stringify({
|
||||
query: { lang, country },
|
||||
error: validatedCountryPageUrl.error,
|
||||
})
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
getCountryPageUrlSuccessCounter.add(1, { lang, country })
|
||||
console.info(
|
||||
"contentstack.countryPageUrl success",
|
||||
JSON.stringify({ query: { lang, country } })
|
||||
)
|
||||
|
||||
return validatedCountryPageUrl.data
|
||||
}
|
||||
@@ -116,25 +116,23 @@ export const hotelPageRefsSchema = z.object({
|
||||
}),
|
||||
})
|
||||
|
||||
export const hotelPageUrlSchema = z
|
||||
export const hotelPageUrlsSchema = z
|
||||
.object({
|
||||
all_hotel_page: z.object({
|
||||
items: z
|
||||
.array(
|
||||
z.object({
|
||||
items: z.array(
|
||||
z
|
||||
.object({
|
||||
url: z.string(),
|
||||
hotel_page_id: z.string(),
|
||||
system: systemSchema,
|
||||
})
|
||||
)
|
||||
.max(1),
|
||||
.transform((data) => {
|
||||
return {
|
||||
url: removeMultipleSlashes(`/${data.system.locale}/${data.url}`),
|
||||
hotelId: data.hotel_page_id,
|
||||
}
|
||||
})
|
||||
),
|
||||
}),
|
||||
})
|
||||
.transform((data) => {
|
||||
const page = data.all_hotel_page.items[0]
|
||||
if (!page) {
|
||||
return null
|
||||
}
|
||||
|
||||
const lang = page.system.locale
|
||||
return removeMultipleSlashes(`/${lang}/${page.url}`)
|
||||
})
|
||||
.transform(({ all_hotel_page }) => all_hotel_page.items)
|
||||
|
||||
@@ -22,12 +22,12 @@ export const getHotelPageFailCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPage.get-fail"
|
||||
)
|
||||
|
||||
export const getHotelPageUrlCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPageUrl.get"
|
||||
export const getHotelPageUrlsCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPageUrls.get"
|
||||
)
|
||||
export const getHotelPageUrlSuccessCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPageUrl.get-success"
|
||||
export const getHotelPageUrlsSuccessCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPageUrls.get-success"
|
||||
)
|
||||
export const getHotelPageUrlFailCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPageUrl.get-fail"
|
||||
export const getHotelPageUrlsFailCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPageUrls.get-fail"
|
||||
)
|
||||
|
||||
@@ -1,29 +1,25 @@
|
||||
import { GetHotelPageRefs } from "@/lib/graphql/Query/HotelPage/HotelPage.graphql"
|
||||
import { GetHotelPageUrl } from "@/lib/graphql/Query/HotelPage/HotelPageUrl.graphql"
|
||||
import { GetHotelPageUrls } from "@/lib/graphql/Query/HotelPage/HotelPageUrl.graphql"
|
||||
import { request } from "@/lib/graphql/request"
|
||||
import { notFound } from "@/server/errors/trpc"
|
||||
|
||||
import {
|
||||
generateHotelUrlTag,
|
||||
generateTag,
|
||||
generateTagsFromSystem,
|
||||
} from "@/utils/generateTag"
|
||||
import { generateTag, generateTagsFromSystem } from "@/utils/generateTag"
|
||||
|
||||
import { hotelPageRefsSchema, hotelPageUrlSchema } from "./output"
|
||||
import { hotelPageRefsSchema, hotelPageUrlsSchema } from "./output"
|
||||
import {
|
||||
getHotelPageRefsCounter,
|
||||
getHotelPageRefsFailCounter,
|
||||
getHotelPageRefsSuccessCounter,
|
||||
getHotelPageUrlCounter,
|
||||
getHotelPageUrlFailCounter,
|
||||
getHotelPageUrlSuccessCounter,
|
||||
getHotelPageUrlsCounter,
|
||||
getHotelPageUrlsFailCounter,
|
||||
getHotelPageUrlsSuccessCounter,
|
||||
} from "./telemetry"
|
||||
|
||||
import { HotelPageEnum } from "@/types/enums/hotelPage"
|
||||
import type { System } from "@/types/requests/system"
|
||||
import type {
|
||||
GetHotelPageRefsSchema,
|
||||
GetHotelPageUrlData,
|
||||
GetHotelPageUrlsData,
|
||||
HotelPageRefs,
|
||||
} from "@/types/trpc/routers/contentstack/hotelPage"
|
||||
import type { Lang } from "@/constants/languages"
|
||||
@@ -136,63 +132,61 @@ export function getConnections({ hotel_page }: HotelPageRefs) {
|
||||
return connections
|
||||
}
|
||||
|
||||
export async function getHotelPageUrl(lang: Lang, hotelId: string) {
|
||||
getHotelPageUrlCounter.add(1, { lang, hotelId })
|
||||
export async function getHotelPageUrls(lang: Lang) {
|
||||
getHotelPageUrlsCounter.add(1, { lang })
|
||||
console.info(
|
||||
"contentstack.hotelPageUrl start",
|
||||
JSON.stringify({ query: { lang, hotelId } })
|
||||
"contentstack.hotelPageUrls start",
|
||||
JSON.stringify({ query: { lang } })
|
||||
)
|
||||
const response = await request<GetHotelPageUrlData>(
|
||||
GetHotelPageUrl,
|
||||
const tags = [`${lang}:hotel_page_urls`]
|
||||
const response = await request<GetHotelPageUrlsData>(
|
||||
GetHotelPageUrls,
|
||||
{
|
||||
locale: lang,
|
||||
hotelId,
|
||||
},
|
||||
{
|
||||
cache: "force-cache",
|
||||
next: {
|
||||
tags: [generateHotelUrlTag(lang, hotelId)],
|
||||
tags,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
if (!response.data) {
|
||||
getHotelPageUrlFailCounter.add(1, {
|
||||
getHotelPageUrlsFailCounter.add(1, {
|
||||
lang,
|
||||
hotelId,
|
||||
error_type: "not_found",
|
||||
error: `Hotel page not found for hotelId: ${hotelId}`,
|
||||
error: `Hotel pages not found for lang: ${lang}`,
|
||||
})
|
||||
console.error(
|
||||
"contentstack.hotelPageUrl not found error",
|
||||
JSON.stringify({ query: { lang, hotelId } })
|
||||
"contentstack.hotelPageUrls not found error",
|
||||
JSON.stringify({ query: { lang } })
|
||||
)
|
||||
return null
|
||||
return []
|
||||
}
|
||||
|
||||
const validatedHotelPageUrl = hotelPageUrlSchema.safeParse(response.data)
|
||||
const validatedHotelPageUrls = hotelPageUrlsSchema.safeParse(response.data)
|
||||
|
||||
if (!validatedHotelPageUrl.success) {
|
||||
getHotelPageUrlFailCounter.add(1, {
|
||||
if (!validatedHotelPageUrls.success) {
|
||||
getHotelPageUrlsFailCounter.add(1, {
|
||||
lang,
|
||||
hotelId,
|
||||
error_type: "validation_error",
|
||||
error: JSON.stringify(validatedHotelPageUrl.error),
|
||||
error: JSON.stringify(validatedHotelPageUrls.error),
|
||||
})
|
||||
console.error(
|
||||
"contentstack.hotelPageUrl validation error",
|
||||
"contentstack.hotelPageUrls validation error",
|
||||
JSON.stringify({
|
||||
query: { lang, hotelId },
|
||||
error: validatedHotelPageUrl.error,
|
||||
query: { lang },
|
||||
error: validatedHotelPageUrls.error,
|
||||
})
|
||||
)
|
||||
return null
|
||||
return []
|
||||
}
|
||||
getHotelPageUrlSuccessCounter.add(1, { lang, hotelId })
|
||||
getHotelPageUrlsSuccessCounter.add(1, { lang })
|
||||
console.info(
|
||||
"contentstack.hotelPageUrl success",
|
||||
JSON.stringify({ query: { lang, hotelId } })
|
||||
JSON.stringify({ query: { lang } })
|
||||
)
|
||||
|
||||
return validatedHotelPageUrl.data
|
||||
return validatedHotelPageUrls.data
|
||||
}
|
||||
|
||||
@@ -17,7 +17,7 @@ import { toApiLang } from "@/server/utils"
|
||||
import { generateChildrenString } from "@/components/HotelReservation/utils"
|
||||
import { cache } from "@/utils/cache"
|
||||
|
||||
import { getHotelPageUrl } from "../contentstack/hotelPage/utils"
|
||||
import { getHotelPageUrls } from "../contentstack/hotelPage/utils"
|
||||
import { getVerifiedUser, parsedUser } from "../user/query"
|
||||
import { additionalDataSchema } from "./schemas/hotel/include/additionalData"
|
||||
import { meetingRoomsSchema } from "./schemas/meetingRoom"
|
||||
@@ -1100,21 +1100,21 @@ export const hotelQueryRouter = router({
|
||||
)
|
||||
return []
|
||||
}
|
||||
|
||||
const hotelPages = await getHotelPageUrls(language)
|
||||
const hotels = await Promise.all(
|
||||
hotelsToFetch.map(async (hotelId) => {
|
||||
const [hotelData, url] = await Promise.all([
|
||||
getHotel(
|
||||
{ hotelId, isCardOnlyPayment: false, language },
|
||||
ctx.serviceToken
|
||||
),
|
||||
getHotelPageUrl(language, hotelId),
|
||||
])
|
||||
const hotelData = await getHotel(
|
||||
{ hotelId, isCardOnlyPayment: false, language },
|
||||
ctx.serviceToken
|
||||
)
|
||||
const hotelPage = hotelPages.find(
|
||||
(page) => page.hotelId === hotelId
|
||||
)
|
||||
|
||||
return hotelData
|
||||
? {
|
||||
...hotelData,
|
||||
url,
|
||||
url: hotelPage?.url ?? null,
|
||||
}
|
||||
: null
|
||||
})
|
||||
|
||||
@@ -6,7 +6,7 @@ import { env } from "@/env/server"
|
||||
import * as api from "@/lib/api"
|
||||
import { toApiLang } from "@/server/utils"
|
||||
|
||||
import { getHotelPageUrl } from "../contentstack/hotelPage/utils"
|
||||
import { getHotelPageUrls } from "../contentstack/hotelPage/utils"
|
||||
import { metrics } from "./metrics"
|
||||
import {
|
||||
citiesByCountrySchema,
|
||||
@@ -484,17 +484,15 @@ export async function getHotelsByHotelIds(
|
||||
lang: Lang,
|
||||
serviceToken: string
|
||||
) {
|
||||
const hotelPages = await getHotelPageUrls(lang)
|
||||
const hotels = await Promise.all(
|
||||
hotelIds.map(async (hotelId) => {
|
||||
const [hotelData, url] = await Promise.all([
|
||||
getHotel(
|
||||
{ hotelId, language: lang, isCardOnlyPayment: false },
|
||||
serviceToken
|
||||
),
|
||||
getHotelPageUrl(lang, hotelId),
|
||||
])
|
||||
|
||||
return hotelData ? { ...hotelData, url } : null
|
||||
const hotelData = await getHotel(
|
||||
{ hotelId, language: lang, isCardOnlyPayment: false },
|
||||
serviceToken
|
||||
)
|
||||
const hotelPage = hotelPages.find((page) => page.hotelId === hotelId)
|
||||
return hotelData ? { ...hotelData, url: hotelPage?.url ?? null } : null
|
||||
})
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user