Merged in feat/rework-contentstack (pull request #3493)
Feat(SW-3708): refactor contentstack fetching (removing all refs) and cache invalidation * Remove all REFS * Revalidate correct language * PR fixes * PR fixes * Throw when errors from contentstack api Approved-by: Joakim Jäderberg
This commit is contained in:
@@ -4,18 +4,9 @@ import { transformedImageVaultAssetSchema } from "@scandic-hotels/common/utils/i
|
||||
|
||||
import { AccountPageEnum } from "../../../types/accountPageEnum"
|
||||
import { discriminatedUnionArray } from "../../../utils/discriminatedUnion"
|
||||
import {
|
||||
accordionRefsSchema,
|
||||
accordionSchema,
|
||||
} from "../schemas/blocks/accordion"
|
||||
import {
|
||||
dynamicContentRefsSchema,
|
||||
dynamicContentSchema,
|
||||
} from "../schemas/blocks/dynamicContent"
|
||||
import {
|
||||
shortcutsRefsSchema,
|
||||
shortcutsSchema,
|
||||
} from "../schemas/blocks/shortcuts"
|
||||
import { accordionSchema } from "../schemas/blocks/accordion"
|
||||
import { dynamicContentSchema } from "../schemas/blocks/dynamicContent"
|
||||
import { shortcutsSchema } from "../schemas/blocks/shortcuts"
|
||||
import { textContentSchema } from "../schemas/blocks/textContent"
|
||||
import { systemSchema } from "../schemas/system"
|
||||
|
||||
@@ -73,37 +64,3 @@ export const accountPageSchema = z.object({
|
||||
url: z.string(),
|
||||
}),
|
||||
})
|
||||
|
||||
const accountPageAccordionRefs = z
|
||||
.object({
|
||||
__typename: z.literal(AccountPageEnum.ContentStack.blocks.Accordion),
|
||||
})
|
||||
.merge(accordionRefsSchema)
|
||||
|
||||
const accountPageDynamicContentRefs = z
|
||||
.object({
|
||||
__typename: z.literal(AccountPageEnum.ContentStack.blocks.DynamicContent),
|
||||
})
|
||||
.merge(dynamicContentRefsSchema)
|
||||
|
||||
const accountPageShortcutsRefs = z
|
||||
.object({
|
||||
__typename: z.literal(AccountPageEnum.ContentStack.blocks.ShortCuts),
|
||||
})
|
||||
.merge(shortcutsRefsSchema)
|
||||
|
||||
const accountPageContentItemRefs = z.discriminatedUnion("__typename", [
|
||||
z.object({
|
||||
__typename: z.literal(AccountPageEnum.ContentStack.blocks.TextContent),
|
||||
}),
|
||||
accountPageAccordionRefs,
|
||||
accountPageDynamicContentRefs,
|
||||
accountPageShortcutsRefs,
|
||||
])
|
||||
|
||||
export const accountPageRefsSchema = z.object({
|
||||
account_page: z.object({
|
||||
content: discriminatedUnionArray(accountPageContentItemRefs.options),
|
||||
system: systemSchema,
|
||||
}),
|
||||
})
|
||||
|
||||
@@ -2,82 +2,31 @@ import { createCounter } from "@scandic-hotels/common/telemetry"
|
||||
|
||||
import { router } from "../../.."
|
||||
import { notFoundError } from "../../../errors"
|
||||
import {
|
||||
GetAccountPage,
|
||||
GetAccountPageRefs,
|
||||
} from "../../../graphql/Query/AccountPage/AccountPage.graphql"
|
||||
import { GetAccountPage } from "../../../graphql/Query/AccountPage/AccountPage.graphql"
|
||||
import { request } from "../../../graphql/request"
|
||||
import { contentstackExtendedProcedureUID } from "../../../procedures"
|
||||
import {
|
||||
generateRefsResponseTag,
|
||||
generateTag,
|
||||
generateTagsFromSystem,
|
||||
} from "../../../utils/generateTag"
|
||||
import { accountPageRefsSchema, accountPageSchema } from "./output"
|
||||
import { getConnections } from "./utils"
|
||||
import { generateTag } from "../../../utils/generateTag"
|
||||
import { accountPageSchema } from "./output"
|
||||
|
||||
import type {
|
||||
GetAccountPageRefsSchema,
|
||||
GetAccountPageSchema,
|
||||
} from "../../../types/accountPage"
|
||||
import type { GetAccountPageSchema } from "../../../types/accountPage"
|
||||
import type { TrackingPageData } from "../../types"
|
||||
|
||||
export const accountPageQueryRouter = router({
|
||||
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
||||
const { lang, uid } = ctx
|
||||
|
||||
const getAccountPageRefsCounter = createCounter(
|
||||
"trpc.contentstack.accountPage.get.refs"
|
||||
)
|
||||
const metricsRefs = getAccountPageRefsCounter.init({ lang, uid })
|
||||
metricsRefs.start()
|
||||
|
||||
const variables = { locale: lang, uid }
|
||||
const refsResponse = await request<GetAccountPageRefsSchema>(
|
||||
GetAccountPageRefs,
|
||||
variables,
|
||||
{
|
||||
key: generateRefsResponseTag(lang, uid),
|
||||
ttl: "max",
|
||||
}
|
||||
)
|
||||
|
||||
if (!refsResponse.data) {
|
||||
metricsRefs.noDataError()
|
||||
throw notFoundError({
|
||||
message: "GetAccountPageRefs returned no data",
|
||||
errorDetails: variables,
|
||||
})
|
||||
}
|
||||
|
||||
const validatedAccountPageRefs = accountPageRefsSchema.safeParse(
|
||||
refsResponse.data
|
||||
)
|
||||
if (!validatedAccountPageRefs.success) {
|
||||
metricsRefs.validationError(validatedAccountPageRefs.error)
|
||||
return null
|
||||
}
|
||||
|
||||
const connections = getConnections(validatedAccountPageRefs.data)
|
||||
|
||||
const tags = [
|
||||
generateTagsFromSystem(lang, connections),
|
||||
generateTag(lang, validatedAccountPageRefs.data.account_page.system.uid),
|
||||
].flat()
|
||||
|
||||
metricsRefs.success()
|
||||
|
||||
const getAccountPageCounter = createCounter(
|
||||
"trpc.contentstack.accountPage.get"
|
||||
)
|
||||
const metrics = getAccountPageCounter.init({ lang, uid })
|
||||
metrics.start()
|
||||
|
||||
const variables = { locale: lang, uid }
|
||||
const response = await request<GetAccountPageSchema>(
|
||||
GetAccountPage,
|
||||
variables,
|
||||
{
|
||||
key: tags,
|
||||
key: generateTag(lang, uid),
|
||||
ttl: "max",
|
||||
}
|
||||
)
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
import { AccountPageEnum } from "../../../types/accountPageEnum"
|
||||
|
||||
import type { AccountPageRefs } from "../../../types/accountPage"
|
||||
import type { System } from "../schemas/system"
|
||||
|
||||
export function getConnections({ account_page }: AccountPageRefs) {
|
||||
const connections: System["system"][] = [account_page.system]
|
||||
|
||||
if (account_page.content) {
|
||||
account_page.content.forEach((block) => {
|
||||
switch (block.__typename) {
|
||||
case AccountPageEnum.ContentStack.blocks.ShortCuts: {
|
||||
if (block.shortcuts.shortcuts.length) {
|
||||
connections.push(...block.shortcuts.shortcuts.filter((c) => !!c))
|
||||
}
|
||||
break
|
||||
}
|
||||
case AccountPageEnum.ContentStack.blocks.DynamicContent: {
|
||||
if (block.dynamic_content.link) {
|
||||
connections.push(block.dynamic_content.link)
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return connections
|
||||
}
|
||||
Reference in New Issue
Block a user