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:
Anton Gunnarsson
2025-06-26 07:53:01 +00:00
parent 0263ab8c87
commit 002d093af4
921 changed files with 3112 additions and 3008 deletions

View File

@@ -0,0 +1,4 @@
import { mergeRouters } from "../../.."
import { loyaltyPageQueryRouter } from "./query"
export const loyaltyPageRouter = mergeRouters(loyaltyPageQueryRouter)

View File

@@ -0,0 +1,193 @@
import { z } from "zod"
import { LoyaltyPageEnum } from "../../../enums/loyaltyPage"
import { discriminatedUnionArray } from "../../../utils/discriminatedUnion"
import {
cardGridRefsSchema,
cardsGridSchema,
} from "../schemas/blocks/cardsGrid"
import {
contentRefsSchema as blockContentRefsSchema,
contentSchema as blockContentSchema,
} from "../schemas/blocks/content"
import {
dynamicContentRefsSchema,
dynamicContentSchema as blockDynamicContentSchema,
} from "../schemas/blocks/dynamicContent"
import {
shortcutsRefsSchema,
shortcutsSchema,
} from "../schemas/blocks/shortcuts"
import { tempImageVaultAssetSchema } from "../schemas/imageVault"
import {
contentRefsSchema as sidebarContentRefsSchema,
contentSchema as sidebarContentSchema,
} from "../schemas/sidebar/content"
import { dynamicContentSchema as sidebarDynamicContentSchema } from "../schemas/sidebar/dynamicContent"
import {
joinLoyaltyContactRefsSchema,
joinLoyaltyContactSchema,
} from "../schemas/sidebar/joinLoyaltyContact"
import { systemSchema } from "../schemas/system"
// LoyaltyPage Refs
const extendedCardGridRefsSchema = z
.object({
__typename: z.literal(LoyaltyPageEnum.ContentStack.blocks.CardsGrid),
})
.merge(cardGridRefsSchema)
const extendedContentRefsSchema = z
.object({
__typename: z.literal(LoyaltyPageEnum.ContentStack.blocks.Content),
})
.merge(blockContentRefsSchema)
const extendedDynamicContentRefsSchema = z
.object({
__typename: z.literal(LoyaltyPageEnum.ContentStack.blocks.DynamicContent),
})
.merge(dynamicContentRefsSchema)
const extendedShortcutsRefsSchema = z
.object({
__typename: z.literal(LoyaltyPageEnum.ContentStack.blocks.Shortcuts),
})
.merge(shortcutsRefsSchema)
const blocksRefsSchema = z.discriminatedUnion("__typename", [
extendedCardGridRefsSchema,
extendedContentRefsSchema,
extendedDynamicContentRefsSchema,
extendedShortcutsRefsSchema,
])
const contentSidebarRefsSchema = z
.object({
__typename: z.literal(LoyaltyPageEnum.ContentStack.sidebar.Content),
})
.merge(sidebarContentRefsSchema)
const extendedJoinLoyaltyContactRefsSchema = z
.object({
__typename: z.literal(
LoyaltyPageEnum.ContentStack.sidebar.JoinLoyaltyContact
),
})
.merge(joinLoyaltyContactRefsSchema)
const sidebarRefsSchema = z.discriminatedUnion("__typename", [
contentSidebarRefsSchema,
extendedJoinLoyaltyContactRefsSchema,
z.object({
__typename: z.literal(LoyaltyPageEnum.ContentStack.sidebar.DynamicContent),
}),
])
export const loyaltyPageRefsSchema = z.object({
loyalty_page: z.object({
blocks: discriminatedUnionArray(blocksRefsSchema.options).optional(),
sidebar: discriminatedUnionArray(sidebarRefsSchema.options)
.optional()
.transform((data) => {
if (data) {
return data.filter(
(block) =>
block.__typename !==
LoyaltyPageEnum.ContentStack.sidebar.DynamicContent
)
}
return data
}),
system: systemSchema,
}),
})
// LoyaltyPage
export const extendedCardsGridSchema = z
.object({
__typename: z.literal(LoyaltyPageEnum.ContentStack.blocks.CardsGrid),
})
.merge(cardsGridSchema)
export const extendedContentSchema = z
.object({
__typename: z.literal(LoyaltyPageEnum.ContentStack.blocks.Content),
})
.merge(blockContentSchema)
export const extendedDynamicContentSchema = z
.object({
__typename: z.literal(LoyaltyPageEnum.ContentStack.blocks.DynamicContent),
})
.merge(blockDynamicContentSchema)
export const extendedShortcutsSchema = z
.object({
__typename: z.literal(LoyaltyPageEnum.ContentStack.blocks.Shortcuts),
})
.merge(shortcutsSchema)
export const blocksSchema = z.discriminatedUnion("__typename", [
extendedCardsGridSchema,
extendedContentSchema,
extendedDynamicContentSchema,
extendedShortcutsSchema,
])
const contentSidebarSchema = z
.object({
__typename: z.literal(LoyaltyPageEnum.ContentStack.sidebar.Content),
})
.merge(sidebarContentSchema)
const dynamicContentSidebarSchema = z
.object({
__typename: z.literal(LoyaltyPageEnum.ContentStack.sidebar.DynamicContent),
})
.merge(sidebarDynamicContentSchema)
export const joinLoyaltyContactSidebarSchema = z
.object({
__typename: z.literal(
LoyaltyPageEnum.ContentStack.sidebar.JoinLoyaltyContact
),
})
.merge(joinLoyaltyContactSchema)
export const sidebarSchema = z.discriminatedUnion("__typename", [
contentSidebarSchema,
dynamicContentSidebarSchema,
joinLoyaltyContactSidebarSchema,
])
export const loyaltyPageSchema = z.object({
loyalty_page: z
.object({
blocks: discriminatedUnionArray(blocksSchema.options).nullable(),
heading: z.string().optional(),
hero_image: tempImageVaultAssetSchema,
preamble: z.string().optional(),
sidebar: discriminatedUnionArray(sidebarSchema.options).nullable(),
title: z.string().optional(),
system: systemSchema.merge(
z.object({
created_at: z.string(),
updated_at: z.string(),
})
),
})
.transform((data) => {
return {
blocks: data.blocks ? data.blocks : [],
heading: data.heading,
heroImage: data.hero_image,
preamble: data.preamble,
sidebar: data.sidebar ? data.sidebar : [],
system: data.system,
}
}),
trackingProps: z.object({
url: z.string(),
}),
})

