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:
Erik Tiekstra
2025-02-25 14:40:31 +00:00
parent 3867baadd6
commit bc50dcf286
27 changed files with 426 additions and 244 deletions
@@ -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
}