fix: refs for account page
This commit is contained in:
@@ -2,6 +2,11 @@
|
|||||||
#import "../Fragments/MyPages/AccountPage/AccountPageContentShortcuts.graphql"
|
#import "../Fragments/MyPages/AccountPage/AccountPageContentShortcuts.graphql"
|
||||||
#import "../Fragments/MyPages/AccountPage/AccountPageContentTextContent.graphql"
|
#import "../Fragments/MyPages/AccountPage/AccountPageContentTextContent.graphql"
|
||||||
|
|
||||||
|
#import "../Fragments/Refs/AccountPage.graphql"
|
||||||
|
#import "../Fragments/Refs/ContentPage.graphql"
|
||||||
|
#import "../Fragments/Refs/LoyaltyPage.graphql"
|
||||||
|
#import "../Fragments/Refs/System.graphql"
|
||||||
|
|
||||||
query GetAccountPage($locale: String!, $url: String!) {
|
query GetAccountPage($locale: String!, $url: String!) {
|
||||||
all_account_page(limit: 1, locale: $locale, where: { url: $url }) {
|
all_account_page(limit: 1, locale: $locale, where: { url: $url }) {
|
||||||
items {
|
items {
|
||||||
@@ -17,3 +22,48 @@ query GetAccountPage($locale: String!, $url: String!) {
|
|||||||
total
|
total
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
query GetAccountPageRefs($locale: String!, $url: String!) {
|
||||||
|
all_account_page(limit: 1, locale: $locale, where: { url: $url }) {
|
||||||
|
items {
|
||||||
|
content {
|
||||||
|
... on AccountPageContentDynamicContent {
|
||||||
|
__typename
|
||||||
|
dynamic_content {
|
||||||
|
link {
|
||||||
|
linkConnection {
|
||||||
|
edges {
|
||||||
|
node {
|
||||||
|
__typename
|
||||||
|
...AccountPageRef
|
||||||
|
...LoyaltyPageRef
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
... on AccountPageContentShortcuts {
|
||||||
|
__typename
|
||||||
|
shortcuts {
|
||||||
|
shortcuts {
|
||||||
|
linkConnection {
|
||||||
|
edges {
|
||||||
|
node {
|
||||||
|
__typename
|
||||||
|
...AccountPageRef
|
||||||
|
...ContentPageRef
|
||||||
|
...LoyaltyPageRef
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
system {
|
||||||
|
...System
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import {
|
|||||||
DynamicContentComponents,
|
DynamicContentComponents,
|
||||||
} from "@/types/components/myPages/myPage/enums"
|
} from "@/types/components/myPages/myPage/enums"
|
||||||
import { Embeds } from "@/types/requests/embeds"
|
import { Embeds } from "@/types/requests/embeds"
|
||||||
|
import { PageLinkEnum } from "@/types/requests/pageLinks"
|
||||||
import { Edges } from "@/types/requests/utils/edges"
|
import { Edges } from "@/types/requests/utils/edges"
|
||||||
import { RTEDocument } from "@/types/rte/node"
|
import { RTEDocument } from "@/types/rte/node"
|
||||||
|
|
||||||
@@ -139,3 +140,61 @@ export type AccountPage = Omit<AccountPageRaw, "content"> & {
|
|||||||
title: string
|
title: string
|
||||||
content: AccountPageContentItem[]
|
content: AccountPageContentItem[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Refs types
|
||||||
|
const pageConnectionRefs = z.object({
|
||||||
|
edges: z.array(
|
||||||
|
z.object({
|
||||||
|
node: z.object({
|
||||||
|
__typename: z.nativeEnum(PageLinkEnum),
|
||||||
|
system: z.object({
|
||||||
|
content_type_uid: z.string(),
|
||||||
|
uid: z.string(),
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
),
|
||||||
|
})
|
||||||
|
|
||||||
|
const accountPageShortcutsRefs = z.object({
|
||||||
|
__typename: z.literal(ContentEntries.AccountPageContentShortcuts),
|
||||||
|
shortcuts: z.object({
|
||||||
|
shortcuts: z.array(
|
||||||
|
z.object({
|
||||||
|
linkConnection: pageConnectionRefs,
|
||||||
|
})
|
||||||
|
),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
const accountPageDynamicContentRefs = z.object({
|
||||||
|
__typename: z.literal(ContentEntries.AccountPageContentDynamicContent),
|
||||||
|
dynamic_content: z.object({
|
||||||
|
link: z.object({
|
||||||
|
linkConnection: pageConnectionRefs,
|
||||||
|
}),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
const accountPageContentItemRefs = z.discriminatedUnion("__typename", [
|
||||||
|
accountPageDynamicContentRefs,
|
||||||
|
accountPageShortcutsRefs,
|
||||||
|
])
|
||||||
|
|
||||||
|
export const validateAccountPageRefsSchema = z.object({
|
||||||
|
all_account_page: z.object({
|
||||||
|
items: z.array(
|
||||||
|
z.object({
|
||||||
|
content: z.array(accountPageContentItemRefs),
|
||||||
|
system: z.object({
|
||||||
|
content_type_uid: z.string(),
|
||||||
|
uid: z.string(),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
),
|
||||||
|
}),
|
||||||
|
})
|
||||||
|
|
||||||
|
export type AccountPageRefsDataRaw = z.infer<
|
||||||
|
typeof validateAccountPageRefsSchema
|
||||||
|
>
|
||||||
|
|||||||
@@ -1,10 +1,26 @@
|
|||||||
import GetAccountPage from "@/lib/graphql/Query/AccountPage.graphql"
|
import {
|
||||||
|
GetAccountPage,
|
||||||
|
GetAccountPageRefs,
|
||||||
|
} from "@/lib/graphql/Query/AccountPage.graphql"
|
||||||
import { request } from "@/lib/graphql/request"
|
import { request } from "@/lib/graphql/request"
|
||||||
import { badRequestError, internalServerError } from "@/server/errors/trpc"
|
import { badRequestError, internalServerError } from "@/server/errors/trpc"
|
||||||
import { publicProcedure, router } from "@/server/trpc"
|
import { publicProcedure, router } from "@/server/trpc"
|
||||||
|
|
||||||
|
import { removeEmptyObjects } from "@/utils/contentType"
|
||||||
|
import {
|
||||||
|
generateRefsResponseTag,
|
||||||
|
generateTag,
|
||||||
|
generateTags,
|
||||||
|
} from "@/utils/generateTag"
|
||||||
|
|
||||||
import { getAccountPageInput } from "./input"
|
import { getAccountPageInput } from "./input"
|
||||||
import { type AccountPage, validateAccountPageSchema } from "./output"
|
import {
|
||||||
|
type AccountPage,
|
||||||
|
AccountPageRefsDataRaw,
|
||||||
|
validateAccountPageRefsSchema,
|
||||||
|
validateAccountPageSchema,
|
||||||
|
} from "./output"
|
||||||
|
import { getConnections } from "./utils"
|
||||||
|
|
||||||
import { ContentEntries } from "@/types/components/myPages/myPage/enums"
|
import { ContentEntries } from "@/types/components/myPages/myPage/enums"
|
||||||
import { Embeds } from "@/types/requests/embeds"
|
import { Embeds } from "@/types/requests/embeds"
|
||||||
@@ -14,10 +30,55 @@ import { RTEDocument } from "@/types/rte/node"
|
|||||||
export const accountPageQueryRouter = router({
|
export const accountPageQueryRouter = router({
|
||||||
get: publicProcedure.input(getAccountPageInput).query(async ({ input }) => {
|
get: publicProcedure.input(getAccountPageInput).query(async ({ input }) => {
|
||||||
try {
|
try {
|
||||||
const response = await request<AccountPage>(GetAccountPage, {
|
const { lang, url } = input
|
||||||
locale: input.lang,
|
|
||||||
url: input.url,
|
const refsResponse = await request<AccountPageRefsDataRaw>(
|
||||||
})
|
GetAccountPageRefs,
|
||||||
|
{
|
||||||
|
locale: lang,
|
||||||
|
url,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
next: {
|
||||||
|
tags: [generateRefsResponseTag(lang, "account_page")],
|
||||||
|
},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
if (!refsResponse.data) {
|
||||||
|
console.error("Bad response for `GetAccountPageRefs`")
|
||||||
|
console.error({ refsResponse })
|
||||||
|
throw internalServerError()
|
||||||
|
}
|
||||||
|
|
||||||
|
const cleanedData = removeEmptyObjects(refsResponse.data)
|
||||||
|
|
||||||
|
const validatedAccountPageRefs =
|
||||||
|
validateAccountPageRefsSchema.safeParse(cleanedData)
|
||||||
|
if (!validatedAccountPageRefs.success) {
|
||||||
|
console.error("Bad validation for `GetAccountPageRefs`")
|
||||||
|
console.error(validatedAccountPageRefs.error)
|
||||||
|
throw badRequestError()
|
||||||
|
}
|
||||||
|
|
||||||
|
const connections = getConnections(validatedAccountPageRefs.data)
|
||||||
|
|
||||||
|
const tags = generateTags(lang, connections)
|
||||||
|
|
||||||
|
tags.push(
|
||||||
|
generateTag(
|
||||||
|
lang,
|
||||||
|
validatedAccountPageRefs.data.all_account_page.items[0].system.uid
|
||||||
|
)
|
||||||
|
)
|
||||||
|
const response = await request<AccountPage>(
|
||||||
|
GetAccountPage,
|
||||||
|
{
|
||||||
|
locale: lang,
|
||||||
|
url,
|
||||||
|
},
|
||||||
|
{ next: { tags } }
|
||||||
|
)
|
||||||
|
|
||||||
if (!response.data) {
|
if (!response.data) {
|
||||||
throw badRequestError()
|
throw badRequestError()
|
||||||
|
|||||||
33
server/routers/contentstack/accountPage/utils.ts
Normal file
33
server/routers/contentstack/accountPage/utils.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
import { AccountPageRefsDataRaw } from "./output"
|
||||||
|
|
||||||
|
import { ContentEntries } from "@/types/components/myPages/myPage/enums"
|
||||||
|
import type { Edges } from "@/types/requests/utils/edges"
|
||||||
|
import type { NodeRefs } from "@/types/requests/utils/refs"
|
||||||
|
|
||||||
|
export function getConnections(refs: AccountPageRefsDataRaw) {
|
||||||
|
const connections: Edges<NodeRefs>[] = []
|
||||||
|
refs.all_account_page.items.forEach((ref) => {
|
||||||
|
if (ref.content) {
|
||||||
|
ref.content.forEach((item) => {
|
||||||
|
switch (item.__typename) {
|
||||||
|
case ContentEntries.AccountPageContentShortcuts: {
|
||||||
|
item.shortcuts.shortcuts.forEach((shortcut) => {
|
||||||
|
if (shortcut.linkConnection.edges.length) {
|
||||||
|
connections.push(shortcut.linkConnection)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
break
|
||||||
|
}
|
||||||
|
case ContentEntries.AccountPageContentDynamicContent: {
|
||||||
|
if (item.dynamic_content.link.linkConnection.edges.length) {
|
||||||
|
connections.push(item.dynamic_content.link.linkConnection)
|
||||||
|
}
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return connections
|
||||||
|
}
|
||||||
@@ -51,7 +51,7 @@ export const loyaltyPageQueryRouter = router({
|
|||||||
)
|
)
|
||||||
|
|
||||||
if (!refsResponse.data) {
|
if (!refsResponse.data) {
|
||||||
console.error("Bad response for `GetNavigationMyPagesRefs`")
|
console.error("Bad response for `GetLoyaltyPageRefs`")
|
||||||
console.error({ refsResponse })
|
console.error({ refsResponse })
|
||||||
throw internalServerError()
|
throw internalServerError()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user