fix(BOOK-453): avoid linkConnection invalid * fix(BOOK-453): avoid linkConnection invalid * test * test * test * Merge master
141 lines
3.7 KiB
TypeScript
141 lines
3.7 KiB
TypeScript
import * as Sentry from "@sentry/nextjs"
|
|
import { z } from "zod"
|
|
|
|
import { logger } from "@scandic-hotels/common/logger"
|
|
|
|
import { getValueFromContactConfig } from "../../../utils/contactConfig"
|
|
|
|
import type { Edges } from "../../../types/edges"
|
|
import type { FooterRefDataRaw } from "../../../types/footer"
|
|
import type { HeaderRefs } from "../../../types/header"
|
|
import type { NodeRefs } from "../../../types/refs"
|
|
import type {
|
|
AlertOutput,
|
|
GetSiteConfigRefData,
|
|
} from "../../../types/siteConfig"
|
|
import type { System } from "../schemas/system"
|
|
import type { ContactConfig } from "./output"
|
|
|
|
export function getConnections({ header }: HeaderRefs) {
|
|
const connections: System["system"][] = [header.system]
|
|
|
|
if (header.top_link) {
|
|
if (header.top_link.logged_in?.link) {
|
|
connections.push(header.top_link.logged_in.link)
|
|
}
|
|
if (header.top_link.logged_out?.link) {
|
|
connections.push(header.top_link.logged_out.link)
|
|
}
|
|
}
|
|
|
|
if (header.menu_items.length) {
|
|
header.menu_items.forEach((menuItem) => {
|
|
if (menuItem.card) {
|
|
connections.push(...menuItem.card)
|
|
}
|
|
if (menuItem.link) {
|
|
connections.push(menuItem.link)
|
|
}
|
|
if (menuItem.see_all_link?.link) {
|
|
connections.push(menuItem.see_all_link.link)
|
|
}
|
|
if (menuItem.submenu.length) {
|
|
menuItem.submenu.forEach((subMenuItem) => {
|
|
if (subMenuItem.links.length) {
|
|
subMenuItem.links.forEach((link) => {
|
|
if (link?.link) {
|
|
connections.push(link.link)
|
|
}
|
|
})
|
|
}
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
return connections
|
|
}
|
|
|
|
export function getFooterConnections(refs: FooterRefDataRaw) {
|
|
const connections: Edges<NodeRefs>[] = []
|
|
const footerData = refs.all_footer.items[0]
|
|
const mainLinks = footerData.main_links
|
|
const secondaryLinks = footerData.secondary_links
|
|
const tertiaryLinks = footerData.tertiary_links
|
|
if (mainLinks) {
|
|
mainLinks.forEach(({ pageConnection }) => {
|
|
connections.push(pageConnection)
|
|
})
|
|
}
|
|
secondaryLinks?.forEach(({ links }) => {
|
|
if (links) {
|
|
links.forEach(({ pageConnection }) => {
|
|
connections.push(pageConnection)
|
|
})
|
|
}
|
|
})
|
|
if (tertiaryLinks) {
|
|
tertiaryLinks.forEach(({ pageConnection }) => {
|
|
connections.push(pageConnection)
|
|
})
|
|
}
|
|
|
|
return connections
|
|
}
|
|
|
|
export function getSiteConfigConnections(refs: GetSiteConfigRefData) {
|
|
const siteConfigData = refs.all_site_config.items[0]
|
|
const connections: System["system"][] = []
|
|
|
|
if (!siteConfigData.sitewide_alert.alert) return connections
|
|
|
|
const alertConnection = siteConfigData.sitewide_alert.alert.alertConnection
|
|
|
|
alertConnection.edges.forEach(({ node }) => {
|
|
connections.push(node.system)
|
|
|
|
const link = node.link.link
|
|
if (link) {
|
|
connections.push(link)
|
|
}
|
|
node.sidepeek_content.content.embedded_itemsConnection.edges.forEach(
|
|
({ node }) => {
|
|
if (node.system) {
|
|
connections.push(node.system)
|
|
}
|
|
}
|
|
)
|
|
})
|
|
|
|
return connections
|
|
}
|
|
|
|
export function getAlertPhoneContactData(
|
|
alert: AlertOutput,
|
|
contactConfig: ContactConfig
|
|
) {
|
|
if (alert.phoneContact) {
|
|
const { displayText, phoneNumber, footnote } = alert.phoneContact
|
|
|
|
return {
|
|
displayText,
|
|
phoneNumber: getValueFromContactConfig(phoneNumber, contactConfig),
|
|
footnote: footnote
|
|
? getValueFromContactConfig(footnote, contactConfig)
|
|
: null,
|
|
}
|
|
}
|
|
return null
|
|
}
|
|
|
|
export const safeUnion = <T extends z.ZodTypeAny>(schema: T) =>
|
|
z.preprocess((val) => {
|
|
try {
|
|
return schema.parse(val)
|
|
} catch (err) {
|
|
Sentry.captureException(err)
|
|
logger.warn("Invalid node in safeUnion", err)
|
|
return null
|
|
}
|
|
}, schema)
|