From 863d99ad4414aca4dfff4b88746ffcc4b1672124 Mon Sep 17 00:00:00 2001 From: Christel Westerberg Date: Wed, 15 May 2024 11:06:52 +0200 Subject: [PATCH] fix: refs for account page --- lib/graphql/Query/AccountPage.graphql | 50 +++++++++++++ .../contentstack/accountPage/output.ts | 59 +++++++++++++++ .../routers/contentstack/accountPage/query.ts | 73 +++++++++++++++++-- .../routers/contentstack/accountPage/utils.ts | 33 +++++++++ .../routers/contentstack/loyaltyPage/query.ts | 2 +- 5 files changed, 210 insertions(+), 7 deletions(-) create mode 100644 server/routers/contentstack/accountPage/utils.ts diff --git a/lib/graphql/Query/AccountPage.graphql b/lib/graphql/Query/AccountPage.graphql index 4119c943b..2bd12f4fa 100644 --- a/lib/graphql/Query/AccountPage.graphql +++ b/lib/graphql/Query/AccountPage.graphql @@ -2,6 +2,11 @@ #import "../Fragments/MyPages/AccountPage/AccountPageContentShortcuts.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!) { all_account_page(limit: 1, locale: $locale, where: { url: $url }) { items { @@ -17,3 +22,48 @@ query GetAccountPage($locale: String!, $url: String!) { 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 + } + } + } +} diff --git a/server/routers/contentstack/accountPage/output.ts b/server/routers/contentstack/accountPage/output.ts index a73757227..080e687e7 100644 --- a/server/routers/contentstack/accountPage/output.ts +++ b/server/routers/contentstack/accountPage/output.ts @@ -7,6 +7,7 @@ import { DynamicContentComponents, } from "@/types/components/myPages/myPage/enums" import { Embeds } from "@/types/requests/embeds" +import { PageLinkEnum } from "@/types/requests/pageLinks" import { Edges } from "@/types/requests/utils/edges" import { RTEDocument } from "@/types/rte/node" @@ -139,3 +140,61 @@ export type AccountPage = Omit & { title: string 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 +> diff --git a/server/routers/contentstack/accountPage/query.ts b/server/routers/contentstack/accountPage/query.ts index 56a0ac2d3..5102e836c 100644 --- a/server/routers/contentstack/accountPage/query.ts +++ b/server/routers/contentstack/accountPage/query.ts @@ -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 { 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 { 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 { Embeds } from "@/types/requests/embeds" @@ -14,10 +30,55 @@ import { RTEDocument } from "@/types/rte/node" export const accountPageQueryRouter = router({ get: publicProcedure.input(getAccountPageInput).query(async ({ input }) => { try { - const response = await request(GetAccountPage, { - locale: input.lang, - url: input.url, - }) + const { lang, url } = input + + const refsResponse = await request( + 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( + GetAccountPage, + { + locale: lang, + url, + }, + { next: { tags } } + ) if (!response.data) { throw badRequestError() diff --git a/server/routers/contentstack/accountPage/utils.ts b/server/routers/contentstack/accountPage/utils.ts new file mode 100644 index 000000000..8a7e62a6d --- /dev/null +++ b/server/routers/contentstack/accountPage/utils.ts @@ -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[] = [] + 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 +} diff --git a/server/routers/contentstack/loyaltyPage/query.ts b/server/routers/contentstack/loyaltyPage/query.ts index dc934783d..0466b46ca 100644 --- a/server/routers/contentstack/loyaltyPage/query.ts +++ b/server/routers/contentstack/loyaltyPage/query.ts @@ -51,7 +51,7 @@ export const loyaltyPageQueryRouter = router({ ) if (!refsResponse.data) { - console.error("Bad response for `GetNavigationMyPagesRefs`") + console.error("Bad response for `GetLoyaltyPageRefs`") console.error({ refsResponse }) throw internalServerError() }