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
193 lines
4.8 KiB
TypeScript
193 lines
4.8 KiB
TypeScript
import { GetHotelPageRefs } from "@/lib/graphql/Query/HotelPage/HotelPage.graphql"
|
|
import { GetHotelPageUrls } from "@/lib/graphql/Query/HotelPage/HotelPageUrl.graphql"
|
|
import { request } from "@/lib/graphql/request"
|
|
import { notFound } from "@/server/errors/trpc"
|
|
|
|
import { generateTag, generateTagsFromSystem } from "@/utils/generateTag"
|
|
|
|
import { hotelPageRefsSchema, hotelPageUrlsSchema } from "./output"
|
|
import {
|
|
getHotelPageRefsCounter,
|
|
getHotelPageRefsFailCounter,
|
|
getHotelPageRefsSuccessCounter,
|
|
getHotelPageUrlsCounter,
|
|
getHotelPageUrlsFailCounter,
|
|
getHotelPageUrlsSuccessCounter,
|
|
} from "./telemetry"
|
|
|
|
import { HotelPageEnum } from "@/types/enums/hotelPage"
|
|
import type { System } from "@/types/requests/system"
|
|
import type {
|
|
GetHotelPageRefsSchema,
|
|
GetHotelPageUrlsData,
|
|
HotelPageRefs,
|
|
} from "@/types/trpc/routers/contentstack/hotelPage"
|
|
import type { Lang } from "@/constants/languages"
|
|
|
|
export async function fetchHotelPageRefs(lang: Lang, uid: string) {
|
|
getHotelPageRefsCounter.add(1, { lang, uid })
|
|
console.info(
|
|
"contentstack.hotelPage.refs start",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
})
|
|
)
|
|
|
|
const refsResponse = await request<GetHotelPageRefsSchema>(
|
|
GetHotelPageRefs,
|
|
{ locale: lang, uid },
|
|
{
|
|
cache: "force-cache",
|
|
next: {
|
|
tags: [generateTag(lang, uid)],
|
|
},
|
|
}
|
|
)
|
|
if (!refsResponse.data) {
|
|
const notFoundError = notFound(refsResponse)
|
|
getHotelPageRefsFailCounter.add(1, {
|
|
lang,
|
|
uid,
|
|
error_type: "http_error",
|
|
error: JSON.stringify({
|
|
code: notFoundError.code,
|
|
}),
|
|
})
|
|
console.error(
|
|
"contentstack.hotelPage.refs not found error",
|
|
JSON.stringify({
|
|
query: {
|
|
lang,
|
|
uid,
|
|
},
|
|
error: { code: notFoundError.code },
|
|
})
|
|
)
|
|
throw notFoundError
|
|
}
|
|
return refsResponse.data
|
|
}
|
|
|
|
export function validateHotelPageRefs(
|
|
data: GetHotelPageRefsSchema,
|
|
lang: Lang,
|
|
uid: string
|
|
) {
|
|
const validatedData = hotelPageRefsSchema.safeParse(data)
|
|
if (!validatedData.success) {
|
|
getHotelPageRefsFailCounter.add(1, {
|
|
lang,
|
|
uid,
|
|
error_type: "validation_error",
|
|
error: JSON.stringify(validatedData.error),
|
|
})
|
|
console.error(
|
|
"contentstack.hotelPage.refs validation error",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
error: validatedData.error,
|
|
})
|
|
)
|
|
return null
|
|
}
|
|
getHotelPageRefsSuccessCounter.add(1, { lang, uid })
|
|
console.info(
|
|
"contentstack.hotelPage.refs success",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
})
|
|
)
|
|
|
|
return validatedData.data
|
|
}
|
|
|
|
export function generatePageTags(
|
|
validatedData: HotelPageRefs,
|
|
lang: Lang
|
|
): string[] {
|
|
const connections = getConnections(validatedData)
|
|
return [
|
|
generateTagsFromSystem(lang, connections),
|
|
generateTag(lang, validatedData.hotel_page.system.uid),
|
|
].flat()
|
|
}
|
|
|
|
export function getConnections({ hotel_page }: HotelPageRefs) {
|
|
const connections: System["system"][] = [hotel_page.system]
|
|
if (hotel_page.content) {
|
|
hotel_page.content.forEach((block) => {
|
|
switch (block.__typename) {
|
|
case HotelPageEnum.ContentStack.blocks.ActivitiesCard: {
|
|
if (block.upcoming_activities_card.length) {
|
|
connections.push(...block.upcoming_activities_card)
|
|
}
|
|
break
|
|
}
|
|
}
|
|
if (hotel_page.faq) {
|
|
connections.push(...hotel_page.faq)
|
|
}
|
|
})
|
|
}
|
|
return connections
|
|
}
|
|
|
|
export async function getHotelPageUrls(lang: Lang) {
|
|
getHotelPageUrlsCounter.add(1, { lang })
|
|
console.info(
|
|
"contentstack.hotelPageUrls start",
|
|
JSON.stringify({ query: { lang } })
|
|
)
|
|
const tags = [`${lang}:hotel_page_urls`]
|
|
const response = await request<GetHotelPageUrlsData>(
|
|
GetHotelPageUrls,
|
|
{
|
|
locale: lang,
|
|
},
|
|
{
|
|
cache: "force-cache",
|
|
next: {
|
|
tags,
|
|
},
|
|
}
|
|
)
|
|
|
|
if (!response.data) {
|
|
getHotelPageUrlsFailCounter.add(1, {
|
|
lang,
|
|
error_type: "not_found",
|
|
error: `Hotel pages not found for lang: ${lang}`,
|
|
})
|
|
console.error(
|
|
"contentstack.hotelPageUrls not found error",
|
|
JSON.stringify({ query: { lang } })
|
|
)
|
|
return []
|
|
}
|
|
|
|
const validatedHotelPageUrls = hotelPageUrlsSchema.safeParse(response.data)
|
|
|
|
if (!validatedHotelPageUrls.success) {
|
|
getHotelPageUrlsFailCounter.add(1, {
|
|
lang,
|
|
error_type: "validation_error",
|
|
error: JSON.stringify(validatedHotelPageUrls.error),
|
|
})
|
|
console.error(
|
|
"contentstack.hotelPageUrls validation error",
|
|
JSON.stringify({
|
|
query: { lang },
|
|
error: validatedHotelPageUrls.error,
|
|
})
|
|
)
|
|
return []
|
|
}
|
|
getHotelPageUrlsSuccessCounter.add(1, { lang })
|
|
console.info(
|
|
"contentstack.hotelPageUrl success",
|
|
JSON.stringify({ query: { lang } })
|
|
)
|
|
|
|
return validatedHotelPageUrls.data
|
|
}
|