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:
130
packages/trpc/lib/routers/contentstack/startPage/query.ts
Normal file
130
packages/trpc/lib/routers/contentstack/startPage/query.ts
Normal file
@@ -0,0 +1,130 @@
|
||||
import { createCounter } from "@scandic-hotels/common/telemetry"
|
||||
|
||||
import { router } from "../../.."
|
||||
import { notFound } from "../../../errors"
|
||||
import {
|
||||
GetStartPage,
|
||||
GetStartPageRefs,
|
||||
} from "../../../graphql/Query/StartPage/StartPage.graphql"
|
||||
import { request } from "../../../graphql/request"
|
||||
import { contentstackExtendedProcedureUID } from "../../../procedures"
|
||||
import {
|
||||
generateRefsResponseTag,
|
||||
generateTag,
|
||||
generateTagsFromSystem,
|
||||
} from "../../../utils/generateTag"
|
||||
import { startPageRefsSchema, startPageSchema } from "./output"
|
||||
import { getConnections } from "./utils"
|
||||
|
||||
import type { z } from "zod"
|
||||
|
||||
import type { TrackingPageData } from "../../types"
|
||||
import type { blocksSchema } from "./output"
|
||||
|
||||
export interface GetStartPageData extends z.input<typeof startPageSchema> {}
|
||||
export interface StartPage extends z.output<typeof startPageSchema> {}
|
||||
|
||||
export interface GetStartPageRefsSchema
|
||||
extends z.input<typeof startPageRefsSchema> {}
|
||||
|
||||
export type Block = z.output<typeof blocksSchema>
|
||||
|
||||
export const startPageQueryRouter = router({
|
||||
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
||||
const { lang, uid } = ctx
|
||||
|
||||
const getStartPageRefsCounter = createCounter(
|
||||
"trpc.contentstack",
|
||||
"startPage.get.refs"
|
||||
)
|
||||
const metricsGetStartPageRefs = getStartPageRefsCounter.init({ lang, uid })
|
||||
|
||||
metricsGetStartPageRefs.start()
|
||||
|
||||
const refsResponse = await request<GetStartPageRefsSchema>(
|
||||
GetStartPageRefs,
|
||||
{
|
||||
locale: lang,
|
||||
uid,
|
||||
},
|
||||
{
|
||||
key: generateRefsResponseTag(lang, uid),
|
||||
ttl: "max",
|
||||
}
|
||||
)
|
||||
if (!refsResponse.data) {
|
||||
const notFoundError = notFound(refsResponse)
|
||||
metricsGetStartPageRefs.noDataError()
|
||||
throw notFoundError
|
||||
}
|
||||
|
||||
const validatedRefsData = startPageRefsSchema.safeParse(refsResponse.data)
|
||||
|
||||
if (!validatedRefsData.success) {
|
||||
metricsGetStartPageRefs.validationError(validatedRefsData.error)
|
||||
return null
|
||||
}
|
||||
|
||||
metricsGetStartPageRefs.success()
|
||||
|
||||
const getStartPageCounter = createCounter(
|
||||
"trpc.contentstack",
|
||||
"startPage.get"
|
||||
)
|
||||
const metricsGetStartPage = getStartPageCounter.init({ lang, uid })
|
||||
|
||||
metricsGetStartPage.start()
|
||||
|
||||
const connections = getConnections(validatedRefsData.data)
|
||||
|
||||
const tags = [
|
||||
generateTagsFromSystem(lang, connections),
|
||||
generateTag(lang, validatedRefsData.data.start_page.system.uid),
|
||||
].flat()
|
||||
|
||||
const response = await request<GetStartPageData>(
|
||||
GetStartPage,
|
||||
{
|
||||
locale: lang,
|
||||
uid,
|
||||
},
|
||||
{
|
||||
key: tags,
|
||||
ttl: "max",
|
||||
}
|
||||
)
|
||||
|
||||
if (!response.data) {
|
||||
const notFoundError = notFound(response)
|
||||
metricsGetStartPage.noDataError()
|
||||
throw notFoundError
|
||||
}
|
||||
|
||||
const startPage = startPageSchema.safeParse(response.data)
|
||||
|
||||
if (!startPage.success) {
|
||||
metricsGetStartPage.validationError(startPage.error)
|
||||
return null
|
||||
}
|
||||
|
||||
metricsGetStartPage.success()
|
||||
|
||||
const system = startPage.data.start_page.system
|
||||
const tracking: TrackingPageData = {
|
||||
pageName: "startpage",
|
||||
pageType: "startpage",
|
||||
pageId: system.uid,
|
||||
channel: "homepage",
|
||||
domainLanguage: lang,
|
||||
createDate: system.created_at,
|
||||
publishDate: system.updated_at,
|
||||
siteSections: "startpage",
|
||||
siteVersion: "new-web",
|
||||
}
|
||||
|
||||
return {
|
||||
startPage: startPage.data.start_page,
|
||||
tracking,
|
||||
}
|
||||
}),
|
||||
})
|
||||
Reference in New Issue
Block a user