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:
118
packages/trpc/lib/routers/contentstack/contentPage/query.ts
Normal file
118
packages/trpc/lib/routers/contentstack/contentPage/query.ts
Normal file
@@ -0,0 +1,118 @@
|
||||
import { createCounter } from "@scandic-hotels/common/telemetry"
|
||||
import { contentstackExtendedProcedureUID } from "@scandic-hotels/trpc/procedures"
|
||||
|
||||
import { router } from "../../.."
|
||||
import { batchRequest } from "../../../graphql/batchRequest"
|
||||
import {
|
||||
GetContentPage,
|
||||
GetContentPageBlocksBatch1,
|
||||
GetContentPageBlocksBatch2,
|
||||
} from "../../../graphql/Query/ContentPage/ContentPage.graphql"
|
||||
import { contentPageSchema } from "./output"
|
||||
import { fetchContentPageRefs, generatePageTags } from "./utils"
|
||||
|
||||
import type { GetContentPageSchema } from "../../../types/contentPage"
|
||||
import type { TrackingPageData } from "../../types"
|
||||
|
||||
export const contentPageQueryRouter = router({
|
||||
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
||||
const { lang, uid } = ctx
|
||||
|
||||
const contentPageRefs = await fetchContentPageRefs(lang, uid)
|
||||
|
||||
if (!contentPageRefs) {
|
||||
return null
|
||||
}
|
||||
|
||||
const tags = generatePageTags(contentPageRefs, lang)
|
||||
|
||||
const getContentPageCounter = createCounter(
|
||||
"trpc.contentstack",
|
||||
"contentPage.get"
|
||||
)
|
||||
const metricsGetContentPage = getContentPageCounter.init({
|
||||
lang,
|
||||
uid,
|
||||
})
|
||||
|
||||
metricsGetContentPage.start()
|
||||
|
||||
const contentPageRequest = await batchRequest<GetContentPageSchema>([
|
||||
{
|
||||
document: GetContentPage,
|
||||
variables: { locale: lang, uid },
|
||||
cacheOptions: {
|
||||
key: `${tags.join(",")}:contentPage`,
|
||||
ttl: "max",
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
document: GetContentPageBlocksBatch1,
|
||||
variables: { locale: lang, uid },
|
||||
cacheOptions: {
|
||||
key: `${tags.join(",")}:contentPageBlocksBatch1`,
|
||||
ttl: "max",
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
document: GetContentPageBlocksBatch2,
|
||||
variables: { locale: lang, uid },
|
||||
cacheOptions: {
|
||||
key: `${tags.join(",")}:contentPageBlocksBatch2`,
|
||||
ttl: "max",
|
||||
},
|
||||
},
|
||||
])
|
||||
|
||||
const contentPage = contentPageSchema.safeParse(contentPageRequest.data)
|
||||
if (!contentPage.success) {
|
||||
metricsGetContentPage.validationError(contentPage.error)
|
||||
return null
|
||||
}
|
||||
|
||||
metricsGetContentPage.success()
|
||||
|
||||
const tracking: TrackingPageData = {
|
||||
pageId: contentPage.data.content_page.system.uid,
|
||||
domainLanguage: lang,
|
||||
publishDate: contentPage.data.content_page.system.updated_at,
|
||||
createDate: contentPage.data.content_page.system.created_at,
|
||||
channel: createChannel(contentPage.data.content_page.system.uid),
|
||||
pageType: createPageType(contentPage.data.content_page.system.uid),
|
||||
pageName: contentPage.data.trackingProps.url,
|
||||
siteSections: contentPage.data.trackingProps.url,
|
||||
siteVersion: "new-web",
|
||||
}
|
||||
|
||||
return {
|
||||
contentPage: contentPage.data.content_page,
|
||||
tracking,
|
||||
}
|
||||
}),
|
||||
})
|
||||
|
||||
const signupContentPageUid = "blt0e6bd6c4d7224f07"
|
||||
const signupVerifyContentPageUid = "blt3247a2a29b34a8e8"
|
||||
|
||||
export function createChannel(uid: string): TrackingPageData["channel"] {
|
||||
switch (uid) {
|
||||
case signupContentPageUid:
|
||||
case signupVerifyContentPageUid:
|
||||
return "scandic-friends"
|
||||
default:
|
||||
return "static-content-page"
|
||||
}
|
||||
}
|
||||
|
||||
export function createPageType(uid: string): string {
|
||||
switch (uid) {
|
||||
case signupContentPageUid:
|
||||
return "memberprofilecreatepage"
|
||||
case signupVerifyContentPageUid:
|
||||
return "memberprofilecreatesuccesspage"
|
||||
default:
|
||||
return "staticcontentpage"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user