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
126 lines
3.3 KiB
TypeScript
126 lines
3.3 KiB
TypeScript
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,
|
|
GetCityPageUrlsData,
|
|
} from "@/types/trpc/routers/contentstack/destinationCityPage"
|
|
import type { Lang } from "@/constants/languages"
|
|
|
|
export function generatePageTags(
|
|
validatedData: DestinationCityPageRefs,
|
|
lang: Lang
|
|
): string[] {
|
|
const connections = getConnections(validatedData)
|
|
return [
|
|
generateTagsFromSystem(lang, connections),
|
|
generateTag(lang, validatedData.destination_city_page.system.uid),
|
|
].flat()
|
|
}
|
|
|
|
export function getConnections({
|
|
destination_city_page,
|
|
}: DestinationCityPageRefs) {
|
|
const connections: System["system"][] = [destination_city_page.system]
|
|
if (destination_city_page.blocks) {
|
|
destination_city_page.blocks.forEach((block) => {
|
|
switch (block.__typename) {
|
|
case DestinationCityPageEnum.ContentStack.blocks.Accordion: {
|
|
if (block.accordion.length) {
|
|
connections.push(...block.accordion)
|
|
}
|
|
break
|
|
}
|
|
case DestinationCityPageEnum.ContentStack.blocks.Content:
|
|
{
|
|
if (block.content.length) {
|
|
// TS has trouble infering the filtered types
|
|
// @ts-ignore
|
|
connections.push(...block.content)
|
|
}
|
|
}
|
|
break
|
|
}
|
|
})
|
|
}
|
|
if (destination_city_page.sidepeek_content) {
|
|
destination_city_page.sidepeek_content.content.embedded_itemsConnection.edges.forEach(
|
|
({ node }) => {
|
|
connections.push(node.system)
|
|
}
|
|
)
|
|
}
|
|
|
|
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
|
|
}
|