From 183e1cd6d26f04a0b52b4899f9014063e4b411b3 Mon Sep 17 00:00:00 2001 From: Christel Westerberg Date: Mon, 15 Jul 2024 13:35:00 +0200 Subject: [PATCH] fix: refactor tracking requests --- .../(protected)/my-pages/[...path]/page.tsx | 13 +++---- .../(protected)/my-pages/profile/page.tsx | 14 ++++---- app/[lang]/(live)/layout.tsx | 3 ++ .../ContentType/LoyaltyPage/LoyaltyPage.tsx | 13 +++---- .../ContentType/Webviews/AccountPage.tsx | 12 +++---- .../ContentType/Webviews/LoyaltyPage.tsx | 13 +++---- components/TrackingSDK/index.tsx | 19 ++++++----- lib/graphql/Query/AccountPage.graphql | 18 ++++------ lib/graphql/Query/LoyaltyPage.graphql | 12 +------ .../contentstack/accountPage/output.ts | 21 ++++-------- .../routers/contentstack/accountPage/query.ts | 34 +++++-------------- .../contentstack/loyaltyPage/output.ts | 21 ++++-------- .../routers/contentstack/loyaltyPage/query.ts | 26 +++----------- server/routers/user/query.ts | 18 +++------- 14 files changed, 74 insertions(+), 163 deletions(-) diff --git a/app/[lang]/(live)/(protected)/my-pages/[...path]/page.tsx b/app/[lang]/(live)/(protected)/my-pages/[...path]/page.tsx index 5019a3cb6..3f140f10e 100644 --- a/app/[lang]/(live)/(protected)/my-pages/[...path]/page.tsx +++ b/app/[lang]/(live)/(protected)/my-pages/[...path]/page.tsx @@ -12,16 +12,14 @@ import type { LangParams, PageArgs } from "@/types/params" export default async function MyPages({ params, }: PageArgs) { - const accountPage = await serverClient().contentstack.accountPage.get() + const accountPageRes = await serverClient().contentstack.accountPage.get() const { formatMessage } = await getIntl() - if (!accountPage) { + if (!accountPageRes) { return null } - const accountPageTracking = serverClient().contentstack.accountPage.tracking() - const userTrackingData = serverClient().user.tracking() - + const { tracking, accountPage } = accountPageRes return (
{accountPage.heading} @@ -30,10 +28,7 @@ export default async function MyPages({ ) : (

{formatMessage({ id: "No content published" })}

)} - +
) } diff --git a/app/[lang]/(live)/(protected)/my-pages/profile/page.tsx b/app/[lang]/(live)/(protected)/my-pages/profile/page.tsx index 7466836ca..7755289e5 100644 --- a/app/[lang]/(live)/(protected)/my-pages/profile/page.tsx +++ b/app/[lang]/(live)/(protected)/my-pages/profile/page.tsx @@ -5,13 +5,11 @@ import { serverClient } from "@/lib/trpc/server" import TrackingSDK from "@/components/TrackingSDK" export default async function ProfilePage() { - const accountPageTracking = serverClient().contentstack.accountPage.tracking() - const userTrackingData = serverClient().user.tracking() + const accountPage = await serverClient().contentstack.accountPage.get() - return ( - - ) + if (!accountPage) { + return null + } + + return } diff --git a/app/[lang]/(live)/layout.tsx b/app/[lang]/(live)/layout.tsx index 2cb8bdb64..db301e06b 100644 --- a/app/[lang]/(live)/layout.tsx +++ b/app/[lang]/(live)/layout.tsx @@ -8,6 +8,7 @@ import TrpcProvider from "@/lib/trpc/Provider" import AdobeSDKScript from "@/components/Current/AdobeSDKScript" import Footer from "@/components/Current/Footer" import VwoScript from "@/components/Current/VwoScript" +import { preloadUserTracking } from "@/components/TrackingSDK" import { getIntl } from "@/i18n" import ServerIntlProvider from "@/i18n/Provider" @@ -29,6 +30,8 @@ export default async function RootLayout({ header: React.ReactNode } >) { + preloadUserTracking() + const { defaultLocale, locale, messages } = await getIntl() return ( diff --git a/components/ContentType/LoyaltyPage/LoyaltyPage.tsx b/components/ContentType/LoyaltyPage/LoyaltyPage.tsx index dd6d6bf07..c4e0eb737 100644 --- a/components/ContentType/LoyaltyPage/LoyaltyPage.tsx +++ b/components/ContentType/LoyaltyPage/LoyaltyPage.tsx @@ -11,14 +11,14 @@ import styles from "./loyaltyPage.module.css" import type { LangParams } from "@/types/params" export default async function LoyaltyPage({ lang }: LangParams) { - const loyaltyPage = await serverClient().contentstack.loyaltyPage.get() + const loyaltyPageRes = await serverClient().contentstack.loyaltyPage.get() - if (!loyaltyPage) { + if (!loyaltyPageRes) { return null } - const loyaltyPageTracking = serverClient().contentstack.loyaltyPage.tracking() - const userTracking = serverClient().user.tracking() + const { tracking, loyaltyPage } = loyaltyPageRes + return (
{loyaltyPage.sidebar.length ? ( @@ -31,10 +31,7 @@ export default async function LoyaltyPage({ lang }: LangParams) { ) : null} - +
) } diff --git a/components/ContentType/Webviews/AccountPage.tsx b/components/ContentType/Webviews/AccountPage.tsx index c722dd586..5f3fc862b 100644 --- a/components/ContentType/Webviews/AccountPage.tsx +++ b/components/ContentType/Webviews/AccountPage.tsx @@ -14,14 +14,13 @@ import styles from "./accountPage.module.css" import { LangParams } from "@/types/params" export default async function MyPages({ lang }: LangParams) { - const accountPage = await serverClient().contentstack.accountPage.get() + const accountPageRes = await serverClient().contentstack.accountPage.get() - if (!accountPage) { + if (!accountPageRes) { return null } - const accountPageTracking = serverClient().contentstack.accountPage.tracking() - const userTrackingData = serverClient().user.tracking() + const { tracking, accountPage } = accountPageRes const linkToOverview = `/${lang}/webview${accountPage.url}` !== overview[lang] @@ -29,10 +28,7 @@ export default async function MyPages({ lang }: LangParams) { {linkToOverview ? : null} - + ) } diff --git a/components/ContentType/Webviews/LoyaltyPage.tsx b/components/ContentType/Webviews/LoyaltyPage.tsx index ba433d3e5..d2e69dd86 100644 --- a/components/ContentType/Webviews/LoyaltyPage.tsx +++ b/components/ContentType/Webviews/LoyaltyPage.tsx @@ -11,15 +11,13 @@ import styles from "./loyaltyPage.module.css" import { LangParams } from "@/types/params" export default async function AboutScandicFriends({ lang }: LangParams) { - const loyaltyPage = await serverClient().contentstack.loyaltyPage.get() + const loyaltyPageRes = await serverClient().contentstack.loyaltyPage.get() - if (!loyaltyPage) { + if (!loyaltyPageRes) { return null } - const loyaltyPageTracking = serverClient().contentstack.loyaltyPage.tracking() - const userTrackingData = serverClient().user.tracking() - + const { tracking, loyaltyPage } = loyaltyPageRes return (
@@ -27,10 +25,7 @@ export default async function AboutScandicFriends({ lang }: LangParams) { {loyaltyPage.heading} - +
) } diff --git a/components/TrackingSDK/index.tsx b/components/TrackingSDK/index.tsx index 6c182150b..761169953 100644 --- a/components/TrackingSDK/index.tsx +++ b/components/TrackingSDK/index.tsx @@ -1,16 +1,19 @@ +import { serverClient } from "@/lib/trpc/server" + import TrackingSDKClient from "./Client" -import { TrackingSDKProps } from "@/types/components/tracking" +import { TrackingSDKPageData } from "@/types/components/tracking" + +export const preloadUserTracking = () => { + void serverClient().user.tracking() +} export default async function TrackingSDK({ - pageDataPromise, - userDataPromise, + pageData, }: { - pageDataPromise: Promise - userDataPromise: Promise + pageData: TrackingSDKPageData }) { - const pageData = await pageDataPromise - const userData = await userDataPromise + const userTrackingData = await serverClient().user.tracking() - return + return } diff --git a/lib/graphql/Query/AccountPage.graphql b/lib/graphql/Query/AccountPage.graphql index 54ec7aa9b..b21150bee 100644 --- a/lib/graphql/Query/AccountPage.graphql +++ b/lib/graphql/Query/AccountPage.graphql @@ -18,6 +18,12 @@ query GetAccountPage($locale: String!, $uid: String!) { ...AccountPageContentShortcuts ...AccountPageContentTextContent } + system { + uid + created_at + updated_at + locale + } } } @@ -99,15 +105,3 @@ query GetFiNoSvUrlsAccountPage($uid: String!) { } } } - -query GetTrackingAccountPage($locale: String!, $uid: String!) { - account_page(locale: $locale, uid: $uid) { - system { - locale - created_at - uid - updated_at - } - title - } -} diff --git a/lib/graphql/Query/LoyaltyPage.graphql b/lib/graphql/Query/LoyaltyPage.graphql index db83c1390..b0cd9cb29 100644 --- a/lib/graphql/Query/LoyaltyPage.graphql +++ b/lib/graphql/Query/LoyaltyPage.graphql @@ -172,6 +172,7 @@ query GetLoyaltyPage($locale: String!, $uid: String!) { uid created_at updated_at + locale } } } @@ -362,14 +363,3 @@ query GetFiNoSvUrlsLoyaltyPage($uid: String!) { } } } - -query GetTrackingLoyaltyPage($locale: String!, $uid: String!) { - loyalty_page(locale: $locale, uid: $uid) { - system { - locale - created_at - uid - updated_at - } - } -} diff --git a/server/routers/contentstack/accountPage/output.ts b/server/routers/contentstack/accountPage/output.ts index 8dd73d65c..7d99bdbba 100644 --- a/server/routers/contentstack/accountPage/output.ts +++ b/server/routers/contentstack/accountPage/output.ts @@ -125,6 +125,12 @@ export const validateAccountPageSchema = z.object({ url: z.string(), title: z.string(), content: z.array(accountPageContentItem), + system: z.object({ + uid: z.string(), + locale: z.nativeEnum(Lang), + created_at: z.string(), + updated_at: z.string(), + }), }), }) @@ -189,18 +195,3 @@ export const validateAccountPageRefsSchema = z.object({ export type AccountPageRefsDataRaw = z.infer< typeof validateAccountPageRefsSchema > - -//Tracking -const validateAccountTrackingData = z.object({ - account_page: z.object({ - system: z.object({ - uid: z.string(), - locale: z.string(), - updated_at: z.string(), - created_at: z.string(), - }), - title: z.string().nullable(), - }), -}) - -export type AccountPageTackingData = z.infer diff --git a/server/routers/contentstack/accountPage/query.ts b/server/routers/contentstack/accountPage/query.ts index b2ae4bee0..53becd9ed 100644 --- a/server/routers/contentstack/accountPage/query.ts +++ b/server/routers/contentstack/accountPage/query.ts @@ -2,7 +2,6 @@ import { Lang } from "@/constants/languages" import { GetAccountPage, GetAccountPageRefs, - GetTrackingAccountPage, } from "@/lib/graphql/Query/AccountPage.graphql" import { request } from "@/lib/graphql/request" import { notFound } from "@/server/errors/trpc" @@ -19,7 +18,6 @@ import { type AccountPage, AccountPageDataRaw, AccountPageRefsDataRaw, - AccountPageTackingData, validateAccountPageRefsSchema, validateAccountPageSchema, } from "./output" @@ -136,32 +134,18 @@ export const accountPageQueryRouter = router({ content, } as AccountPage - return accountPage - }), - tracking: contentstackExtendedProcedureUID.query(async ({ ctx }) => { - const { lang, uid } = ctx - - const response = await request( - GetTrackingAccountPage, - { - locale: lang, - uid, - } - ) - - if (!response.data) { - throw notFound(response) - } - - const accountTrackingData: TrackingSDKPageData = { - pageId: response.data.account_page.system.uid, - lang: response.data.account_page.system.locale as Lang, - publishedDate: response.data.account_page.system.updated_at, - createdDate: response.data.account_page.system.created_at, + const tracking: TrackingSDKPageData = { + pageId: validatedAccountPage.data.account_page.system.uid, + lang: validatedAccountPage.data.account_page.system.locale as Lang, + publishedDate: validatedAccountPage.data.account_page.system.updated_at, + createdDate: validatedAccountPage.data.account_page.system.created_at, channel: TrackingChannelEnum["scandic-friends"], pageType: `member${response.data.account_page.title}page`, } - return accountTrackingData + return { + accountPage, + tracking, + } }), }) diff --git a/server/routers/contentstack/loyaltyPage/output.ts b/server/routers/contentstack/loyaltyPage/output.ts index 043ec508d..205509a4c 100644 --- a/server/routers/contentstack/loyaltyPage/output.ts +++ b/server/routers/contentstack/loyaltyPage/output.ts @@ -193,7 +193,12 @@ export const validateLoyaltyPageSchema = z.object({ heading: z.string().nullable(), blocks: z.array(loyaltyPageBlockItem).nullable(), sidebar: z.array(loyaltyPageSidebarItem).nullable(), - system: z.object({ uid: z.string() }), + system: z.object({ + uid: z.string(), + locale: z.nativeEnum(Lang), + created_at: z.string(), + updated_at: z.string(), + }), }) // Block types @@ -417,17 +422,3 @@ export const validateLoyaltyPageRefsSchema = z.object({ export type LoyaltyPageRefsDataRaw = z.infer< typeof validateLoyaltyPageRefsSchema > - -//Tracking -const validateLoyaltyTrackingData = z.object({ - loyalty_page: z.object({ - system: z.object({ - uid: z.string(), - locale: z.string(), - updated_at: z.string(), - created_at: z.string(), - }), - }), -}) - -export type LoyaltyPageTackingData = z.infer diff --git a/server/routers/contentstack/loyaltyPage/query.ts b/server/routers/contentstack/loyaltyPage/query.ts index 047966c8a..ecdc08ac7 100644 --- a/server/routers/contentstack/loyaltyPage/query.ts +++ b/server/routers/contentstack/loyaltyPage/query.ts @@ -2,7 +2,6 @@ import { Lang } from "@/constants/languages" import { GetLoyaltyPage, GetLoyaltyPageRefs, - GetTrackingLoyaltyPage, } from "@/lib/graphql/Query/LoyaltyPage.graphql" import { request } from "@/lib/graphql/request" import { notFound } from "@/server/errors/trpc" @@ -20,7 +19,6 @@ import { removeEmptyObjects } from "../../utils" import { LoyaltyPage, type LoyaltyPageRefsDataRaw, - LoyaltyPageTackingData, validateLoyaltyPageRefsSchema, validateLoyaltyPageSchema, } from "./output" @@ -228,24 +226,6 @@ export const loyaltyPageQueryRouter = router({ return null } - // Assert LoyaltyPage type to get correct typings for RTE fields - return validatedLoyaltyPage.data as LoyaltyPage - }), - tracking: contentstackExtendedProcedureUID.query(async ({ ctx }) => { - const { lang, uid } = ctx - - const response = await request( - GetTrackingLoyaltyPage, - { - locale: lang, - uid, - } - ) - - if (!response.data) { - throw notFound(response) - } - const loyaltyTrackingData: TrackingSDKPageData = { pageId: response.data.loyalty_page.system.uid, lang: response.data.loyalty_page.system.locale as Lang, @@ -255,6 +235,10 @@ export const loyaltyPageQueryRouter = router({ pageType: "loyaltycontentpage", } - return loyaltyTrackingData + // Assert LoyaltyPage type to get correct typings for RTE fields + return { + loyaltyPage: validatedLoyaltyPage.data as LoyaltyPage, + tracking: loyaltyTrackingData, + } }), }) diff --git a/server/routers/user/query.ts b/server/routers/user/query.ts index 0fdcf5701..3a4c13aac 100644 --- a/server/routers/user/query.ts +++ b/server/routers/user/query.ts @@ -248,18 +248,9 @@ export const userQueryRouter = router({ ) if (!previousStaysResponse.ok) { - // switch (apiResponse.status) { - // case 400: - // throw badRequestError(apiResponse) - // case 401: - // throw unauthorizedError(apiResponse) - // case 403: - // throw forbiddenError(apiResponse) - // default: - // throw internalServerError(apiResponse) - // } - console.info(`API Response Failed - Getting Previous Stays`) - console.info(`User: (${JSON.stringify(ctx.session.user)})`) + console.info( + `API Response Failed - Getting Previous Stays for tracking user` + ) console.error(previousStaysResponse) return notLoggedInUserTrackingData } @@ -268,8 +259,7 @@ export const userQueryRouter = router({ const verifiedPreviousStaysData = getStaysSchema.safeParse(previousStaysApiJson) if (!verifiedPreviousStaysData.success) { - console.info(`Failed to validate Previous Stays Data`) - console.info(`User: (${JSON.stringify(ctx.session.user)})`) + console.info(`Failed to validate Previous Stays Data for tracking user`) console.error(verifiedPreviousStaysData.error) return notLoggedInUserTrackingData }