View File

@@ -0,0 +1,124 @@
import { createCounter } from "@scandic-hotels/common/telemetry"
import { notFound } from "@scandic-hotels/trpc/errors"
import { contentstackExtendedProcedureUID } from "@scandic-hotels/trpc/procedures"
import { router } from "../../.."
import {
GetLoyaltyPage,
GetLoyaltyPageRefs,
} from "../../../graphql/Query/LoyaltyPage/LoyaltyPage.graphql"
import { request } from "../../../graphql/request"
import {
generateRefsResponseTag,
generateTag,
generateTagsFromSystem,
} from "../../../utils/generateTag"
import { loyaltyPageRefsSchema, loyaltyPageSchema } from "./output"
import { getConnections } from "./utils"
import type {
GetLoyaltyPageRefsSchema,
GetLoyaltyPageSchema,
} from "../../../types/loyaltyPage"
import type { TrackingPageData } from "../../types"
export const loyaltyPageQueryRouter = router({
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
const { lang, uid } = ctx
const getLoyaltyPageRefsCounter = createCounter(
"trpc.contentstack",
"loyaltyPage.get.refs"
)
const metricsGetLoyaltyPageRefs = getLoyaltyPageRefsCounter.init({
lang,
uid,
})
metricsGetLoyaltyPageRefs.start()
const variables = { locale: lang, uid }
const refsResponse = await request<GetLoyaltyPageRefsSchema>(
GetLoyaltyPageRefs,
variables,
{
key: generateRefsResponseTag(lang, uid),
ttl: "max",
}
)
if (!refsResponse.data) {
const notFoundError = notFound(refsResponse)
metricsGetLoyaltyPageRefs.noDataError()
throw notFoundError
}
const validatedLoyaltyPageRefs = loyaltyPageRefsSchema.safeParse(
refsResponse.data
)
if (!validatedLoyaltyPageRefs.success) {
metricsGetLoyaltyPageRefs.validationError(validatedLoyaltyPageRefs.error)
return null
}
metricsGetLoyaltyPageRefs.success()
const connections = getConnections(validatedLoyaltyPageRefs.data)
const tags = [
generateTagsFromSystem(lang, connections),
generateTag(lang, validatedLoyaltyPageRefs.data.loyalty_page.system.uid),
].flat()
const getLoyaltyPageCounter = createCounter(
"trpc.contentstack",
"loyaltyPage.get"
)
const metricsGetLoyaltyPage = getLoyaltyPageCounter.init({ lang, uid })
metricsGetLoyaltyPage.start()
const response = await request<GetLoyaltyPageSchema>(
GetLoyaltyPage,
variables,
{
key: tags,
ttl: "max",
}
)
if (!response.data) {
const notFoundError = notFound(response)
metricsGetLoyaltyPage.noDataError()
throw notFoundError
}
const validatedLoyaltyPage = loyaltyPageSchema.safeParse(response.data)
if (!validatedLoyaltyPage.success) {
metricsGetLoyaltyPage.validationError(validatedLoyaltyPage.error)
return null
}
const loyaltyPage = validatedLoyaltyPage.data.loyalty_page
const tracking: TrackingPageData = {
pageId: loyaltyPage.system.uid,
domainLanguage: lang,
publishDate: loyaltyPage.system.updated_at,
createDate: loyaltyPage.system.created_at,
channel: "scandic-friends",
pageType: "loyaltycontentpage",
pageName: validatedLoyaltyPage.data.trackingProps.url,
siteSections: validatedLoyaltyPage.data.trackingProps.url,
siteVersion: "new-web",
}
metricsGetLoyaltyPage.success()
// Assert LoyaltyPage type to get correct typings for RTE fields
return {
loyaltyPage,
tracking,
}
}),
})

