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,143 @@
|
||||
import { createCounter } from "@scandic-hotels/common/telemetry"
|
||||
import { notFound } from "@scandic-hotels/trpc/errors"
|
||||
import {
|
||||
contentStackBaseWithServiceProcedure,
|
||||
contentstackExtendedProcedureUID,
|
||||
} from "@scandic-hotels/trpc/procedures"
|
||||
|
||||
import { router } from "../../.."
|
||||
import {
|
||||
GetDestinationCountryPage,
|
||||
GetDestinationCountryPageRefs,
|
||||
} from "../../../graphql/Query/DestinationCountryPage/DestinationCountryPage.graphql"
|
||||
import { request } from "../../../graphql/request"
|
||||
import { ApiCountry } from "../../../types/country"
|
||||
import { generateRefsResponseTag } from "../../../utils/generateTag"
|
||||
import { getCityPagesInput } from "./input"
|
||||
import {
|
||||
destinationCountryPageRefsSchema,
|
||||
destinationCountryPageSchema,
|
||||
} from "./output"
|
||||
import { generatePageTags, getCityPages } from "./utils"
|
||||
|
||||
import type {
|
||||
GetDestinationCountryPageData,
|
||||
GetDestinationCountryPageRefsSchema,
|
||||
} from "../../../types/destinationCountryPage"
|
||||
import type { TrackingPageData } from "../../types"
|
||||
|
||||
export const destinationCountryPageQueryRouter = router({
|
||||
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
||||
const { lang, uid } = ctx
|
||||
|
||||
const getDestinationCountryPageRefsCounter = createCounter(
|
||||
"trpc.contentstack",
|
||||
"destinationCountryPage.get.refs"
|
||||
)
|
||||
const metricsGetDestinationCountryPageRefs =
|
||||
getDestinationCountryPageRefsCounter.init({ lang, uid })
|
||||
|
||||
metricsGetDestinationCountryPageRefs.start()
|
||||
|
||||
const refsResponse = await request<GetDestinationCountryPageRefsSchema>(
|
||||
GetDestinationCountryPageRefs,
|
||||
{ locale: lang, uid },
|
||||
{
|
||||
key: generateRefsResponseTag(lang, uid),
|
||||
ttl: "max",
|
||||
}
|
||||
)
|
||||
|
||||
if (!refsResponse.data) {
|
||||
const notFoundError = notFound(refsResponse)
|
||||
metricsGetDestinationCountryPageRefs.noDataError()
|
||||
throw notFoundError
|
||||
}
|
||||
|
||||
const validatedRefsData = destinationCountryPageRefsSchema.safeParse(
|
||||
refsResponse.data
|
||||
)
|
||||
if (!validatedRefsData.success) {
|
||||
metricsGetDestinationCountryPageRefs.validationError(
|
||||
validatedRefsData.error
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
metricsGetDestinationCountryPageRefs.success()
|
||||
|
||||
const tags = generatePageTags(validatedRefsData.data, lang)
|
||||
|
||||
const getDestinationCountryPageCounter = createCounter(
|
||||
"trpc.contentstack",
|
||||
"destinationCountryPage.get"
|
||||
)
|
||||
const metricsGetDestinationCountryPage =
|
||||
getDestinationCountryPageCounter.init({ lang, uid })
|
||||
|
||||
metricsGetDestinationCountryPage.start()
|
||||
|
||||
const response = await request<GetDestinationCountryPageData>(
|
||||
GetDestinationCountryPage,
|
||||
{
|
||||
locale: lang,
|
||||
uid,
|
||||
},
|
||||
{
|
||||
key: tags,
|
||||
ttl: "max",
|
||||
}
|
||||
)
|
||||
if (!response.data) {
|
||||
const notFoundError = notFound(response)
|
||||
metricsGetDestinationCountryPage.noDataError()
|
||||
throw notFoundError
|
||||
}
|
||||
|
||||
const validatedResponse = destinationCountryPageSchema.safeParse(
|
||||
response.data
|
||||
)
|
||||
|
||||
if (!validatedResponse.success) {
|
||||
metricsGetDestinationCountryPage.validationError(validatedResponse.error)
|
||||
return null
|
||||
}
|
||||
|
||||
const destinationCountryPage =
|
||||
validatedResponse.data.destination_country_page
|
||||
const country = destinationCountryPage.destination_settings.country
|
||||
|
||||
metricsGetDestinationCountryPage.success()
|
||||
|
||||
const system = destinationCountryPage.system
|
||||
const pageName = `destinations|${country}`
|
||||
|
||||
const tracking: TrackingPageData = {
|
||||
pageId: system.uid,
|
||||
domainLanguage: system.locale,
|
||||
publishDate: system.updated_at,
|
||||
createDate: system.created_at,
|
||||
channel: "hotels",
|
||||
pageType: "countrypage",
|
||||
pageName,
|
||||
siteSections: pageName,
|
||||
siteVersion: "new-web",
|
||||
}
|
||||
|
||||
return {
|
||||
destinationCountryPage,
|
||||
translatedCountry: ApiCountry[lang][country],
|
||||
tracking,
|
||||
}
|
||||
}),
|
||||
cityPages: contentStackBaseWithServiceProcedure
|
||||
.input(getCityPagesInput)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const { lang, serviceToken } = ctx
|
||||
const { country } = input
|
||||
|
||||
const cities = await getCityPages(lang, serviceToken, country)
|
||||
|
||||
return cities
|
||||
}),
|
||||
})
|
||||
Reference in New Issue
Block a user