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:
@@ -2,64 +2,20 @@ import { createCounter } from "@scandic-hotels/common/telemetry"
|
||||
|
||||
import { router } from "../../.."
|
||||
import { notFoundError } from "../../../errors"
|
||||
import {
|
||||
GetCampaignPage,
|
||||
GetCampaignPageRefs,
|
||||
} from "../../../graphql/Query/CampaignPage/CampaignPage.graphql"
|
||||
import { GetCampaignPage } from "../../../graphql/Query/CampaignPage/CampaignPage.graphql"
|
||||
import { request } from "../../../graphql/request"
|
||||
import { contentStackUidWithServiceProcedure } from "../../../procedures"
|
||||
import { generateRefsResponseTag } from "../../../utils/generateTag"
|
||||
import { campaignPageRefsSchema, campaignPageSchema } from "./output"
|
||||
import { generatePageTags } from "./utils"
|
||||
import { generateTag } from "../../../utils/generateTag"
|
||||
import { campaignPageSchema } from "./output"
|
||||
|
||||
import type {
|
||||
GetCampaignPageData,
|
||||
GetCampaignPageRefsData,
|
||||
} from "../../../types/campaignPage"
|
||||
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 getCampaignPageRefsCounter = createCounter(
|
||||
"trpc.contentstack.campaignPage.get.refs"
|
||||
)
|
||||
const metricsGetCampaignPageRefs = getCampaignPageRefsCounter.init({
|
||||
lang,
|
||||
uid,
|
||||
})
|
||||
|
||||
metricsGetCampaignPageRefs.start()
|
||||
|
||||
const refVariables = { locale: lang, uid }
|
||||
const refsResponse = await request<GetCampaignPageRefsData>(
|
||||
GetCampaignPageRefs,
|
||||
refVariables,
|
||||
{
|
||||
key: generateRefsResponseTag(lang, uid),
|
||||
ttl: "max",
|
||||
}
|
||||
)
|
||||
if (!refsResponse.data) {
|
||||
metricsGetCampaignPageRefs.noDataError()
|
||||
throw notFoundError({
|
||||
message: "GetCampaignPageRefs returned no data",
|
||||
errorDetails: refVariables,
|
||||
})
|
||||
}
|
||||
|
||||
const validatedRefsData = campaignPageRefsSchema.safeParse(
|
||||
refsResponse.data
|
||||
)
|
||||
if (!validatedRefsData.success) {
|
||||
metricsGetCampaignPageRefs.validationError(validatedRefsData.error)
|
||||
return null
|
||||
}
|
||||
|
||||
metricsGetCampaignPageRefs.success()
|
||||
|
||||
const tags = generatePageTags(validatedRefsData.data, lang)
|
||||
const cacheKey = generateTag(lang, uid)
|
||||
|
||||
const getCampaignPageCounter = createCounter(
|
||||
"trpc.contentstack.campaignPage.get"
|
||||
@@ -76,7 +32,7 @@ export const campaignPageQueryRouter = router({
|
||||
GetCampaignPage,
|
||||
variables,
|
||||
{
|
||||
key: tags,
|
||||
key: `${cacheKey}:campaignPage`,
|
||||
ttl: "max",
|
||||
}
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user