Merged in feat/SW-3443-destinations-list (pull request #2781)

feat(destinations list): SW-3443 remove hard coded response and fetch from API instead

* feat(destinations list): SW-3443 remove hard coded response and fetch from API instead


Approved-by: Erik Tiekstra
Approved-by: Matilda Landström
This commit is contained in:
Linus Flood
2025-09-11 09:14:01 +00:00
parent 3e5263bf72
commit bacec22a8a
8 changed files with 61 additions and 5740 deletions
@@ -1,4 +1,3 @@
import { Lang } from "@scandic-hotels/common/constants/language"
import { createCounter } from "@scandic-hotels/common/telemetry"
import { safeTry } from "@scandic-hotels/common/utils/safeTry"
@@ -25,12 +24,6 @@ import {
} from "../../hotels/utils"
import { getCityPageUrls } from "../destinationCityPage/utils"
import { getCountryPageUrls } from "../destinationCountryPage/utils"
import destinationsDataDa from "./destinations-da.json" with { assert: "json" }
import destinationsDataDe from "./destinations-de.json" with { assert: "json" }
import destinationsDataEn from "./destinations-en.json" with { assert: "json" }
import destinationsDataFi from "./destinations-fi.json" with { assert: "json" }
import destinationsDataNo from "./destinations-no.json" with { assert: "json" }
import destinationsDataSv from "./destinations-sv.json" with { assert: "json" }
import {
destinationOverviewPageRefsSchema,
destinationOverviewPageSchema,
@@ -149,122 +142,76 @@ export const destinationOverviewPageQueryRouter = router({
get: serviceProcedure.query(async function ({
ctx,
}): Promise<DestinationsData> {
// For go live we are using static data here, as it rarely changes.
// This also improves operational reliance as we are not hammering
// a lot of endpoints for a lot of data.
// Re-implement once we have better API support and established caching
// patterns and mechanisms.
const { lang } = ctx
// NOTE: To update the static data set `useStaticData = false`.
// Then go to the "Hotels & Destinations" page and visit every language.
// At the time of commit http://localhost:3000/en/destinations.
// This will update the JSON file locally, each page load for each language,
// if all data loads correctly.
// Set back `useStaticData = true` again and test with the updated JSON file.
// Add, commit and push the updated JSON files with useStaticData = true here.
const useStaticData = true
const countries = await getCountries({
lang,
serviceToken: ctx.serviceToken,
})
if (useStaticData) {
switch (ctx.lang) {
case Lang.da:
return getSortedDestinationsByLanguage(destinationsDataDa, ctx.lang)
case Lang.de:
return getSortedDestinationsByLanguage(destinationsDataDe, ctx.lang)
case Lang.fi:
return getSortedDestinationsByLanguage(destinationsDataFi, ctx.lang)
case Lang.en:
return getSortedDestinationsByLanguage(destinationsDataEn, ctx.lang)
case Lang.no:
return getSortedDestinationsByLanguage(destinationsDataNo, ctx.lang)
case Lang.sv:
return getSortedDestinationsByLanguage(destinationsDataSv, ctx.lang)
default:
return []
}
} else {
return await updateJSONOnDisk()
if (!countries) {
return []
}
async function updateJSONOnDisk() {
const { lang } = ctx
const countryNames = countries.data.map((country) => country.name)
const countries = await getCountries({
lang,
serviceToken: ctx.serviceToken,
})
const citiesByCountry = await getCitiesByCountry({
lang,
countries: countryNames,
serviceToken: ctx.serviceToken,
})
if (!countries) {
return []
}
const cityPages = await getCityPageUrls(lang)
const countryNames = countries.data.map((country) => country.name)
const citiesByCountry = await getCitiesByCountry({
lang,
countries: countryNames,
serviceToken: ctx.serviceToken,
})
const cityPages = await getCityPageUrls(lang)
const destinations = await Promise.all(
Object.entries(citiesByCountry).map(async ([country, cities]) => {
const activeCitiesWithHotelCount: (City | null)[] =
await Promise.all(
cities.map(async (city) => {
const [hotels] = await safeTry(
getHotelIdsByCityId({
cityId: city.id,
serviceToken: ctx.serviceToken,
})
)
const cityPage = cityPages.find(
(cityPage) => cityPage.city === city.cityIdentifier
)
return cityPage?.url
? {
id: city.id,
name: city.name,
hotelIds: hotels || [],
hotelCount: hotels ? hotels.length : 0,
url: cityPage.url,
}
: null
const destinations = await Promise.all(
Object.entries(citiesByCountry).map(async ([country, cities]) => {
const activeCitiesWithHotelCount: (City | null)[] = await Promise.all(
cities.map(async (city) => {
const [hotels] = await safeTry(
getHotelIdsByCityId({
cityId: city.id,
serviceToken: ctx.serviceToken,
})
)
const filteredActiveCitiesWithHotelCount: City[] =
activeCitiesWithHotelCount.filter((c): c is City => !!c)
const countryPages = await getCountryPageUrls(lang)
const countryPage = countryPages.find(
(countryPage) =>
ApiCountry[lang][countryPage.country as Country] === country
)
const cityPage = cityPages.find(
(cityPage) => cityPage.city === city.cityIdentifier
)
return {
country,
countryUrl: countryPage?.url,
numberOfHotels: filteredActiveCitiesWithHotelCount.reduce(
(acc, city) => acc + city.hotelCount,
0
),
cities: filteredActiveCitiesWithHotelCount,
}
})
)
const data = getSortedDestinationsByLanguage(destinations, lang)
const fs = await import("node:fs")
fs.writeFileSync(
`../../packages/trpc/lib/routers/contentstack/destinationOverviewPage/destinations-${lang}.json`,
JSON.stringify(data),
{
encoding: "utf-8",
return cityPage?.url
? {
id: city.id,
name: city.name,
hotelIds: hotels || [],
hotelCount: hotels ? hotels.length : 0,
url: cityPage.url,
}
: null
})
)
const filteredActiveCitiesWithHotelCount: City[] =
activeCitiesWithHotelCount.filter((c): c is City => !!c)
const countryPages = await getCountryPageUrls(lang)
const countryPage = countryPages.find(
(countryPage) =>
ApiCountry[lang][countryPage.country as Country] === country
)
return {
country,
countryUrl: countryPage?.url,
numberOfHotels: filteredActiveCitiesWithHotelCount.reduce(
(acc, city) => acc + city.hotelCount,
0
),
cities: filteredActiveCitiesWithHotelCount,
}
)
return data
}
})
)
const data = getSortedDestinationsByLanguage(destinations, lang)
return data
}),
}),
})