Include more details when throwing errors for debugging in Sentry * WIP throw errors with more details for debugging in Sentry * Fix throwing response-data * Clearer message when a response fails * Add message to errors * better typings * . * Try to send profileID and membershipNumber to Sentry when we fail to parse the apiResponse * rename notFound -> notFoundError * Merge branch 'master' of bitbucket.org:scandic-swap/web into chore/add-error-details-for-sentry Approved-by: Linus Flood
169 lines
4.8 KiB
TypeScript
169 lines
4.8 KiB
TypeScript
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
|
|
import { notFoundError } from "../../../errors"
|
|
import { GetCollectionPageRefs } from "../../../graphql/Query/CollectionPage/CollectionPage.graphql"
|
|
import { request } from "../../../graphql/request"
|
|
import {
|
|
CollectionPageEnum,
|
|
type CollectionPageRefs,
|
|
type GetCollectionPageRefsSchema,
|
|
} from "../../../types/collectionPage"
|
|
import {
|
|
generateRefsResponseTag,
|
|
generateTag,
|
|
generateTagsFromAssetSystem,
|
|
generateTagsFromSystem,
|
|
} from "../../../utils/generateTag"
|
|
import { collectionPageRefsSchema } from "./output"
|
|
|
|
import type { Lang } from "@scandic-hotels/common/constants/language"
|
|
|
|
import type { AssetSystem, System } from "../schemas/system"
|
|
|
|
export async function fetchCollectionPageRefs(lang: Lang, uid: string) {
|
|
const getCollectionPageRefsCounter = createCounter(
|
|
"trpc.contentstack.collectionPage.get.refs"
|
|
)
|
|
const metricsGetCollectionPageRefs = getCollectionPageRefsCounter.init({
|
|
lang,
|
|
uid,
|
|
})
|
|
|
|
metricsGetCollectionPageRefs.start()
|
|
|
|
const cacheKey = generateRefsResponseTag(lang, uid)
|
|
const variables = {
|
|
locale: lang,
|
|
uid,
|
|
}
|
|
const refsResponse = await request<GetCollectionPageRefsSchema>(
|
|
GetCollectionPageRefs,
|
|
variables,
|
|
{
|
|
key: cacheKey,
|
|
ttl: "max",
|
|
}
|
|
)
|
|
|
|
if (!refsResponse.data) {
|
|
metricsGetCollectionPageRefs.noDataError()
|
|
throw notFoundError({
|
|
message: "GetCollectionPageRefs returned no data",
|
|
errorDetails: variables,
|
|
})
|
|
}
|
|
|
|
return refsResponse.data
|
|
}
|
|
|
|
export function validateCollectionPageRefs(
|
|
data: GetCollectionPageRefsSchema,
|
|
lang: Lang,
|
|
uid: string
|
|
) {
|
|
const getCollectionPageRefsCounter = createCounter(
|
|
"trpc.contentstack.collectionPage.get.refs"
|
|
)
|
|
const metricsGetCollectionPageRefs = getCollectionPageRefsCounter.init({
|
|
lang,
|
|
uid,
|
|
})
|
|
|
|
const validatedData = collectionPageRefsSchema.safeParse(data)
|
|
if (!validatedData.success) {
|
|
metricsGetCollectionPageRefs.validationError(validatedData.error)
|
|
return null
|
|
}
|
|
|
|
metricsGetCollectionPageRefs.success()
|
|
|
|
return validatedData.data
|
|
}
|
|
|
|
export function generatePageTags(
|
|
validatedData: CollectionPageRefs,
|
|
lang: Lang
|
|
): string[] {
|
|
const connections = getConnections(validatedData)
|
|
const assetConnections = getConnectionsFromAssets(validatedData)
|
|
return [
|
|
generateTagsFromSystem(lang, connections),
|
|
generateTagsFromAssetSystem(assetConnections),
|
|
generateTag(lang, validatedData.collection_page.system.uid),
|
|
].flat()
|
|
}
|
|
|
|
export function getConnectionsFromAssets({
|
|
collection_page,
|
|
}: CollectionPageRefs) {
|
|
const connections: AssetSystem["system"][] = []
|
|
|
|
if (collection_page.hero_video?.sourceConnection.edges[0]) {
|
|
connections.push(
|
|
collection_page.hero_video.sourceConnection.edges[0].node.system
|
|
)
|
|
}
|
|
|
|
if (collection_page.blocks) {
|
|
collection_page.blocks.forEach((block) => {
|
|
switch (block.__typename) {
|
|
case CollectionPageEnum.ContentStack.blocks.VideoCard:
|
|
if (block.video_card?.video.sourceConnection.edges[0]) {
|
|
connections.push(
|
|
block.video_card.video.sourceConnection.edges[0].node.system
|
|
)
|
|
}
|
|
break
|
|
case CollectionPageEnum.ContentStack.blocks.Video:
|
|
if (block.video?.sourceConnection.edges[0]) {
|
|
connections.push(block.video.sourceConnection.edges[0].node.system)
|
|
}
|
|
break
|
|
default:
|
|
break
|
|
}
|
|
})
|
|
}
|
|
|
|
return connections
|
|
}
|
|
|
|
export function getConnections({ collection_page }: CollectionPageRefs) {
|
|
const connections: System["system"][] = [collection_page.system]
|
|
if (collection_page.blocks) {
|
|
collection_page.blocks.forEach((block) => {
|
|
const typeName = block.__typename
|
|
switch (typeName) {
|
|
case CollectionPageEnum.ContentStack.blocks.Shortcuts:
|
|
if (block.shortcuts.shortcuts.length) {
|
|
connections.push(...block.shortcuts.shortcuts.filter((c) => !!c))
|
|
}
|
|
break
|
|
case CollectionPageEnum.ContentStack.blocks.CardsGrid:
|
|
if (block.cards_grid.length) {
|
|
connections.push(...block.cards_grid)
|
|
}
|
|
break
|
|
case CollectionPageEnum.ContentStack.blocks.UspGrid:
|
|
if (block.usp_grid.length) {
|
|
connections.push(...block.usp_grid.filter((c) => !!c))
|
|
}
|
|
break
|
|
case CollectionPageEnum.ContentStack.blocks.VideoCard:
|
|
if (block.video_card?.system) {
|
|
connections.push(block.video_card.system)
|
|
}
|
|
break
|
|
case CollectionPageEnum.ContentStack.blocks.DynamicContent:
|
|
case CollectionPageEnum.ContentStack.blocks.Video:
|
|
break
|
|
default:
|
|
const _exhaustiveCheck: never = typeName
|
|
break
|
|
}
|
|
})
|
|
}
|
|
|
|
return connections
|
|
}
|