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( 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 }