Files
web/packages/trpc/lib/routers/contentstack/destinationCityPage/utils.ts
Joakim Jäderberg 8b94540d19 Merged in chore/redirect-counter (pull request #3302)
Counter name is now searchable and add counter for redirects

* refactor: createCounter() only takes one argument, the name of the counter. Makes it easier to search for

* feat: add counter when we do a redirect from redirect-service


Approved-by: Linus Flood
2025-12-08 10:24:05 +00:00

152 lines
4.5 KiB
TypeScript

import { createCounter } from "@scandic-hotels/common/telemetry"
import { GetCityPageCount } from "../../../graphql/Query/DestinationCityPage/DestinationCityPageCount.graphql"
import { GetCityPageUrls } from "../../../graphql/Query/DestinationCityPage/DestinationCityPageUrl.graphql"
import { request } from "../../../graphql/request"
import { DestinationCityPageEnum } from "../../../types/destinationCityPage"
import { generateTag, generateTagsFromSystem } from "../../../utils/generateTag"
import { batchedCityPageUrlsSchema, cityPageCountSchema } from "./output"
import type { Lang } from "@scandic-hotels/common/constants/language"
import type {
DestinationCityPageRefs,
GetCityPageCountData,
GetCityPageUrlsData,
} from "../../../types/destinationCityPage"
import type { System } from "../schemas/system"
export function generatePageTags(
validatedData: DestinationCityPageRefs,
lang: Lang
): string[] {
const connections = getConnections(validatedData)
return [
// This tag is added for the city list data on country pages to invalidate the list when city page changes.
generateTag(
lang,
`city_list_data:${validatedData.destination_city_page.destination_settings.city}`
),
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.filter((c) => !!c))
}
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 }) => {
if (node.system) {
connections.push(node.system)
}
}
)
}
return connections
}
export async function getCityPageCount(lang: Lang) {
const getCityPageCountCounter = createCounter(
"trpc.contentstack.cityPageCount.get"
)
const metricsGetCityPageCount = getCityPageCountCounter.init({ lang })
metricsGetCityPageCount.start()
const response = await request<GetCityPageCountData>(
GetCityPageCount,
{
locale: lang,
},
{
key: `${lang}:city_page_count`,
ttl: "max",
}
)
if (!response.data) {
metricsGetCityPageCount.dataError(
`Failed to get city pages count for ${lang}`
)
return 0
}
const validatedResponse = cityPageCountSchema.safeParse(response.data)
if (!validatedResponse.success) {
metricsGetCityPageCount.validationError(validatedResponse.error)
return 0
}
metricsGetCityPageCount.success()
return validatedResponse.data
}
export async function getCityPageUrls(lang: Lang) {
const getCityPageUrlsCounter = createCounter(
"trpc.contentstack.cityPageUrls.get"
)
const metricsGetCityPageUrls = getCityPageUrlsCounter.init({ lang })
metricsGetCityPageUrls.start()
const count = await getCityPageCount(lang)
if (count === 0) {
return []
}
// Calculating the amount of requests needed to fetch all pages.
// Contentstack has a limit of 100 items per request.
// So we need to make multiple requests to fetch urls to all pages.
// The `batchRequest` function is not working here, because the arrayMerge is
// used for other purposes.
const amountOfRequests = Math.ceil(count / 100)
const batchedResponse = await Promise.all(
Array.from({ length: amountOfRequests }).map((_, i) =>
request<GetCityPageUrlsData>(
GetCityPageUrls,
{ locale: lang, skip: i * 100 },
{ key: `${lang}:city_page_urls_batch_${i}`, ttl: "max" }
)
)
)
const validatedResponse = batchedCityPageUrlsSchema.safeParse(batchedResponse)
if (!validatedResponse.success) {
metricsGetCityPageUrls.validationError(validatedResponse.error)
return []
}
metricsGetCityPageUrls.success()
return validatedResponse.data
}