180 lines
5.2 KiB
TypeScript
180 lines
5.2 KiB
TypeScript
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
|
|
import { notFound } from "../../../errors"
|
|
import { batchRequest } from "../../../graphql/batchRequest"
|
|
import {
|
|
GetContentPageBlocksRefs,
|
|
GetContentPageRefs,
|
|
} from "../../../graphql/Query/ContentPage/ContentPage.graphql"
|
|
import { ContentPageEnum } from "../../../types/contentPage"
|
|
import {
|
|
generateRefsResponseTag,
|
|
generateTag,
|
|
generateTagsFromAssetSystem,
|
|
generateTagsFromSystem,
|
|
} from "../../../utils/generateTag"
|
|
import { contentPageRefsSchema } from "./output"
|
|
|
|
import type { Lang } from "@scandic-hotels/common/constants/language"
|
|
|
|
import type {
|
|
ContentPageRefs,
|
|
GetContentPageRefsSchema,
|
|
} from "../../../types/contentPage"
|
|
import type { System } from "../schemas/system"
|
|
|
|
export async function fetchContentPageRefs(lang: Lang, uid: string) {
|
|
const getContentPageRefsCounter = createCounter(
|
|
"trpc.contentstack.contentPage.get.refs"
|
|
)
|
|
const metricsGetContentPageRefs = getContentPageRefsCounter.init({
|
|
lang,
|
|
uid,
|
|
})
|
|
|
|
metricsGetContentPageRefs.start()
|
|
|
|
const res = await batchRequest<GetContentPageRefsSchema>([
|
|
{
|
|
document: GetContentPageRefs,
|
|
variables: { locale: lang, uid },
|
|
cacheOptions: {
|
|
key: generateRefsResponseTag(lang, uid),
|
|
ttl: "max",
|
|
},
|
|
},
|
|
{
|
|
document: GetContentPageBlocksRefs,
|
|
variables: { locale: lang, uid },
|
|
cacheOptions: {
|
|
key: generateTag(lang, uid + 1),
|
|
ttl: "max",
|
|
},
|
|
},
|
|
])
|
|
if (!res.data) {
|
|
const notFoundError = notFound(res)
|
|
metricsGetContentPageRefs.noDataError()
|
|
throw notFoundError
|
|
}
|
|
|
|
const validatedData = contentPageRefsSchema.safeParse(res.data)
|
|
if (!validatedData.success) {
|
|
metricsGetContentPageRefs.validationError(validatedData.error)
|
|
return null
|
|
}
|
|
|
|
metricsGetContentPageRefs.success()
|
|
|
|
return validatedData.data
|
|
}
|
|
|
|
export function generatePageTags(
|
|
validatedData: ContentPageRefs,
|
|
lang: Lang
|
|
): string[] {
|
|
const connections = getConnections(validatedData)
|
|
return [
|
|
generateTagsFromSystem(lang, connections),
|
|
generateTagsFromAssetSystem(connections),
|
|
generateTag(lang, validatedData.content_page.system.uid),
|
|
].flat()
|
|
}
|
|
|
|
export function getConnections({ content_page }: ContentPageRefs) {
|
|
const connections: System["system"][] = [content_page.system]
|
|
|
|
if (content_page.blocks) {
|
|
content_page.blocks.forEach((block) => {
|
|
switch (block.__typename) {
|
|
case ContentPageEnum.ContentStack.blocks.Accordion: {
|
|
if (block.accordion.length) {
|
|
connections.push(...block.accordion.filter((c) => !!c))
|
|
}
|
|
break
|
|
}
|
|
case ContentPageEnum.ContentStack.blocks.Content:
|
|
{
|
|
if (block?.content?.length) {
|
|
connections.push(...block.content)
|
|
}
|
|
}
|
|
break
|
|
case ContentPageEnum.ContentStack.blocks.CardsGrid: {
|
|
if (block.cards_grid.length) {
|
|
connections.push(...block.cards_grid)
|
|
}
|
|
break
|
|
}
|
|
case ContentPageEnum.ContentStack.blocks.DynamicContent: {
|
|
if (block.dynamic_content.link) {
|
|
connections.push(block.dynamic_content.link)
|
|
}
|
|
break
|
|
}
|
|
case ContentPageEnum.ContentStack.blocks.Shortcuts: {
|
|
if (block.shortcuts.shortcuts.length) {
|
|
connections.push(...block.shortcuts.shortcuts.filter((c) => !!c))
|
|
}
|
|
break
|
|
}
|
|
case ContentPageEnum.ContentStack.blocks.TextCols: {
|
|
if (block.text_cols.length) {
|
|
connections.push(...block.text_cols)
|
|
}
|
|
break
|
|
}
|
|
case ContentPageEnum.ContentStack.blocks.UspGrid: {
|
|
if (block.usp_grid.length) {
|
|
connections.push(...block.usp_grid.filter((c) => !!c))
|
|
}
|
|
break
|
|
}
|
|
case ContentPageEnum.ContentStack.blocks.CardsGrid: {
|
|
if (block.cards_grid.length) {
|
|
block.cards_grid.forEach((card) => {
|
|
connections.push(card)
|
|
})
|
|
}
|
|
break
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
if (content_page.sidebar) {
|
|
content_page.sidebar.forEach((block) => {
|
|
switch (block.__typename) {
|
|
case ContentPageEnum.ContentStack.sidebar.Content:
|
|
if (block.content.length) {
|
|
connections.push(...block.content.filter((c) => !!c))
|
|
}
|
|
break
|
|
case ContentPageEnum.ContentStack.sidebar.JoinLoyaltyContact:
|
|
if (block.join_loyalty_contact?.button) {
|
|
connections.push(block.join_loyalty_contact.button)
|
|
}
|
|
break
|
|
case ContentPageEnum.ContentStack.sidebar.ScriptedCard:
|
|
if (block.scripted_card?.length) {
|
|
connections.push(...block.scripted_card)
|
|
}
|
|
break
|
|
case ContentPageEnum.ContentStack.sidebar.TeaserCard:
|
|
if (block.teaser_card?.length) {
|
|
connections.push(...block.teaser_card)
|
|
}
|
|
break
|
|
case ContentPageEnum.ContentStack.sidebar.QuickLinks:
|
|
if (block.shortcuts.shortcuts.length) {
|
|
connections.push(...block.shortcuts.shortcuts.filter((c) => !!c))
|
|
}
|
|
break
|
|
default:
|
|
break
|
|
}
|
|
})
|
|
}
|
|
return connections
|
|
}
|