View File

@@ -0,0 +1,63 @@
import { LoyaltyPageEnum } from "../../../enums/loyaltyPage"
import type { LoyaltyPageRefs } from "../../../types/loyaltyPage"
import type { System } from "../schemas/system"
export function getConnections({ loyalty_page }: LoyaltyPageRefs) {
const connections: System["system"][] = [loyalty_page.system]
if (loyalty_page.blocks) {
loyalty_page.blocks.forEach((block) => {
switch (block.__typename) {
case LoyaltyPageEnum.ContentStack.blocks.CardsGrid:
if (block.cards_grid.length) {
connections.push(...block.cards_grid)
}
break
case LoyaltyPageEnum.ContentStack.blocks.Content:
if (block.content.length) {
// TS has trouble infering the filtered types
// @ts-ignore
connections.push(...block.content)
}
break
case LoyaltyPageEnum.ContentStack.blocks.DynamicContent:
if (block.dynamic_content.link) {
connections.push(block.dynamic_content.link)
}
break
case LoyaltyPageEnum.ContentStack.blocks.Shortcuts:
if (block.shortcuts.shortcuts.length) {
connections.push(...block.shortcuts.shortcuts)
}
break
default:
break
}
})
}
if (loyalty_page.sidebar) {
loyalty_page.sidebar.forEach((block) => {
switch (block?.__typename) {
case LoyaltyPageEnum.ContentStack.sidebar.Content:
if (block.content.length) {
// TS has trouble infering the filtered types
// @ts-ignore
connections.push(...block.content)
}
break
case LoyaltyPageEnum.ContentStack.sidebar.JoinLoyaltyContact:
if (block.join_loyalty_contact?.button) {
connections.push(block.join_loyalty_contact.button)
}
break
default:
break
}
})
}
return connections
}