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:
@@ -3,14 +3,9 @@ import { z } from "zod"
|
||||
import { transformedImageVaultAssetSchema } from "@scandic-hotels/common/utils/imageVault"
|
||||
import { IconName } from "@scandic-hotels/design-system/Icons/iconName"
|
||||
|
||||
import {
|
||||
linkConnectionRefs,
|
||||
linkConnectionSchema,
|
||||
} from "../schemas/linkConnection"
|
||||
import { systemSchema } from "../schemas/system"
|
||||
import { linkConnectionSchema } from "../schemas/linkConnection"
|
||||
|
||||
export type UsePointsModalData = z.output<typeof usePointsModalSchema>
|
||||
export type UsePointsModalRefsData = z.output<typeof usePointsModalRefsSchema>
|
||||
type LinkGroupItem = z.infer<typeof linkGroupItemSchema>
|
||||
|
||||
const linkGroupItemSchema = z.intersection(
|
||||
@@ -57,16 +52,3 @@ export const usePointsModalSchema = z.object({
|
||||
})
|
||||
.optional(),
|
||||
})
|
||||
|
||||
export const usePointsModalRefsSchema = z.object({
|
||||
all_usepointsmodal: z
|
||||
.object({
|
||||
items: z.array(
|
||||
z.object({
|
||||
link_group: z.array(linkConnectionRefs),
|
||||
system: systemSchema,
|
||||
})
|
||||
),
|
||||
})
|
||||
.optional(),
|
||||
})
|
||||
|
||||
@@ -2,66 +2,15 @@ import { createCounter } from "@scandic-hotels/common/telemetry"
|
||||
|
||||
import { router } from "../../.."
|
||||
import { notFoundError } from "../../../errors"
|
||||
import {
|
||||
GetUsePointsModal,
|
||||
GetUsePointsModalRefs,
|
||||
} from "../../../graphql/Query/UsePointsModal.graphql"
|
||||
import { GetUsePointsModal } from "../../../graphql/Query/UsePointsModal.graphql"
|
||||
import { request } from "../../../graphql/request"
|
||||
import { contentstackBaseProcedure } from "../../../procedures"
|
||||
import {
|
||||
generateRefsResponseTag,
|
||||
generateTag,
|
||||
generateTagsFromSystem,
|
||||
} from "../../../utils/generateTag"
|
||||
import {
|
||||
type UsePointsModalData,
|
||||
type UsePointsModalRefsData,
|
||||
usePointsModalRefsSchema,
|
||||
usePointsModalSchema,
|
||||
} from "./output"
|
||||
import { getConnections } from "./utils"
|
||||
import { generateTag } from "../../../utils/generateTag"
|
||||
import { type UsePointsModalData, usePointsModalSchema } from "./output"
|
||||
|
||||
export const usePointsModalQueryRouter = router({
|
||||
get: contentstackBaseProcedure.query(async ({ ctx }) => {
|
||||
const { lang, uid } = ctx
|
||||
const getRefsCounter = createCounter(
|
||||
"trpc.contentstack.usePointsModal.get.refs"
|
||||
)
|
||||
const metricsRefs = getRefsCounter.init({
|
||||
lang,
|
||||
uid,
|
||||
})
|
||||
|
||||
metricsRefs.start()
|
||||
const refsTag = generateRefsResponseTag(lang, "usepointsmodal")
|
||||
|
||||
const variables = { locale: lang }
|
||||
const refsResponse = await request<UsePointsModalRefsData>(
|
||||
GetUsePointsModalRefs,
|
||||
variables,
|
||||
{
|
||||
key: refsTag,
|
||||
ttl: "max",
|
||||
}
|
||||
)
|
||||
if (!refsResponse.data) {
|
||||
metricsRefs.noDataError()
|
||||
throw notFoundError({
|
||||
message: "GetUsePointsModalRefs returned no data",
|
||||
errorDetails: variables,
|
||||
})
|
||||
}
|
||||
|
||||
const validatedRefsData = usePointsModalRefsSchema.safeParse(
|
||||
refsResponse.data
|
||||
)
|
||||
|
||||
if (!validatedRefsData.success) {
|
||||
metricsRefs.validationError(validatedRefsData.error)
|
||||
return null
|
||||
}
|
||||
|
||||
metricsRefs.success()
|
||||
|
||||
const getCounter = createCounter("trpc.contentstack.usePointsModal.get")
|
||||
|
||||
@@ -71,18 +20,13 @@ export const usePointsModalQueryRouter = router({
|
||||
})
|
||||
metrics.start()
|
||||
|
||||
const connections = getConnections(validatedRefsData.data)
|
||||
|
||||
const tags = [
|
||||
generateTagsFromSystem(lang, connections),
|
||||
generateTag(lang, "usepointsmodal"),
|
||||
].flat()
|
||||
const variables = { locale: lang }
|
||||
|
||||
const response = await request<UsePointsModalData>(
|
||||
GetUsePointsModal,
|
||||
variables,
|
||||
{
|
||||
key: tags,
|
||||
key: generateTag(lang, "usepointsmodal"),
|
||||
ttl: "max",
|
||||
}
|
||||
)
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
import type { System } from "../schemas/system"
|
||||
import type { UsePointsModalRefsData } from "./output"
|
||||
|
||||
export function getConnections({ all_usepointsmodal }: UsePointsModalRefsData) {
|
||||
const connections: System["system"][] = all_usepointsmodal?.items?.[0]
|
||||
? [all_usepointsmodal?.items?.[0]?.system]
|
||||
: []
|
||||
|
||||
if (all_usepointsmodal && all_usepointsmodal.items) {
|
||||
all_usepointsmodal?.items.forEach((item) => {
|
||||
item.link_group.forEach((link) => {
|
||||
link.link?.uid && connections.push(link.link)
|
||||
})
|
||||
})
|
||||
}
|
||||
return connections
|
||||
}
|
||||
Reference in New Issue
Block a user