251 lines
7.2 KiB
TypeScript
251 lines
7.2 KiB
TypeScript
import { metrics } from "@opentelemetry/api"
|
|
|
|
import { Lang } from "@/constants/languages"
|
|
import {
|
|
GetAccountPage,
|
|
GetAccountPageRefs,
|
|
} from "@/lib/graphql/Query/AccountPage.graphql"
|
|
import { request } from "@/lib/graphql/request"
|
|
import { notFound } from "@/server/errors/trpc"
|
|
import { contentstackExtendedProcedureUID, router } from "@/server/trpc"
|
|
|
|
import {
|
|
generateRefsResponseTag,
|
|
generateTag,
|
|
generateTags,
|
|
} from "@/utils/generateTag"
|
|
|
|
import { removeEmptyObjects } from "../../utils"
|
|
import {
|
|
type AccountPage,
|
|
AccountPageDataRaw,
|
|
AccountPageRefsDataRaw,
|
|
validateAccountPageRefsSchema,
|
|
validateAccountPageSchema,
|
|
} from "./output"
|
|
import { getConnections } from "./utils"
|
|
|
|
import { ContentEntries } from "@/types/components/myPages/myPage/enums"
|
|
import {
|
|
TrackingChannelEnum,
|
|
TrackingSDKPageData,
|
|
} from "@/types/components/tracking"
|
|
import { Embeds } from "@/types/requests/embeds"
|
|
import { Edges } from "@/types/requests/utils/edges"
|
|
import { RTEDocument } from "@/types/rte/node"
|
|
|
|
const meter = metrics.getMeter("trpc.accountPage")
|
|
|
|
// OpenTelemetry metrics
|
|
const getAccountPageRefsCounter = meter.createCounter(
|
|
"trpc.contentstack.accountPage.get"
|
|
)
|
|
const getAccountPageRefsSuccessCounter = meter.createCounter(
|
|
"trpc.contentstack.accountPage.get-success"
|
|
)
|
|
const getAccountPageRefsFailCounter = meter.createCounter(
|
|
"trpc.contentstack.accountPage.get-fail"
|
|
)
|
|
const getAccountPageCounter = meter.createCounter(
|
|
"trpc.contentstack.accountPage.get"
|
|
)
|
|
const getAccountPageSuccessCounter = meter.createCounter(
|
|
"trpc.contentstack.accountPage.get-success"
|
|
)
|
|
const getAccountPageFailCounter = meter.createCounter(
|
|
"trpc.contentstack.accountPage.get-fail"
|
|
)
|
|
|
|
export const accountPageQueryRouter = router({
|
|
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
|
const { lang, uid } = ctx
|
|
getAccountPageRefsCounter.add(1, { lang, uid })
|
|
console.info(
|
|
"contentstack.accountPage.refs start",
|
|
JSON.stringify({ query: { lang, uid } })
|
|
)
|
|
const refsResponse = await request<AccountPageRefsDataRaw>(
|
|
GetAccountPageRefs,
|
|
{
|
|
locale: lang,
|
|
uid,
|
|
},
|
|
{
|
|
cache: "force-cache",
|
|
next: {
|
|
tags: [generateRefsResponseTag(lang, uid)],
|
|
},
|
|
}
|
|
)
|
|
|
|
if (!refsResponse.data) {
|
|
const notFoundError = notFound(refsResponse)
|
|
getAccountPageRefsFailCounter.add(1, {
|
|
lang,
|
|
uid,
|
|
error_type: "not_found",
|
|
error: JSON.stringify({ code: notFoundError.code }),
|
|
})
|
|
console.error(
|
|
"contentstack.accountPage.refs not found error",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
error: { code: notFoundError.code },
|
|
})
|
|
)
|
|
throw notFoundError
|
|
}
|
|
|
|
const cleanedData = removeEmptyObjects(refsResponse.data)
|
|
|
|
const validatedAccountPageRefs =
|
|
validateAccountPageRefsSchema.safeParse(cleanedData)
|
|
if (!validatedAccountPageRefs.success) {
|
|
getAccountPageRefsFailCounter.add(1, {
|
|
lang,
|
|
uid,
|
|
error_type: "validation_error",
|
|
error: JSON.stringify(validatedAccountPageRefs.error),
|
|
})
|
|
console.error(
|
|
"contentstack.accountPage.refs validation error",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
error: validatedAccountPageRefs.error,
|
|
})
|
|
)
|
|
return null
|
|
}
|
|
|
|
const connections = getConnections(validatedAccountPageRefs.data)
|
|
|
|
const tags = [
|
|
generateTags(lang, connections),
|
|
generateTag(lang, validatedAccountPageRefs.data.account_page.system.uid),
|
|
].flat()
|
|
getAccountPageRefsSuccessCounter.add(1, { lang, uid, tags })
|
|
getAccountPageCounter.add(1, { lang, uid })
|
|
console.info(
|
|
"contentstack.accountPage start",
|
|
JSON.stringify({ query: { lang, uid } })
|
|
)
|
|
const response = await request<AccountPageDataRaw>(
|
|
GetAccountPage,
|
|
{
|
|
locale: lang,
|
|
uid,
|
|
},
|
|
{
|
|
cache: "force-cache",
|
|
next: {
|
|
tags,
|
|
},
|
|
}
|
|
)
|
|
|
|
if (!response.data) {
|
|
const notFoundError = notFound(response)
|
|
getAccountPageFailCounter.add(1, {
|
|
lang,
|
|
uid,
|
|
error_type: "not_found",
|
|
error: JSON.stringify({ code: notFoundError.code }),
|
|
})
|
|
console.error(
|
|
"contentstack.accountPage not found error",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
error: { code: notFoundError.code },
|
|
})
|
|
)
|
|
throw notFoundError
|
|
}
|
|
|
|
const validatedAccountPage = validateAccountPageSchema.safeParse(
|
|
response.data
|
|
)
|
|
|
|
if (!validatedAccountPage.success) {
|
|
getAccountPageFailCounter.add(1, {
|
|
lang,
|
|
uid,
|
|
error_type: "validation_error",
|
|
error: JSON.stringify(validatedAccountPage.error),
|
|
})
|
|
console.error(
|
|
"contentstack.accountPage validation error",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
error: validatedAccountPage.error,
|
|
})
|
|
)
|
|
return null
|
|
}
|
|
getAccountPageSuccessCounter.add(1, { lang, uid })
|
|
console.info(
|
|
"contentstack.accountPage success",
|
|
JSON.stringify({ query: { lang, uid } })
|
|
)
|
|
// TODO: Make returned data nicer
|
|
const content = validatedAccountPage.data.account_page.content.map(
|
|
(block) => {
|
|
switch (block.__typename) {
|
|
case ContentEntries.AccountPageContentDynamicContent:
|
|
return block
|
|
case ContentEntries.AccountPageContentShortcuts:
|
|
return {
|
|
...block,
|
|
shortcuts: {
|
|
...block.shortcuts,
|
|
shortcuts: block.shortcuts.shortcuts.map((shortcut) => ({
|
|
text: shortcut.text,
|
|
openInNewTab: shortcut.open_in_new_tab,
|
|
...shortcut.linkConnection.edges[0].node,
|
|
url:
|
|
shortcut.linkConnection.edges[0].node.original_url ||
|
|
`/${shortcut.linkConnection.edges[0].node.system.locale}${shortcut.linkConnection.edges[0].node.url}`,
|
|
})),
|
|
},
|
|
}
|
|
case ContentEntries.AccountPageContentTextContent:
|
|
return {
|
|
...block,
|
|
text_content: {
|
|
content: {
|
|
json: block.text_content.content.json as RTEDocument,
|
|
embedded_itemsConnection: block.text_content.content
|
|
.embedded_itemsConnection as Edges<Embeds>,
|
|
},
|
|
},
|
|
}
|
|
default:
|
|
return null
|
|
}
|
|
}
|
|
)
|
|
|
|
const accountPage = {
|
|
...validatedAccountPage.data.account_page,
|
|
content,
|
|
} as AccountPage
|
|
|
|
const parsedtitle = response.data.account_page.title
|
|
.replaceAll(" ", "")
|
|
.toLowerCase()
|
|
|
|
const tracking: TrackingSDKPageData = {
|
|
pageId: validatedAccountPage.data.account_page.system.uid,
|
|
lang: validatedAccountPage.data.account_page.system.locale as Lang,
|
|
publishedDate: validatedAccountPage.data.account_page.system.updated_at,
|
|
createdDate: validatedAccountPage.data.account_page.system.created_at,
|
|
channel: TrackingChannelEnum["scandic-friends"],
|
|
pageType: `member${parsedtitle}page`,
|
|
}
|
|
|
|
return {
|
|
accountPage,
|
|
tracking,
|
|
}
|
|
}),
|
|
})
|