Merged in feat/sw-2863-move-contentstack-router-to-trpc-package (pull request #2389)
feat(SW-2863): Move contentstack router to trpc package * Add exports to packages and lint rule to prevent relative imports * Add env to trpc package * Add eslint to trpc package * Apply lint rules * Use direct imports from trpc package * Add lint-staged config to trpc * Move lang enum to common * Restructure trpc package folder structure * WIP first step * update internal imports in trpc * Fix most errors in scandic-web Just 100 left... * Move Props type out of trpc * Fix CategorizedFilters types * Move more schemas in hotel router * Fix deps * fix getNonContentstackUrls * Fix import error * Fix entry error handling * Fix generateMetadata metrics * Fix alertType enum * Fix duplicated types * lint:fix * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package * Fix broken imports * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package Approved-by: Linus Flood
This commit is contained in:
@@ -0,0 +1,271 @@
|
||||
import { Lang } from "@scandic-hotels/common/constants/language"
|
||||
import { createCounter } from "@scandic-hotels/common/telemetry"
|
||||
import { safeTry } from "@scandic-hotels/common/utils/safeTry"
|
||||
import { notFound } from "@scandic-hotels/trpc/errors"
|
||||
import {
|
||||
contentstackExtendedProcedureUID,
|
||||
serviceProcedure,
|
||||
} from "@scandic-hotels/trpc/procedures"
|
||||
|
||||
import { router } from "../../.."
|
||||
import {
|
||||
GetDestinationOverviewPage,
|
||||
GetDestinationOverviewPageRefs,
|
||||
} from "../../../graphql/Query/DestinationOverviewPage/DestinationOverviewPage.graphql"
|
||||
import { request } from "../../../graphql/request"
|
||||
import { ApiCountry, type Country } from "../../../types/country"
|
||||
import {
|
||||
generateRefsResponseTag,
|
||||
generateTag,
|
||||
} from "../../../utils/generateTag"
|
||||
import {
|
||||
getCitiesByCountry,
|
||||
getCountries,
|
||||
getHotelIdsByCityId,
|
||||
} 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,
|
||||
} from "./output"
|
||||
import { getSortedDestinationsByLanguage } from "./utils"
|
||||
|
||||
import type {
|
||||
GetDestinationOverviewPageData,
|
||||
GetDestinationOverviewPageRefsSchema,
|
||||
} from "../../../types/destinationOverviewPage"
|
||||
import type { City, DestinationsData } from "../../../types/destinationsData"
|
||||
import type { TrackingPageData } from "../../types"
|
||||
|
||||
export const destinationOverviewPageQueryRouter = router({
|
||||
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
||||
const { lang, uid } = ctx
|
||||
|
||||
const getDestinationOverviewPageRefsCounter = createCounter(
|
||||
"trpc.contentstack",
|
||||
"destinationOverviewPage.get.refs"
|
||||
)
|
||||
const metricsGetDestinationOverviewPageRefs =
|
||||
getDestinationOverviewPageRefsCounter.init({ lang, uid })
|
||||
|
||||
metricsGetDestinationOverviewPageRefs.start()
|
||||
|
||||
const refsResponse = await request<GetDestinationOverviewPageRefsSchema>(
|
||||
GetDestinationOverviewPageRefs,
|
||||
{
|
||||
locale: lang,
|
||||
uid,
|
||||
},
|
||||
{
|
||||
key: generateRefsResponseTag(lang, uid),
|
||||
ttl: "max",
|
||||
}
|
||||
)
|
||||
if (!refsResponse.data) {
|
||||
const notFoundError = notFound(refsResponse)
|
||||
metricsGetDestinationOverviewPageRefs.noDataError()
|
||||
throw notFoundError
|
||||
}
|
||||
|
||||
const validatedRefsData = destinationOverviewPageRefsSchema.safeParse(
|
||||
refsResponse.data
|
||||
)
|
||||
|
||||
if (!validatedRefsData.success) {
|
||||
metricsGetDestinationOverviewPageRefs.validationError(
|
||||
validatedRefsData.error
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
metricsGetDestinationOverviewPageRefs.success()
|
||||
|
||||
const getDestinationOverviewPageCounter = createCounter(
|
||||
"trpc.contentstack",
|
||||
"destinationOverviewPage.get"
|
||||
)
|
||||
const metricsGetDestinationOverviewPage =
|
||||
getDestinationOverviewPageCounter.init({ lang, uid })
|
||||
|
||||
metricsGetDestinationOverviewPage.start()
|
||||
|
||||
const response = await request<GetDestinationOverviewPageData>(
|
||||
GetDestinationOverviewPage,
|
||||
{
|
||||
locale: lang,
|
||||
uid,
|
||||
},
|
||||
{
|
||||
key: generateTag(lang, uid),
|
||||
ttl: "max",
|
||||
}
|
||||
)
|
||||
if (!response.data) {
|
||||
const notFoundError = notFound(response)
|
||||
metricsGetDestinationOverviewPage.noDataError()
|
||||
throw notFoundError
|
||||
}
|
||||
|
||||
const destinationOverviewPage = destinationOverviewPageSchema.safeParse(
|
||||
response.data
|
||||
)
|
||||
|
||||
if (!destinationOverviewPage.success) {
|
||||
metricsGetDestinationOverviewPage.validationError(
|
||||
destinationOverviewPage.error
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
metricsGetDestinationOverviewPage.success()
|
||||
|
||||
const system = destinationOverviewPage.data.destination_overview_page.system
|
||||
const tracking: TrackingPageData = {
|
||||
pageId: system.uid,
|
||||
domainLanguage: lang,
|
||||
publishDate: system.updated_at,
|
||||
createDate: system.created_at,
|
||||
channel: "hotels",
|
||||
pageType: "destinationoverviewpage",
|
||||
pageName: "destinations|overview",
|
||||
siteSections: "destinations|overview",
|
||||
siteVersion: "new-web",
|
||||
}
|
||||
|
||||
return {
|
||||
destinationOverviewPage:
|
||||
destinationOverviewPage.data.destination_overview_page,
|
||||
tracking,
|
||||
}
|
||||
}),
|
||||
destinations: 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.
|
||||
|
||||
// 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
|
||||
|
||||
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()
|
||||
}
|
||||
|
||||
async function updateJSONOnDisk() {
|
||||
const { lang } = ctx
|
||||
|
||||
const countries = await getCountries({
|
||||
lang,
|
||||
serviceToken: ctx.serviceToken,
|
||||
})
|
||||
|
||||
if (!countries) {
|
||||
return []
|
||||
}
|
||||
|
||||
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 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,
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
const data = getSortedDestinationsByLanguage(destinations, lang)
|
||||
const fs = await import("node:fs")
|
||||
fs.writeFileSync(
|
||||
`./server/routers/contentstack/destinationOverviewPage/destinations-${lang}.json`,
|
||||
JSON.stringify(data),
|
||||
{
|
||||
encoding: "utf-8",
|
||||
}
|
||||
)
|
||||
return data
|
||||
}
|
||||
}),
|
||||
}),
|
||||
})
|
||||
Reference in New Issue
Block a user