Files
web/apps/scandic-web/server/routers/contentstack/accountPage/query.ts
Joakim Jäderberg fa63b20ed0 Merged in feature/redis (pull request #1478)
Distributed cache

* cache deleteKey now uses an options object instead of a lonely argument variable fuzzy

* merge

* remove debug logs and cleanup

* cleanup

* add fault handling

* add fault handling

* add pid when logging redis client creation

* add identifier when logging redis client creation

* cleanup

* feat: add redis-api as it's own app

* feature: use http wrapper for redis

* feat: add the possibility to fallback to unstable_cache

* Add error handling if redis cache is unresponsive

* add logging for unstable_cache

* merge

* don't cache errors

* fix: metadatabase on branchdeploys

* Handle when /en/destinations throws
add ErrorBoundary

* Add sentry-logging when ErrorBoundary catches exception

* Fix error handling for distributed cache

* cleanup code

* Added Application Insights back

* Update generateApiKeys script and remove duplicate

* Merge branch 'feature/redis' of bitbucket.org:scandic-swap/web into feature/redis

* merge


Approved-by: Linus Flood
2025-03-14 07:54:21 +00:00

197 lines
5.5 KiB
TypeScript

import { metrics } from "@opentelemetry/api"
import {
GetAccountPage,
GetAccountPageRefs,
} from "@/lib/graphql/Query/AccountPage/AccountPage.graphql"
import { request } from "@/lib/graphql/request"
import { notFound } from "@/server/errors/trpc"
import { contentstackExtendedProcedureUID, router } from "@/server/trpc"
import {
generateRefsResponseTag,
generateTag,
generateTagsFromSystem,
} from "@/utils/generateTag"
import { accountPageRefsSchema, accountPageSchema } from "./output"
import { getConnections } from "./utils"
import {
TrackingChannelEnum,
type TrackingSDKPageData,
} from "@/types/components/tracking"
import type {
GetAccountPageRefsSchema,
GetAccountPageSchema,
} from "@/types/trpc/routers/contentstack/accountPage"
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<GetAccountPageRefsSchema>(
GetAccountPageRefs,
{
locale: lang,
uid,
},
{
key: generateRefsResponseTag(lang, uid),
ttl: "max",
}
)
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 validatedAccountPageRefs = accountPageRefsSchema.safeParse(
refsResponse.data
)
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 = [
generateTagsFromSystem(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<GetAccountPageSchema>(
GetAccountPage,
{
locale: lang,
uid,
},
{
key: tags,
ttl: "max",
}
)
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 = accountPageSchema.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 } })
)
const parsedtitle = response.data.account_page.title
.replaceAll(" ", "")
.toLowerCase()
const tracking: TrackingSDKPageData = {
pageId: validatedAccountPage.data.account_page.system.uid,
domainLanguage: lang,
publishDate: validatedAccountPage.data.account_page.system.updated_at,
createDate: validatedAccountPage.data.account_page.system.created_at,
channel: TrackingChannelEnum["scandic-friends"],
pageType: `member${parsedtitle}page`,
pageName: validatedAccountPage.data.trackingProps.url,
siteSections: validatedAccountPage.data.trackingProps.url,
siteVersion: "new-web",
}
return {
accountPage: validatedAccountPage.data.account_page,
tracking,
}
}),
})