feat(SW-266): Replacing static metadata with data from Contentstack on Loyalty pages and Account Pages
This commit is contained in:
@@ -6,6 +6,7 @@ import { breadcrumbsRouter } from "./breadcrumbs"
|
||||
import { hotelPageRouter } from "./hotelPage"
|
||||
import { languageSwitcherRouter } from "./languageSwitcher"
|
||||
import { loyaltyPageRouter } from "./loyaltyPage"
|
||||
import { metaDataRouter } from "./metadata"
|
||||
import { myPagesRouter } from "./myPages"
|
||||
|
||||
export const contentstackRouter = router({
|
||||
@@ -16,4 +17,5 @@ export const contentstackRouter = router({
|
||||
languageSwitcher: languageSwitcherRouter,
|
||||
loyaltyPage: loyaltyPageRouter,
|
||||
myPages: myPagesRouter,
|
||||
metaData: metaDataRouter,
|
||||
})
|
||||
|
||||
@@ -190,6 +190,7 @@ const loyaltyPageSidebarItem = z.discriminatedUnion("__typename", [
|
||||
])
|
||||
|
||||
export const validateLoyaltyPageSchema = z.object({
|
||||
title: z.string(),
|
||||
heading: z.string().nullable(),
|
||||
blocks: z.array(loyaltyPageBlockItem).nullable(),
|
||||
sidebar: z.array(loyaltyPageSidebarItem).nullable(),
|
||||
|
||||
@@ -209,6 +209,7 @@ export const loyaltyPageQueryRouter = router({
|
||||
: null
|
||||
|
||||
const loyaltyPage = {
|
||||
title: response.data.loyalty_page.title,
|
||||
heading: response.data.loyalty_page.heading,
|
||||
system: response.data.loyalty_page.system,
|
||||
blocks,
|
||||
|
||||
5
server/routers/contentstack/metadata/index.ts
Normal file
5
server/routers/contentstack/metadata/index.ts
Normal file
@@ -0,0 +1,5 @@
|
||||
import { mergeRouters } from "@/server/trpc"
|
||||
|
||||
import { metaDataQueryRouter } from "./query"
|
||||
|
||||
export const metaDataRouter = mergeRouters(metaDataQueryRouter)
|
||||
62
server/routers/contentstack/metadata/output.ts
Normal file
62
server/routers/contentstack/metadata/output.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { z } from "zod"
|
||||
|
||||
export const getMetaDataSchema = z.object({
|
||||
title: z.string().optional(),
|
||||
description: z.string().optional(),
|
||||
imageConnection: z
|
||||
.object({
|
||||
edges: z.array(
|
||||
z.object({
|
||||
node: z.object({
|
||||
url: z.string(),
|
||||
}),
|
||||
})
|
||||
),
|
||||
})
|
||||
.optional(),
|
||||
})
|
||||
|
||||
const page = z.object({
|
||||
web: z.object({
|
||||
seo_metadata: z.object({
|
||||
title: z.string().optional(),
|
||||
description: z.string().optional(),
|
||||
imageConnection: z
|
||||
.object({
|
||||
edges: z.array(
|
||||
z.object({
|
||||
node: z.object({
|
||||
url: z.string(),
|
||||
}),
|
||||
})
|
||||
),
|
||||
})
|
||||
.optional(),
|
||||
}),
|
||||
}),
|
||||
system: z.object({
|
||||
uid: z.string(),
|
||||
}),
|
||||
})
|
||||
|
||||
export type Page = z.infer<typeof page>
|
||||
|
||||
const metaDataItems = z.object({
|
||||
items: z.array(page),
|
||||
})
|
||||
|
||||
export const validateMyPagesMetaDataContentstackSchema = z.object({
|
||||
all_account_page: metaDataItems,
|
||||
})
|
||||
|
||||
export type GetMyPagesMetaDataData = z.infer<
|
||||
typeof validateMyPagesMetaDataContentstackSchema
|
||||
>
|
||||
|
||||
export const validateLoyaltyPageMetaDataContentstackSchema = z.object({
|
||||
all_loyalty_page: metaDataItems,
|
||||
})
|
||||
|
||||
export type GetLoyaltyPageMetaDataData = z.infer<
|
||||
typeof validateLoyaltyPageMetaDataContentstackSchema
|
||||
>
|
||||
78
server/routers/contentstack/metadata/query.ts
Normal file
78
server/routers/contentstack/metadata/query.ts
Normal file
@@ -0,0 +1,78 @@
|
||||
import { GetLoyaltyPageMetaData } from "@/lib/graphql/Query/MetaDataLoyaltyPage.graphql"
|
||||
import { GetMyPagesMetaData } from "@/lib/graphql/Query/MetaDataMyPages.graphql"
|
||||
import { contentstackExtendedProcedureUID, router } from "@/server/trpc"
|
||||
|
||||
import {
|
||||
type GetLoyaltyPageMetaDataData,
|
||||
type GetMyPagesMetaDataData,
|
||||
validateLoyaltyPageMetaDataContentstackSchema,
|
||||
validateMyPagesMetaDataContentstackSchema,
|
||||
} from "./output"
|
||||
import { getMetaData, getResponse, Variables } from "./utils"
|
||||
|
||||
import { PageTypeEnum } from "@/types/requests/pageType"
|
||||
|
||||
async function getLoyaltyPageMetaData(variables: Variables) {
|
||||
const response = await getResponse<GetLoyaltyPageMetaDataData>(
|
||||
GetLoyaltyPageMetaData,
|
||||
variables
|
||||
)
|
||||
|
||||
if (!response.data.all_loyalty_page.items[0].web?.seo_metadata?.title) {
|
||||
return null
|
||||
}
|
||||
const validatedMetaDataData =
|
||||
validateLoyaltyPageMetaDataContentstackSchema.safeParse(response.data)
|
||||
|
||||
if (!validatedMetaDataData.success) {
|
||||
console.error(
|
||||
`Failed to validate Loyaltypage MetaData Data - (url: ${variables.url})`
|
||||
)
|
||||
console.error(validatedMetaDataData.error)
|
||||
return null
|
||||
}
|
||||
|
||||
return getMetaData(validatedMetaDataData.data.all_loyalty_page.items[0])
|
||||
}
|
||||
|
||||
async function getMyPagesMetaData(variables: Variables) {
|
||||
const response = await getResponse<GetMyPagesMetaDataData>(
|
||||
GetMyPagesMetaData,
|
||||
variables
|
||||
)
|
||||
|
||||
if (!response.data.all_account_page.items[0].web?.seo_metadata?.title) {
|
||||
return []
|
||||
}
|
||||
|
||||
const validatedMetaDataData =
|
||||
validateMyPagesMetaDataContentstackSchema.safeParse(response.data)
|
||||
|
||||
if (!validatedMetaDataData.success) {
|
||||
console.error(
|
||||
`Failed to validate My Page MetaData Data - (url: ${variables.url})`
|
||||
)
|
||||
console.error(validatedMetaDataData.error)
|
||||
return null
|
||||
}
|
||||
|
||||
return getMetaData(validatedMetaDataData.data.all_account_page.items[0])
|
||||
}
|
||||
|
||||
export const metaDataQueryRouter = router({
|
||||
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
||||
const variables = {
|
||||
locale: ctx.lang,
|
||||
url: ctx.pathname,
|
||||
}
|
||||
|
||||
switch (ctx.contentType) {
|
||||
case PageTypeEnum.accountPage:
|
||||
return await getMyPagesMetaData(variables)
|
||||
case PageTypeEnum.loyaltyPage:
|
||||
return await getLoyaltyPageMetaData(variables)
|
||||
default:
|
||||
return []
|
||||
}
|
||||
}),
|
||||
})
|
||||
34
server/routers/contentstack/metadata/utils.ts
Normal file
34
server/routers/contentstack/metadata/utils.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { Lang } from "@/constants/languages"
|
||||
import { request } from "@/lib/graphql/request"
|
||||
import { internalServerError, notFound } from "@/server/errors/trpc"
|
||||
|
||||
import { getMetaDataSchema, Page } from "./output"
|
||||
|
||||
export type Variables = {
|
||||
locale: Lang
|
||||
url: string
|
||||
}
|
||||
|
||||
export async function getResponse<T>(query: string, variables: Variables) {
|
||||
const response = await request<T>(query, variables)
|
||||
if (!response.data) {
|
||||
throw notFound(response)
|
||||
}
|
||||
|
||||
return response
|
||||
}
|
||||
|
||||
export function getMetaData(page: Page) {
|
||||
const pageMetaData = {
|
||||
title: page.web.seo_metadata.title,
|
||||
description: page.web.seo_metadata.description,
|
||||
imageConnection: page.web.seo_metadata.imageConnection,
|
||||
uid: page.system.uid,
|
||||
}
|
||||
const validatedMetaData = getMetaDataSchema.safeParse(pageMetaData)
|
||||
if (!validatedMetaData.success) {
|
||||
throw internalServerError(validatedMetaData.error)
|
||||
}
|
||||
|
||||
return validatedMetaData.data
|
||||
}
|
||||
Reference in New Issue
Block a user