From 19bb9652981955ca694eb029db204a252e3262ef Mon Sep 17 00:00:00 2001 From: Erik Tiekstra Date: Mon, 10 Mar 2025 09:06:19 +0000 Subject: [PATCH] Merged in feat/SW-1745-language-switcher-non-contentstack-pages (pull request #1501) feat(SW-1745): Query for language switcher returns just current pathname for non-contentstack pages except for hotelreservation paths * feat(SW-1745): Query for language switcher returns just current pathname for non-contentstack pages except for hotelreservation paths Approved-by: Linus Flood --- apps/scandic-web/i18n/dictionaries/da.json | 1 + apps/scandic-web/i18n/dictionaries/de.json | 1 + apps/scandic-web/i18n/dictionaries/en.json | 1 + apps/scandic-web/i18n/dictionaries/fi.json | 1 + apps/scandic-web/i18n/dictionaries/no.json | 1 + apps/scandic-web/i18n/dictionaries/sv.json | 1 + .../lib/trpc/memoizedRequests/index.ts | 6 - .../contentstack/languageSwitcher/input.ts | 10 +- .../contentstack/languageSwitcher/query.ts | 266 ++---------------- .../languageSwitcher/telemetry.ts | 12 + .../contentstack/languageSwitcher/utils.ts | 227 +++++++++++++++ 11 files changed, 267 insertions(+), 260 deletions(-) create mode 100644 apps/scandic-web/server/routers/contentstack/languageSwitcher/telemetry.ts diff --git a/apps/scandic-web/i18n/dictionaries/da.json b/apps/scandic-web/i18n/dictionaries/da.json index 8ac929ca1..4545f9db2 100644 --- a/apps/scandic-web/i18n/dictionaries/da.json +++ b/apps/scandic-web/i18n/dictionaries/da.json @@ -61,6 +61,7 @@ "As our Close Friend": "Som vores nære ven", "As our {level}": "Som vores {level}", "As this is a multiroom stay, the cancellation has to be done by the person who made the booking. Please call 08-517 517 00 to talk to our customer service if you would need further assistance.": "Da dette er et ophold med flere værelser, skal annullereringen gennemføres af personen, der har booket opholdet. Bedes du ringe til vores kundeservice på 08-517 517 00, hvis du har brug for yderligere hjælp.", + "At a cost": "Mod betaling", "At latest": "Senest", "At the hotel": "På hotellet", "Attractions": "Attraktioner", diff --git a/apps/scandic-web/i18n/dictionaries/de.json b/apps/scandic-web/i18n/dictionaries/de.json index 3a13840a0..512b0524e 100644 --- a/apps/scandic-web/i18n/dictionaries/de.json +++ b/apps/scandic-web/i18n/dictionaries/de.json @@ -61,6 +61,7 @@ "As our Close Friend": "Als unser enger Freund", "As our {level}": "Als unser {level}", "As this is a multiroom stay, the cancellation has to be done by the person who made the booking. Please call 08-517 517 00 to talk to our customer service if you would need further assistance.": "Da dies ein Mehrzimmer-Aufenthalt ist, muss die Stornierung von der Person, die die Buchung getätigt hat, durchgeführt werden. Bitte rufen Sie uns unter der Telefonnummer 08-517 517 00 an, wenn Sie weitere Hilfe benötigen.", + "At a cost": "Gegen Gebühr", "At latest": "Spätestens", "At the hotel": "Im Hotel", "Attraction": "Attraktion", diff --git a/apps/scandic-web/i18n/dictionaries/en.json b/apps/scandic-web/i18n/dictionaries/en.json index 4fdbae580..797541365 100644 --- a/apps/scandic-web/i18n/dictionaries/en.json +++ b/apps/scandic-web/i18n/dictionaries/en.json @@ -64,6 +64,7 @@ "As our Close Friend": "As our Close Friend", "As our {level}": "As our {level}", "As this is a multiroom stay, the cancellation has to be done by the person who made the booking. Please call 08-517 517 00 to talk to our customer service if you would need further assistance.": "As this is a multiroom stay, the cancellation has to be done by the person who made the booking. Please call 08-517 517 00 to talk to our customer service if you would need further assistance.", + "At a cost": "At a cost", "At latest": "At latest", "At the hotel": "At the hotel", "Attractions": "Attractions", diff --git a/apps/scandic-web/i18n/dictionaries/fi.json b/apps/scandic-web/i18n/dictionaries/fi.json index eb75d5537..a30ff2257 100644 --- a/apps/scandic-web/i18n/dictionaries/fi.json +++ b/apps/scandic-web/i18n/dictionaries/fi.json @@ -60,6 +60,7 @@ "As our Close Friend": "Läheisenä ystävänämme", "As our {level}": "{level}-etu", "As this is a multiroom stay, the cancellation has to be done by the person who made the booking. Please call 08-517 517 00 to talk to our customer service if you would need further assistance.": "Koska tämä on monihuoneinen majoitus, peruutus on tehtävä henkilölle, joka teki varauksen. Ota yhteyttä asiakaspalveluun apua varten, jos tarvitset lisää apua.", + "At a cost": "Maksua vastaan", "At latest": "Viimeistään", "At the hotel": "Hotellissa", "Attractions": "Nähtävyydet", diff --git a/apps/scandic-web/i18n/dictionaries/no.json b/apps/scandic-web/i18n/dictionaries/no.json index ea059c61b..0255b91a4 100644 --- a/apps/scandic-web/i18n/dictionaries/no.json +++ b/apps/scandic-web/i18n/dictionaries/no.json @@ -60,6 +60,7 @@ "As our Close Friend": "Som vår nære venn", "As our {level}": "Som vår {level}", "As this is a multiroom stay, the cancellation has to be done by the person who made the booking. Please call 08-517 517 00 to talk to our customer service if you would need further assistance.": "Som dette er et ophold med flere rom, må annullereringen gjøres av personen som booket opholdet. Vennligst ring 08-517 517 00 til vår kundeservice hvis du trenger mer hjelp.", + "At a cost": "Mot betaling", "At latest": "Senest", "At the hotel": "På hotellet", "Attractions": "Attraksjoner", diff --git a/apps/scandic-web/i18n/dictionaries/sv.json b/apps/scandic-web/i18n/dictionaries/sv.json index 8b0a02c3b..50f9b359e 100644 --- a/apps/scandic-web/i18n/dictionaries/sv.json +++ b/apps/scandic-web/i18n/dictionaries/sv.json @@ -60,6 +60,7 @@ "As our Close Friend": "Som vår nära vän", "As our {level}": "Som vår {level}", "As this is a multiroom stay, the cancellation has to be done by the person who made the booking. Please call 08-517 517 00 to talk to our customer service if you would need further assistance.": "Då detta är en vistelse med flera rum måste avbokningen göras av personen som bokade vistelsen. Kontakta vår kundsupport på 08-517 517 00 om du behöver mer hjälp.", + "At a cost": "Mot en kostnad", "At latest": "Senast", "At the hotel": "På hotellet", "Attractions": "Sevärdheter", diff --git a/apps/scandic-web/lib/trpc/memoizedRequests/index.ts b/apps/scandic-web/lib/trpc/memoizedRequests/index.ts index 39cf0306b..b56916508 100644 --- a/apps/scandic-web/lib/trpc/memoizedRequests/index.ts +++ b/apps/scandic-web/lib/trpc/memoizedRequests/index.ts @@ -112,12 +112,6 @@ export const getCurrentFooter = cache(async function getMemoizedCurrentFooter( return serverClient().contentstack.base.currentFooter({ lang }) }) -export const getLanguageSwitcher = cache( - async function getMemoizedLanguageSwitcher() { - return serverClient().contentstack.languageSwitcher.get() - } -) - export const getSiteConfig = cache(async function getMemoizedSiteConfig( lang: Lang ) { diff --git a/apps/scandic-web/server/routers/contentstack/languageSwitcher/input.ts b/apps/scandic-web/server/routers/contentstack/languageSwitcher/input.ts index 0c6ac4c25..39f056b95 100644 --- a/apps/scandic-web/server/routers/contentstack/languageSwitcher/input.ts +++ b/apps/scandic-web/server/routers/contentstack/languageSwitcher/input.ts @@ -2,9 +2,7 @@ import { z } from "zod" import { Lang } from "@/constants/languages" -export const getLanguageSwitcherInput = z - .object({ - lang: z.nativeEnum(Lang), - pathName: z.string(), - }) - .optional() +export const getLanguageSwitcherInput = z.object({ + lang: z.nativeEnum(Lang), + pathName: z.string(), +}) diff --git a/apps/scandic-web/server/routers/contentstack/languageSwitcher/query.ts b/apps/scandic-web/server/routers/contentstack/languageSwitcher/query.ts index 003e3ac6e..9ea090333 100644 --- a/apps/scandic-web/server/routers/contentstack/languageSwitcher/query.ts +++ b/apps/scandic-web/server/routers/contentstack/languageSwitcher/query.ts @@ -1,277 +1,47 @@ -import { metrics } from "@opentelemetry/api" - -import { Lang } from "@/constants/languages" import { baseUrls } from "@/constants/routes/baseUrls" import { findMyBooking } from "@/constants/routes/findMyBooking" -import { batchRequest } from "@/lib/graphql/batchRequest" -import { - GetDaDeEnUrlsAccountPage, - GetFiNoSvUrlsAccountPage, -} from "@/lib/graphql/Query/AccountPage/AccountPage.graphql" -import { - GetDaDeEnUrlsCollectionPage, - GetFiNoSvUrlsCollectionPage, -} from "@/lib/graphql/Query/CollectionPage/CollectionPage.graphql" -import { - GetDaDeEnUrlsContentPage, - GetFiNoSvUrlsContentPage, -} from "@/lib/graphql/Query/ContentPage/ContentPage.graphql" -import { - GetDaDeEnUrlsCurrentBlocksPage, - GetFiNoSvUrlsCurrentBlocksPage, -} from "@/lib/graphql/Query/Current/LanguageSwitcher.graphql" -import { - GetDaDeEnUrlsDestinationCityPage, - GetFiNoSvUrlsDestinationCityPage, -} from "@/lib/graphql/Query/DestinationCityPage/DestinationCityPage.graphql" -import { - GetDaDeEnUrlsDestinationCountryPage, - GetFiNoSvUrlsDestinationCountryPage, -} from "@/lib/graphql/Query/DestinationCountryPage/DestinationCountryPage.graphql" -import { - GetDaDeEnUrlsDestinationOverviewPage, - GetFiNoSvUrlsDestinationOverviewPage, -} from "@/lib/graphql/Query/DestinationOverviewPage/DestinationOverviewPage.graphql" -import { - GetDaDeEnUrlsHotelPage, - GetFiNoSvUrlsHotelPage, -} from "@/lib/graphql/Query/HotelPage/HotelPage.graphql" -import { - GetDaDeEnUrlsLoyaltyPage, - GetFiNoSvUrlsLoyaltyPage, -} from "@/lib/graphql/Query/LoyaltyPage/LoyaltyPage.graphql" -import { - GetDaDeEnUrlsStartPage, - GetFiNoSvUrlsStartPage, -} from "@/lib/graphql/Query/StartPage/StartPage.graphql" -import { internalServerError } from "@/server/errors/trpc" +import { hotelreservation } from "@/constants/routes/hotelReservation" import { publicProcedure, router } from "@/server/trpc" import { getUidAndContentTypeByPath } from "@/services/cms/getUidAndContentTypeByPath" -import { generateTag } from "@/utils/generateTag" import { getLanguageSwitcherInput } from "./input" -import { validateLanguageSwitcherData } from "./output" -import { languageSwitcherAffix } from "./utils" +import { getUrlsOfAllLanguages } from "./utils" -import { PageContentTypeEnum } from "@/types/requests/contentType" -import type { - LanguageSwitcherData, - LanguageSwitcherQueryDataRaw, -} from "@/types/requests/languageSwitcher" - -interface LanguageSwitcherVariables { - contentType: string - uid: string -} - -const meter = metrics.getMeter("trpc.contentstack.languageSwitcher") -const getLanguageSwitcherCounter = meter.createCounter( - "trpc.contentstack.languageSwitcher.get" -) -const getLanguageSwitcherSuccessCounter = meter.createCounter( - "trpc.contentstack.languageSwitcher.get-success" -) -const getLanguageSwitcherFailCounter = meter.createCounter( - "trpc.contentstack.languageSwitcher.get-fail" -) - -async function getLanguageSwitcher(options: LanguageSwitcherVariables) { - const variables = { uid: options.uid } - const tagsDaDeEn = [ - generateTag(Lang.da, options.uid, languageSwitcherAffix), - generateTag(Lang.de, options.uid, languageSwitcherAffix), - generateTag(Lang.en, options.uid, languageSwitcherAffix), - ] - const tagsFiNoSv = [ - generateTag(Lang.fi, options.uid, languageSwitcherAffix), - generateTag(Lang.no, options.uid, languageSwitcherAffix), - generateTag(Lang.sv, options.uid, languageSwitcherAffix), - ] - let daDeEnDocument = null - let fiNoSvDocument = null - switch (options.contentType) { - case PageContentTypeEnum.accountPage: - daDeEnDocument = GetDaDeEnUrlsAccountPage - fiNoSvDocument = GetFiNoSvUrlsAccountPage - break - case PageContentTypeEnum.currentBlocksPage: - daDeEnDocument = GetDaDeEnUrlsCurrentBlocksPage - fiNoSvDocument = GetFiNoSvUrlsCurrentBlocksPage - break - case PageContentTypeEnum.loyaltyPage: - daDeEnDocument = GetDaDeEnUrlsLoyaltyPage - fiNoSvDocument = GetFiNoSvUrlsLoyaltyPage - break - case PageContentTypeEnum.hotelPage: - daDeEnDocument = GetDaDeEnUrlsHotelPage - fiNoSvDocument = GetFiNoSvUrlsHotelPage - break - case PageContentTypeEnum.contentPage: - daDeEnDocument = GetDaDeEnUrlsContentPage - fiNoSvDocument = GetFiNoSvUrlsContentPage - break - case PageContentTypeEnum.collectionPage: - daDeEnDocument = GetDaDeEnUrlsCollectionPage - fiNoSvDocument = GetFiNoSvUrlsCollectionPage - break - case PageContentTypeEnum.destinationOverviewPage: - daDeEnDocument = GetDaDeEnUrlsDestinationOverviewPage - fiNoSvDocument = GetFiNoSvUrlsDestinationOverviewPage - break - case PageContentTypeEnum.destinationCountryPage: - daDeEnDocument = GetDaDeEnUrlsDestinationCountryPage - fiNoSvDocument = GetFiNoSvUrlsDestinationCountryPage - break - case PageContentTypeEnum.destinationCityPage: - daDeEnDocument = GetDaDeEnUrlsDestinationCityPage - fiNoSvDocument = GetFiNoSvUrlsDestinationCityPage - break - case PageContentTypeEnum.startPage: - daDeEnDocument = GetDaDeEnUrlsStartPage - fiNoSvDocument = GetFiNoSvUrlsStartPage - break - default: - console.error(`type: [${options.contentType}]`) - console.error(`Trying to get a content type that is not supported`) - throw internalServerError() - } - - if (daDeEnDocument && fiNoSvDocument) { - return await batchRequest([ - { - document: daDeEnDocument, - variables, - options: { - cache: "force-cache", - next: { - tags: tagsDaDeEn, - }, - }, - }, - { - document: fiNoSvDocument, - variables, - options: { - cache: "force-cache", - next: { - tags: tagsFiNoSv, - }, - }, - }, - ]) - } - - throw internalServerError() -} +import type { LanguageSwitcherData } from "@/types/requests/languageSwitcher" +import type { Lang } from "@/constants/languages" export const languageSwitcherQueryRouter = router({ get: publicProcedure .input(getLanguageSwitcherInput) - .query(async ({ input, ctx }) => { - let uid = ctx.uid - let contentType = ctx.contentType - let lang = ctx.lang ?? input?.lang + .query(async ({ input }) => { + const { pathName, lang } = input + const { uid, contentType } = await getUidAndContentTypeByPath(pathName) - if (input) { - const data = await getUidAndContentTypeByPath(input.pathName) - uid = data.uid - contentType = data.contentType ?? ctx.contentType - } - - if (!uid || !lang) { + if (!uid || !contentType) { // we have pages that are not currently routed within contentstack context, // therefor this fix is needed for some of these pages - if (input && Object.values(findMyBooking).includes(input.pathName)) { - const urls: Record = {} + if (Object.values(findMyBooking).includes(pathName)) { + const urls: LanguageSwitcherData = {} return { lang, urls: Object.entries(findMyBooking).reduce((acc, [lang, url]) => { - acc[lang] = { url } + acc[lang as Lang] = { url } return urls }, urls), } } - return { lang: lang, urls: baseUrls } + if (pathName.startsWith(hotelreservation(lang))) { + return { lang, urls: baseUrls } + } + + return { lang, urls: { [lang]: { url: pathName } } } } - getLanguageSwitcherCounter.add(1, { - uid: uid, - lang: lang, - contentType: contentType, - }) - console.info( - "contentstack.languageSwitcher start", - JSON.stringify({ - query: { - uid: uid, - lang: lang, - contentType: contentType, - }, - }) - ) - const res = await getLanguageSwitcher({ - contentType: contentType!, - uid: uid, - }) + const urls = await getUrlsOfAllLanguages(lang, uid, contentType) - const urls = Object.keys(res.data).reduce( - (acc, key) => { - const item = res.data[key as Lang] - - const url = item - ? item.web?.original_url || `/${key}${item.url}` - : undefined - - return { - ...acc, - [key]: { url, isExternal: !!item?.web?.original_url }, - } - }, - {} as LanguageSwitcherData - ) - - const validatedLanguageSwitcherData = - validateLanguageSwitcherData.safeParse(urls) - - if (!validatedLanguageSwitcherData.success) { - getLanguageSwitcherFailCounter.add(1, { - uid: uid, - lang: lang, - contentType: contentType, - error_type: "validation_error", - error: JSON.stringify(validatedLanguageSwitcherData.error), - }) - console.error( - "contentstack.languageSwitcher validation error", - JSON.stringify({ - query: { - uid: uid, - lang: lang, - contentType: contentType, - }, - error: validatedLanguageSwitcherData.error, - }) - ) - return null - } - getLanguageSwitcherSuccessCounter.add(1, { - uid: uid, - lang: lang, - contentType: contentType, - }) - console.info( - "contentstack.languageSwitcher success", - JSON.stringify({ - query: { - uid: uid, - lang: lang, - contentType: contentType, - }, - }) - ) return { - lang: lang, + lang, urls, } }), diff --git a/apps/scandic-web/server/routers/contentstack/languageSwitcher/telemetry.ts b/apps/scandic-web/server/routers/contentstack/languageSwitcher/telemetry.ts new file mode 100644 index 000000000..6d81b5edc --- /dev/null +++ b/apps/scandic-web/server/routers/contentstack/languageSwitcher/telemetry.ts @@ -0,0 +1,12 @@ +import { metrics } from "@opentelemetry/api" + +const meter = metrics.getMeter("trpc.contentstack.languageSwitcher") +export const getLanguageSwitcherCounter = meter.createCounter( + "trpc.contentstack.languageSwitcher.get" +) +export const getLanguageSwitcherSuccessCounter = meter.createCounter( + "trpc.contentstack.languageSwitcher.get-success" +) +export const getLanguageSwitcherFailCounter = meter.createCounter( + "trpc.contentstack.languageSwitcher.get-fail" +) diff --git a/apps/scandic-web/server/routers/contentstack/languageSwitcher/utils.ts b/apps/scandic-web/server/routers/contentstack/languageSwitcher/utils.ts index 8adaaa376..3203a1ed9 100644 --- a/apps/scandic-web/server/routers/contentstack/languageSwitcher/utils.ts +++ b/apps/scandic-web/server/routers/contentstack/languageSwitcher/utils.ts @@ -1 +1,228 @@ +import { Lang } from "@/constants/languages" +import { batchRequest } from "@/lib/graphql/batchRequest" +import { + GetDaDeEnUrlsAccountPage, + GetFiNoSvUrlsAccountPage, +} from "@/lib/graphql/Query/AccountPage/AccountPage.graphql" +import { + GetDaDeEnUrlsCollectionPage, + GetFiNoSvUrlsCollectionPage, +} from "@/lib/graphql/Query/CollectionPage/CollectionPage.graphql" +import { + GetDaDeEnUrlsContentPage, + GetFiNoSvUrlsContentPage, +} from "@/lib/graphql/Query/ContentPage/ContentPage.graphql" +import { + GetDaDeEnUrlsCurrentBlocksPage, + GetFiNoSvUrlsCurrentBlocksPage, +} from "@/lib/graphql/Query/Current/LanguageSwitcher.graphql" +import { + GetDaDeEnUrlsDestinationCityPage, + GetFiNoSvUrlsDestinationCityPage, +} from "@/lib/graphql/Query/DestinationCityPage/DestinationCityPage.graphql" +import { + GetDaDeEnUrlsDestinationCountryPage, + GetFiNoSvUrlsDestinationCountryPage, +} from "@/lib/graphql/Query/DestinationCountryPage/DestinationCountryPage.graphql" +import { + GetDaDeEnUrlsDestinationOverviewPage, + GetFiNoSvUrlsDestinationOverviewPage, +} from "@/lib/graphql/Query/DestinationOverviewPage/DestinationOverviewPage.graphql" +import { + GetDaDeEnUrlsHotelPage, + GetFiNoSvUrlsHotelPage, +} from "@/lib/graphql/Query/HotelPage/HotelPage.graphql" +import { + GetDaDeEnUrlsLoyaltyPage, + GetFiNoSvUrlsLoyaltyPage, +} from "@/lib/graphql/Query/LoyaltyPage/LoyaltyPage.graphql" +import { + GetDaDeEnUrlsStartPage, + GetFiNoSvUrlsStartPage, +} from "@/lib/graphql/Query/StartPage/StartPage.graphql" +import { internalServerError } from "@/server/errors/trpc" + +import { generateTag } from "@/utils/generateTag" + +import { validateLanguageSwitcherData } from "./output" +import { + getLanguageSwitcherCounter, + getLanguageSwitcherFailCounter, + getLanguageSwitcherSuccessCounter, +} from "./telemetry" + +import { PageContentTypeEnum } from "@/types/requests/contentType" +import type { + LanguageSwitcherData, + LanguageSwitcherQueryDataRaw, +} from "@/types/requests/languageSwitcher" + export const languageSwitcherAffix = "languageSwitcher" + +export async function getUrlsOfAllLanguages( + lang: Lang, + uid: string, + contentType: string +) { + getLanguageSwitcherCounter.add(1, { + uid, + lang, + contentType, + }) + console.info( + "contentstack.languageSwitcher start", + JSON.stringify({ + query: { + uid, + lang, + contentType, + }, + }) + ) + + const variables = { uid } + const tagsDaDeEn = [ + generateTag(Lang.da, uid, languageSwitcherAffix), + generateTag(Lang.de, uid, languageSwitcherAffix), + generateTag(Lang.en, uid, languageSwitcherAffix), + ] + const tagsFiNoSv = [ + generateTag(Lang.fi, uid, languageSwitcherAffix), + generateTag(Lang.no, uid, languageSwitcherAffix), + generateTag(Lang.sv, uid, languageSwitcherAffix), + ] + let daDeEnDocument = null + let fiNoSvDocument = null + switch (contentType) { + case PageContentTypeEnum.accountPage: + daDeEnDocument = GetDaDeEnUrlsAccountPage + fiNoSvDocument = GetFiNoSvUrlsAccountPage + break + case PageContentTypeEnum.currentBlocksPage: + daDeEnDocument = GetDaDeEnUrlsCurrentBlocksPage + fiNoSvDocument = GetFiNoSvUrlsCurrentBlocksPage + break + case PageContentTypeEnum.loyaltyPage: + daDeEnDocument = GetDaDeEnUrlsLoyaltyPage + fiNoSvDocument = GetFiNoSvUrlsLoyaltyPage + break + case PageContentTypeEnum.hotelPage: + daDeEnDocument = GetDaDeEnUrlsHotelPage + fiNoSvDocument = GetFiNoSvUrlsHotelPage + break + case PageContentTypeEnum.contentPage: + daDeEnDocument = GetDaDeEnUrlsContentPage + fiNoSvDocument = GetFiNoSvUrlsContentPage + break + case PageContentTypeEnum.collectionPage: + daDeEnDocument = GetDaDeEnUrlsCollectionPage + fiNoSvDocument = GetFiNoSvUrlsCollectionPage + break + case PageContentTypeEnum.destinationOverviewPage: + daDeEnDocument = GetDaDeEnUrlsDestinationOverviewPage + fiNoSvDocument = GetFiNoSvUrlsDestinationOverviewPage + break + case PageContentTypeEnum.destinationCountryPage: + daDeEnDocument = GetDaDeEnUrlsDestinationCountryPage + fiNoSvDocument = GetFiNoSvUrlsDestinationCountryPage + break + case PageContentTypeEnum.destinationCityPage: + daDeEnDocument = GetDaDeEnUrlsDestinationCityPage + fiNoSvDocument = GetFiNoSvUrlsDestinationCityPage + break + case PageContentTypeEnum.startPage: + daDeEnDocument = GetDaDeEnUrlsStartPage + fiNoSvDocument = GetFiNoSvUrlsStartPage + break + default: + console.error(`type: [${contentType}]`) + console.error(`Trying to get a content type that is not supported`) + throw internalServerError() + } + + if (!daDeEnDocument || !fiNoSvDocument) { + throw internalServerError() + } + + const response = await batchRequest([ + { + document: daDeEnDocument, + variables, + options: { + cache: "force-cache", + next: { + tags: tagsDaDeEn, + }, + }, + }, + { + document: fiNoSvDocument, + variables, + options: { + cache: "force-cache", + next: { + tags: tagsFiNoSv, + }, + }, + }, + ]) + + const urls = Object.keys(response.data).reduce( + (acc, key) => { + const item = response.data[key as Lang] + + const url = item + ? item.web?.original_url || `/${key}${item.url}` + : undefined + + return { + ...acc, + [key]: { url, isExternal: !!item?.web?.original_url }, + } + }, + {} + ) + + const validatedLanguageSwitcherData = + validateLanguageSwitcherData.safeParse(urls) + + if (!validatedLanguageSwitcherData.success) { + getLanguageSwitcherFailCounter.add(1, { + uid, + lang, + contentType, + error_type: "validation_error", + error: JSON.stringify(validatedLanguageSwitcherData.error), + }) + console.error( + "contentstack.languageSwitcher validation error", + JSON.stringify({ + query: { + uid, + lang, + contentType, + }, + error: validatedLanguageSwitcherData.error, + }) + ) + return null + } + + getLanguageSwitcherSuccessCounter.add(1, { + uid, + lang, + contentType, + }) + console.info( + "contentstack.languageSwitcher success", + JSON.stringify({ + query: { + uid, + lang, + contentType, + }, + }) + ) + + return urls +}