From 3f12246e122ebbcd7fb75de8abf67ca262217b30 Mon Sep 17 00:00:00 2001 From: Erik Tiekstra Date: Thu, 3 Oct 2024 11:29:26 +0200 Subject: [PATCH] feat(SW-497): Changed to siteConfig which includes Global Alert --- ...nfiguration.graphql => SiteConfig.graphql} | 4 +- server/routers/contentstack/base/output.ts | 16 +- server/routers/contentstack/base/query.ts | 216 ++++++++++-------- server/routers/contentstack/base/telemetry.ts | 14 +- server/routers/contentstack/base/utils.ts | 30 ++- .../{siteConfiguration.ts => siteConfig.ts} | 6 +- 6 files changed, 166 insertions(+), 120 deletions(-) rename lib/graphql/Query/{SiteConfiguration.graphql => SiteConfig.graphql} (71%) rename types/trpc/routers/contentstack/{siteConfiguration.ts => siteConfig.ts} (63%) diff --git a/lib/graphql/Query/SiteConfiguration.graphql b/lib/graphql/Query/SiteConfig.graphql similarity index 71% rename from lib/graphql/Query/SiteConfiguration.graphql rename to lib/graphql/Query/SiteConfig.graphql index f4d8a09af..52bac0fa4 100644 --- a/lib/graphql/Query/SiteConfiguration.graphql +++ b/lib/graphql/Query/SiteConfig.graphql @@ -1,7 +1,7 @@ #import "../Fragments/GlobalAlert.graphql" -query GetSiteConfiguration($locale: String!) { - all_site_configuration(limit: 1, locale: $locale) { +query GetSiteConfig($locale: String!) { + all_site_config(limit: 1, locale: $locale) { items { sitewide_alert { booking_widget_disabled diff --git a/server/routers/contentstack/base/output.ts b/server/routers/contentstack/base/output.ts index 9379fb370..55a94f790 100644 --- a/server/routers/contentstack/base/output.ts +++ b/server/routers/contentstack/base/output.ts @@ -15,7 +15,7 @@ import { removeMultipleSlashes } from "@/utils/url" import { systemSchema } from "../schemas/system" import { Image } from "@/types/image" -import { GlobalAlertType } from "@/types/trpc/routers/contentstack/siteConfiguration" +import { GlobalAlertType } from "@/types/trpc/routers/contentstack/siteConfig" // Help me write this zod schema based on the type ContactConfig export const validateContactConfigSchema = z.object({ @@ -669,15 +669,15 @@ export const globalAlertSchema = z.object({ text: z.string(), heading: z.string(), phone_contact: z.object({ - display_text: z.string(), - phone_number: z.string(), - footnote: z.string(), + display_text: z.string().nullable(), + phone_number: z.string().nullable(), + footnote: z.string().nullable(), }), }) -export const siteConfigurationSchema = z +export const siteConfigSchema = z .object({ - all_site_configuration: z.object({ + all_site_config: z.object({ items: z .array( z.object({ @@ -699,14 +699,14 @@ export const siteConfigurationSchema = z }), }) .transform((data) => { - if (!data.all_site_configuration.items.length) { + if (!data.all_site_config.items.length) { return { globalAlert: null, bookingWidgetDisabled: false, } } - const { sitewide_alert } = data.all_site_configuration.items[0] + const { sitewide_alert } = data.all_site_config.items[0] return { globalAlert: sitewide_alert.alertConnection.edges[0]?.node || null, diff --git a/server/routers/contentstack/base/query.ts b/server/routers/contentstack/base/query.ts index f596d711a..98597a825 100644 --- a/server/routers/contentstack/base/query.ts +++ b/server/routers/contentstack/base/query.ts @@ -1,3 +1,4 @@ +import { Lang } from "@/constants/languages" import { GetContactConfig } from "@/lib/graphql/Query/ContactConfig.graphql" import { GetCurrentFooter, @@ -9,7 +10,7 @@ import { } from "@/lib/graphql/Query/Current/Header.graphql" import { GetFooter, GetFooterRef } from "@/lib/graphql/Query/Footer.graphql" import { GetHeader, GetHeaderRef } from "@/lib/graphql/Query/Header.graphql" -import { GetSiteConfiguration } from "@/lib/graphql/Query/SiteConfiguration.graphql" +import { GetSiteConfig } from "@/lib/graphql/Query/SiteConfig.graphql" import { request } from "@/lib/graphql/request" import { notFound } from "@/server/errors/trpc" import { contentstackBaseProcedure, router } from "@/server/trpc" @@ -30,7 +31,7 @@ import { type GetCurrentHeaderData, headerRefsSchema, headerSchema, - siteConfigurationSchema, + siteConfigSchema, validateContactConfigSchema, validateCurrentFooterConfigSchema, validateCurrentHeaderConfigSchema, @@ -61,11 +62,15 @@ import { getHeaderRefsFailCounter, getHeaderRefsSuccessCounter, getHeaderSuccessCounter, - getSiteConfigurationCounter, - getSiteConfigurationFailCounter, - getSiteConfigurationSuccessCounter, + getSiteConfigCounter, + getSiteConfigFailCounter, + getSiteConfigSuccessCounter, } from "./telemetry" -import { getConnections, getFooterConnections } from "./utils" +import { + getConnections, + getFooterConnections, + getGlobalAlertPhoneContactData, +} from "./utils" import type { FooterDataRaw, @@ -75,71 +80,74 @@ import type { GetHeader as GetHeaderData, GetHeaderRefs, } from "@/types/trpc/routers/contentstack/header" -import type { GetSiteConfigurationData } from "@/types/trpc/routers/contentstack/siteConfiguration" +import type { GetSiteConfigData } from "@/types/trpc/routers/contentstack/siteConfig" + +async function getContactConfig(lang: Lang) { + getContactConfigCounter.add(1, { lang }) + console.info( + "contentstack.contactConfig start", + JSON.stringify({ query: { lang } }) + ) + const response = await request( + GetContactConfig, + { + locale: lang, + }, + { + cache: "force-cache", + next: { + tags: [`${lang}:contact`], + }, + } + ) + + if (!response.data) { + const notFoundError = notFound(response) + + getContactConfigFailCounter.add(1, { + lang, + error_type: "not_found", + error: JSON.stringify({ code: notFoundError.code }), + }) + + console.error( + "contentstack.config not found error", + JSON.stringify({ query: { lang }, error: { code: notFoundError.code } }) + ) + + throw notFoundError + } + + const validatedContactConfigConfig = validateContactConfigSchema.safeParse( + response.data + ) + + if (!validatedContactConfigConfig.success) { + getContactConfigFailCounter.add(1, { + lang, + error_type: "validation_error", + error: JSON.stringify(validatedContactConfigConfig.error), + }) + console.error( + "contentstack.contactConfig validation error", + JSON.stringify({ + query: { lang }, + error: validatedContactConfigConfig.error, + }) + ) + return null + } + getContactConfigSuccessCounter.add(1, { lang }) + console.info( + "contentstack.contactConfig success", + JSON.stringify({ query: { lang } }) + ) + return validatedContactConfigConfig.data.all_contact_config.items[0] +} export const baseQueryRouter = router({ contact: contentstackBaseProcedure.query(async ({ ctx }) => { - const { lang } = ctx - getContactConfigCounter.add(1, { lang }) - console.info( - "contentstack.contactConfig start", - JSON.stringify({ query: { lang } }) - ) - const response = await request( - GetContactConfig, - { - locale: lang, - }, - { - cache: "force-cache", - next: { - tags: [`${lang}:contact`], - }, - } - ) - - if (!response.data) { - const notFoundError = notFound(response) - - getContactConfigFailCounter.add(1, { - lang, - error_type: "not_found", - error: JSON.stringify({ code: notFoundError.code }), - }) - - console.error( - "contentstack.config not found error", - JSON.stringify({ query: { lang }, error: { code: notFoundError.code } }) - ) - - throw notFoundError - } - - const validatedContactConfigConfig = validateContactConfigSchema.safeParse( - response.data - ) - - if (!validatedContactConfigConfig.success) { - getContactConfigFailCounter.add(1, { - lang, - error_type: "validation_error", - error: JSON.stringify(validatedContactConfigConfig.error), - }) - console.error( - "contentstack.contactConfig validation error", - JSON.stringify({ - query: { lang }, - error: validatedContactConfigConfig.error, - }) - ) - return null - } - getContactConfigSuccessCounter.add(1, { lang }) - console.info( - "contentstack.contactConfig success", - JSON.stringify({ query: { lang } }) - ) - return validatedContactConfigConfig.data.all_contact_config.items[0] + return await getContactConfig(ctx.lang) }), header: contentstackBaseProcedure.query(async ({ ctx }) => { const { lang } = ctx @@ -594,67 +602,83 @@ export const baseQueryRouter = router({ return validatedFooterConfig.data }), - siteConfiguration: contentstackBaseProcedure.query(async ({ ctx }) => { + siteConfig: contentstackBaseProcedure.query(async ({ ctx }) => { const { lang } = ctx - getSiteConfigurationCounter.add(1, { lang }) + getSiteConfigCounter.add(1, { lang }) console.info( - "contentstack.siteConfiguration start", + "contentstack.siteConfig start", JSON.stringify({ query: { lang } }) ) - const response = await request( - GetSiteConfiguration, - { - locale: lang, - }, - { - cache: "force-cache", - next: { - tags: [`${lang}:siteConfiguration`], + const [siteConfigResponse, contactConfig] = await Promise.all([ + request( + GetSiteConfig, + { + locale: lang, }, - } - ) + { + cache: "force-cache", + next: { + tags: [`${lang}:siteConfig`], + }, + } + ), + getContactConfig(lang), + ]) - if (!response.data) { - const notFoundError = notFound(response) + if (!siteConfigResponse.data) { + const notFoundError = notFound(siteConfigResponse) - getSiteConfigurationFailCounter.add(1, { + getSiteConfigFailCounter.add(1, { lang, error_type: "not_found", error: JSON.stringify({ code: notFoundError.code }), }) console.error( - "contentstack.siteConfiguration not found error", + "contentstack.siteConfig not found error", JSON.stringify({ query: { lang }, error: { code: notFoundError.code } }) ) throw notFoundError } - const validatedSiteConfiguration = siteConfigurationSchema.safeParse( - response.data + const validatedSiteConfig = siteConfigSchema.safeParse( + siteConfigResponse.data ) - if (!validatedSiteConfiguration.success) { - getSiteConfigurationFailCounter.add(1, { + if (!validatedSiteConfig.success) { + getSiteConfigFailCounter.add(1, { lang, error_type: "validation_error", - error: JSON.stringify(validatedSiteConfiguration.error), + error: JSON.stringify(validatedSiteConfig.error), }) console.error( - "contentstack.siteConfiguration validation error", + "contentstack.siteConfig validation error", JSON.stringify({ query: { lang }, - error: validatedSiteConfiguration.error, + error: validatedSiteConfig.error, }) ) return null } - getSiteConfigurationSuccessCounter.add(1, { lang }) + getSiteConfigSuccessCounter.add(1, { lang }) console.info( - "contentstack.siteConfiguration success", + "contentstack.siteConfig success", JSON.stringify({ query: { lang } }) ) - return validatedSiteConfiguration.data + + const { globalAlert } = validatedSiteConfig.data + + return { + ...validatedSiteConfig.data, + globalAlert: globalAlert + ? { + ...globalAlert, + phone_contact: contactConfig + ? getGlobalAlertPhoneContactData(globalAlert, contactConfig) + : null, + } + : null, + } }), }) diff --git a/server/routers/contentstack/base/telemetry.ts b/server/routers/contentstack/base/telemetry.ts index db7139ed4..78000689d 100644 --- a/server/routers/contentstack/base/telemetry.ts +++ b/server/routers/contentstack/base/telemetry.ts @@ -91,13 +91,13 @@ export const getFooterFailCounter = meter.createCounter( "trpc.contentstack.footer.get-fail" ) -// OpenTelemetry metrics: SiteConfiguration -export const getSiteConfigurationCounter = meter.createCounter( - "trpc.contentstack.SiteConfiguration.get" +// OpenTelemetry metrics: SiteConfig +export const getSiteConfigCounter = meter.createCounter( + "trpc.contentstack.SiteConfig.get" ) -export const getSiteConfigurationSuccessCounter = meter.createCounter( - "trpc.contentstack.SiteConfiguration.get-success" +export const getSiteConfigSuccessCounter = meter.createCounter( + "trpc.contentstack.SiteConfig.get-success" ) -export const getSiteConfigurationFailCounter = meter.createCounter( - "trpc.contentstack.SiteConfiguration.get-fail" +export const getSiteConfigFailCounter = meter.createCounter( + "trpc.contentstack.SiteConfig.get-fail" ) diff --git a/server/routers/contentstack/base/utils.ts b/server/routers/contentstack/base/utils.ts index cd61dea0c..1bab9b170 100644 --- a/server/routers/contentstack/base/utils.ts +++ b/server/routers/contentstack/base/utils.ts @@ -1,11 +1,13 @@ -import type { - FooterLinkItem, - FooterRefDataRaw, -} from "@/types/components/footer/footer" +import { getValueFromContactConfig } from "@/utils/contactConfig" + +import { ContactConfig } from "./output" + +import type { FooterRefDataRaw } from "@/types/components/footer/footer" import { System } from "@/types/requests/system" import { Edges } from "@/types/requests/utils/edges" import { NodeRefs } from "@/types/requests/utils/refs" import type { HeaderRefs } from "@/types/trpc/routers/contentstack/header" +import { SiteConfig } from "@/types/trpc/routers/contentstack/siteConfig" export function getConnections({ header }: HeaderRefs) { const connections: System["system"][] = [header.system] @@ -68,3 +70,23 @@ export function getFooterConnections(refs: FooterRefDataRaw) { return connections } + +export function getGlobalAlertPhoneContactData( + globalAlert: SiteConfig["globalAlert"], + contactConfig: ContactConfig +) { + if (globalAlert?.phone_contact) { + const { display_text, phone_number, footnote } = globalAlert.phone_contact + + return { + display_text, + phone: phone_number + ? getValueFromContactConfig(phone_number, contactConfig) + : null, + footnote: footnote + ? getValueFromContactConfig(footnote, contactConfig) + : null, + } + } + return null +} diff --git a/types/trpc/routers/contentstack/siteConfiguration.ts b/types/trpc/routers/contentstack/siteConfig.ts similarity index 63% rename from types/trpc/routers/contentstack/siteConfiguration.ts rename to types/trpc/routers/contentstack/siteConfig.ts index dcc86c4ec..f51470770 100644 --- a/types/trpc/routers/contentstack/siteConfiguration.ts +++ b/types/trpc/routers/contentstack/siteConfig.ts @@ -2,7 +2,7 @@ import { z } from "zod" import { globalAlertSchema, - siteConfigurationSchema, + siteConfigSchema, } from "@/server/routers/contentstack/base/output" export enum GlobalAlertType { @@ -10,8 +10,8 @@ export enum GlobalAlertType { Alarm = "alarm", } -export type GetSiteConfigurationData = z.input -export type SiteConfiguration = z.output +export type GetSiteConfigData = z.input +export type SiteConfig = z.output export type GetGlobalAlertData = z.input export type GlobalAlert = z.output