feat: add revlidation for loyalty page

This commit is contained in:
Christel Westerberg
2024-05-15 09:41:19 +02:00
parent a734ba848a
commit d651ea526c
8 changed files with 368 additions and 13 deletions
@@ -1,15 +1,28 @@
import GetLoyaltyPage from "@/lib/graphql/Query/LoyaltyPage.graphql"
import {
GetLoyaltyPage,
GetLoyaltyPageRefs,
} from "@/lib/graphql/Query/LoyaltyPage.graphql"
import { request } from "@/lib/graphql/request"
import { _ } from "@/lib/translation"
import { badRequestError } from "@/server/errors/trpc"
import { badRequestError, internalServerError } from "@/server/errors/trpc"
import { publicProcedure, router } from "@/server/trpc"
import { removeEmptyObjects } from "@/utils/contentType"
import {
generateRefsResponseTag,
generateTag,
generateTags,
} from "@/utils/generateTag"
import { getLoyaltyPageInput } from "./input"
import {
type LoyaltyPage,
type LoyaltyPageDataRaw,
type LoyaltyPageRefsDataRaw,
validateLoyaltyPageRefsSchema,
validateLoyaltyPageSchema,
} from "./output"
import { getConnections } from "./utils"
import {
LoyaltyBlocksTypenameEnum,
@@ -22,10 +35,56 @@ import { RTEDocument } from "@/types/rte/node"
export const loyaltyPageQueryRouter = router({
get: publicProcedure.input(getLoyaltyPageInput).query(async ({ input }) => {
try {
const loyaltyPageRes = await request<LoyaltyPageDataRaw>(GetLoyaltyPage, {
locale: input.locale,
url: input.href,
})
const { locale } = input
const refsResponse = await request<LoyaltyPageRefsDataRaw>(
GetLoyaltyPageRefs,
{
locale,
url: input.href,
},
{
next: {
tags: [generateRefsResponseTag(locale, "loyalty_page")],
},
}
)
if (!refsResponse.data) {
console.error("Bad response for `GetNavigationMyPagesRefs`")
console.error({ refsResponse })
throw internalServerError()
}
const cleanedData = removeEmptyObjects(refsResponse.data)
const validatedLoyaltyPageRefs =
validateLoyaltyPageRefsSchema.safeParse(cleanedData)
if (!validatedLoyaltyPageRefs.success) {
console.error("Bad validation for `GetLoyaltyPageRefs`")
console.error(validatedLoyaltyPageRefs.error)
throw badRequestError()
}
const connections = getConnections(validatedLoyaltyPageRefs.data)
const tags = generateTags(locale, connections)
tags.push(
generateTag(
locale,
validatedLoyaltyPageRefs.data.all_loyalty_page.items[0].system.uid
)
)
const loyaltyPageRes = await request<LoyaltyPageDataRaw>(
GetLoyaltyPage,
{
locale,
url: input.href,
},
{ next: { tags } }
)
if (!loyaltyPageRes.data) {
throw badRequestError()