feat(SW-1442): added destination overview page * feat(SW-1442): added destination overview page Approved-by: Fredrik Thorsson Approved-by: Matilda Landström
191 lines
5.3 KiB
TypeScript
191 lines
5.3 KiB
TypeScript
import {
|
|
GetDestinationOverviewPage,
|
|
GetDestinationOverviewPageRefs,
|
|
} from "@/lib/graphql/Query/DestinationOverviewPage/DestinationOverviewPage.graphql"
|
|
import { request } from "@/lib/graphql/request"
|
|
import { notFound } from "@/server/errors/trpc"
|
|
import { contentstackExtendedProcedureUID, router } from "@/server/trpc"
|
|
|
|
import { generateTag } from "@/utils/generateTag"
|
|
|
|
import {
|
|
destinationOverviewPageRefsSchema,
|
|
destinationOverviewPageSchema,
|
|
} from "./output"
|
|
import {
|
|
getDestinationOverviewPageCounter,
|
|
getDestinationOverviewPageFailCounter,
|
|
getDestinationOverviewPageRefsCounter,
|
|
getDestinationOverviewPageRefsFailCounter,
|
|
getDestinationOverviewPageRefsSuccessCounter,
|
|
getDestinationOverviewPageSuccessCounter,
|
|
} from "./telemetry"
|
|
|
|
import {
|
|
TrackingChannelEnum,
|
|
type TrackingSDKPageData,
|
|
} from "@/types/components/tracking"
|
|
import type {
|
|
GetDestinationOverviewPageData,
|
|
GetDestinationOverviewPageRefsSchema,
|
|
} from "@/types/trpc/routers/contentstack/destinationOverviewPage"
|
|
|
|
export const destinationOverviewPageQueryRouter = router({
|
|
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
|
const { lang, uid } = ctx
|
|
|
|
getDestinationOverviewPageRefsCounter.add(1, { lang, uid: `${uid}` })
|
|
console.info(
|
|
"contentstack.destinationOverviewPage.refs start",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
})
|
|
)
|
|
const refsResponse = await request<GetDestinationOverviewPageRefsSchema>(
|
|
GetDestinationOverviewPageRefs,
|
|
{
|
|
locale: lang,
|
|
uid,
|
|
},
|
|
{
|
|
cache: "force-cache",
|
|
next: {
|
|
tags: [generateTag(lang, uid)],
|
|
},
|
|
}
|
|
)
|
|
if (!refsResponse.data) {
|
|
const notFoundError = notFound(refsResponse)
|
|
getDestinationOverviewPageRefsFailCounter.add(1, {
|
|
lang,
|
|
uid,
|
|
error_type: "not_found",
|
|
error: JSON.stringify({ code: notFoundError.code }),
|
|
})
|
|
console.error(
|
|
"contentstack.destinationOverviewPage.refs not found error",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
error: { code: notFoundError.code },
|
|
})
|
|
)
|
|
throw notFoundError
|
|
}
|
|
|
|
const validatedRefsData = destinationOverviewPageRefsSchema.safeParse(
|
|
refsResponse.data
|
|
)
|
|
|
|
if (!validatedRefsData.success) {
|
|
getDestinationOverviewPageRefsFailCounter.add(1, {
|
|
lang,
|
|
uid,
|
|
error_type: "validation_error",
|
|
error: JSON.stringify(validatedRefsData.error),
|
|
})
|
|
console.error(
|
|
"contentstack.destinationOverviewPage.refs validation error",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
error: validatedRefsData.error,
|
|
})
|
|
)
|
|
return null
|
|
}
|
|
|
|
getDestinationOverviewPageRefsSuccessCounter.add(1, { lang, uid: `${uid}` })
|
|
console.info(
|
|
"contentstack.destinationOverviewPage.refs success",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
})
|
|
)
|
|
|
|
getDestinationOverviewPageCounter.add(1, { lang, uid: `${uid}` })
|
|
console.info(
|
|
"contentstack.destinationOverviewPage start",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
})
|
|
)
|
|
const response = await request<GetDestinationOverviewPageData>(
|
|
GetDestinationOverviewPage,
|
|
{
|
|
locale: lang,
|
|
uid,
|
|
},
|
|
{
|
|
cache: "force-cache",
|
|
next: {
|
|
tags: [generateTag(lang, uid)],
|
|
},
|
|
}
|
|
)
|
|
if (!response.data) {
|
|
const notFoundError = notFound(response)
|
|
getDestinationOverviewPageFailCounter.add(1, {
|
|
lang,
|
|
uid: `${uid}`,
|
|
error_type: "not_found",
|
|
error: JSON.stringify({ code: notFoundError.code }),
|
|
})
|
|
console.error(
|
|
"contentstack.destinationOverviewPage not found error",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
error: { code: notFoundError.code },
|
|
})
|
|
)
|
|
throw notFoundError
|
|
}
|
|
|
|
const destinationOverviewPage = destinationOverviewPageSchema.safeParse(
|
|
response.data
|
|
)
|
|
|
|
if (!destinationOverviewPage.success) {
|
|
getDestinationOverviewPageFailCounter.add(1, {
|
|
lang,
|
|
uid: `${uid}`,
|
|
error_type: "validation_error",
|
|
error: JSON.stringify(destinationOverviewPage.error),
|
|
})
|
|
console.error(
|
|
"contentstack.destinationOverviewPage validation error",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
error: destinationOverviewPage.error,
|
|
})
|
|
)
|
|
return null
|
|
}
|
|
|
|
getDestinationOverviewPageSuccessCounter.add(1, { lang, uid: `${uid}` })
|
|
console.info(
|
|
"contentstack.destinationOverviewPage success",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
})
|
|
)
|
|
|
|
const system = destinationOverviewPage.data.destination_overview_page.system
|
|
const tracking: TrackingSDKPageData = {
|
|
pageId: system.uid,
|
|
domainLanguage: lang,
|
|
publishDate: system.updated_at,
|
|
createDate: system.created_at,
|
|
channel: TrackingChannelEnum["destination-overview-page"],
|
|
pageType: "staticcontentpage",
|
|
pageName: destinationOverviewPage.data.trackingProps.url,
|
|
siteSections: destinationOverviewPage.data.trackingProps.url,
|
|
siteVersion: "new-web",
|
|
}
|
|
|
|
return {
|
|
destinationOverviewPage:
|
|
destinationOverviewPage.data.destination_overview_page,
|
|
tracking,
|
|
}
|
|
}),
|
|
})
|