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
79 lines
2.2 KiB
TypeScript
79 lines
2.2 KiB
TypeScript
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
|
|
import { router } from "../../.."
|
|
import { notFoundError } from "../../../errors"
|
|
import { GetCampaignPage } from "../../../graphql/Query/CampaignPage/CampaignPage.graphql"
|
|
import { request } from "../../../graphql/request"
|
|
import { contentStackUidWithServiceProcedure } from "../../../procedures"
|
|
import { generateTag } from "../../../utils/generateTag"
|
|
import { campaignPageSchema } from "./output"
|
|
|
|
import type { GetCampaignPageData } from "../../../types/campaignPage"
|
|
import type { TrackingPageData } from "../../types"
|
|
|
|
export const campaignPageQueryRouter = router({
|
|
get: contentStackUidWithServiceProcedure.query(async ({ ctx }) => {
|
|
const { lang, uid } = ctx
|
|
|
|
const cacheKey = generateTag(lang, uid)
|
|
|
|
const getCampaignPageCounter = createCounter(
|
|
"trpc.contentstack.campaignPage.get"
|
|
)
|
|
const metricsGetCampaignPage = getCampaignPageCounter.init({
|
|
lang,
|
|
uid,
|
|
})
|
|
|
|
metricsGetCampaignPage.start()
|
|
|
|
const variables = { locale: lang, uid }
|
|
const response = await request<GetCampaignPageData>(
|
|
GetCampaignPage,
|
|
variables,
|
|
{
|
|
key: `${cacheKey}:campaignPage`,
|
|
ttl: "max",
|
|
}
|
|
)
|
|
if (!response.data) {
|
|
metricsGetCampaignPage.noDataError()
|
|
throw notFoundError({
|
|
message: "GetCampaignPage returned no data",
|
|
errorDetails: variables,
|
|
})
|
|
}
|
|
|
|
const validatedResponse = campaignPageSchema.safeParse(response.data)
|
|
|
|
if (!validatedResponse.success) {
|
|
metricsGetCampaignPage.validationError(validatedResponse.error)
|
|
return null
|
|
}
|
|
|
|
const { campaign_page, trackingProps } = validatedResponse.data
|
|
|
|
metricsGetCampaignPage.success()
|
|
|
|
const system = campaign_page.system
|
|
const pageName = trackingProps.url
|
|
|
|
const tracking: TrackingPageData = {
|
|
pageId: system.uid,
|
|
domainLanguage: system.locale,
|
|
publishDate: system.updated_at,
|
|
createDate: system.created_at,
|
|
channel: "campaign",
|
|
pageType: "campaigncollectionpage",
|
|
pageName,
|
|
siteSections: pageName,
|
|
siteVersion: "new-web",
|
|
}
|
|
|
|
return {
|
|
campaign_page,
|
|
tracking,
|
|
}
|
|
}),
|
|
})
|