Merged in feat/rework-contentstack (pull request #3493)
Feat(SW-3708): refactor contentstack fetching (removing all refs) and cache invalidation * Remove all REFS * Revalidate correct language * PR fixes * PR fixes * Throw when errors from contentstack api Approved-by: Joakim Jäderberg
This commit is contained in:
@@ -6,21 +6,11 @@ import { removeMultipleSlashes } from "@scandic-hotels/common/utils/url"
|
||||
import { DestinationCityPageEnum } from "../../../types/destinationCityPage"
|
||||
import { isDefined } from "../../../utils"
|
||||
import { discriminatedUnionArray } from "../../../utils/discriminatedUnion"
|
||||
import {
|
||||
accordionRefsSchema,
|
||||
accordionSchema,
|
||||
} from "../schemas/blocks/accordion"
|
||||
import { contentRefsSchema, contentSchema } from "../schemas/blocks/content"
|
||||
import {
|
||||
destinationFiltersRefsSchema,
|
||||
transformedDestinationFiltersSchema,
|
||||
} from "../schemas/destinationFilters"
|
||||
import { accordionSchema } from "../schemas/blocks/accordion"
|
||||
import { contentSchema } from "../schemas/blocks/content"
|
||||
import { transformedDestinationFiltersSchema } from "../schemas/destinationFilters"
|
||||
import { mapLocationSchema } from "../schemas/mapLocation"
|
||||
import {
|
||||
linkRefsUnionSchema,
|
||||
linkUnionSchema,
|
||||
transformPageLink,
|
||||
} from "../schemas/pageLinks"
|
||||
import { linkUnionSchema, transformPageLink } from "../schemas/pageLinks"
|
||||
import { systemSchema } from "../schemas/system"
|
||||
|
||||
import type { ImageVaultAsset } from "@scandic-hotels/common/utils/imageVault"
|
||||
@@ -227,47 +217,3 @@ export const batchedCityPageUrlsSchema = z
|
||||
.transform((allItems) => {
|
||||
return allItems.flatMap((item) => item.data)
|
||||
})
|
||||
|
||||
/** REFS */
|
||||
const destinationCityPageContentRefs = z
|
||||
.object({
|
||||
__typename: z.literal(DestinationCityPageEnum.ContentStack.blocks.Content),
|
||||
})
|
||||
.merge(contentRefsSchema)
|
||||
|
||||
const destinationCityPageAccordionRefs = z
|
||||
.object({
|
||||
__typename: z.literal(
|
||||
DestinationCityPageEnum.ContentStack.blocks.Accordion
|
||||
),
|
||||
})
|
||||
.merge(accordionRefsSchema)
|
||||
|
||||
const blocksRefsSchema = z.discriminatedUnion("__typename", [
|
||||
destinationCityPageAccordionRefs,
|
||||
destinationCityPageContentRefs,
|
||||
])
|
||||
|
||||
export const destinationCityPageRefsSchema = z.object({
|
||||
destination_city_page: z.object({
|
||||
destination_settings: destinationCityPageDestinationSettingsSchema,
|
||||
sidepeek_content: z
|
||||
.object({
|
||||
content: z
|
||||
.object({
|
||||
embedded_itemsConnection: z.object({
|
||||
edges: z.array(
|
||||
z.object({
|
||||
node: linkRefsUnionSchema,
|
||||
})
|
||||
),
|
||||
}),
|
||||
})
|
||||
.nullish(),
|
||||
})
|
||||
.nullish(),
|
||||
blocks: discriminatedUnionArray(blocksRefsSchema.options).nullable(),
|
||||
seo_filters: destinationFiltersRefsSchema,
|
||||
system: systemSchema,
|
||||
}),
|
||||
})
|
||||
|
||||
@@ -2,67 +2,21 @@ import { createCounter } from "@scandic-hotels/common/telemetry"
|
||||
|
||||
import { router } from "../../.."
|
||||
import { notFoundError } from "../../../errors"
|
||||
import {
|
||||
GetDestinationCityPage,
|
||||
GetDestinationCityPageRefs,
|
||||
} from "../../../graphql/Query/DestinationCityPage/DestinationCityPage.graphql"
|
||||
import { GetDestinationCityPage } from "../../../graphql/Query/DestinationCityPage/DestinationCityPage.graphql"
|
||||
import { request } from "../../../graphql/request"
|
||||
import { contentStackUidWithServiceProcedure } from "../../../procedures"
|
||||
import { generateRefsResponseTag } from "../../../utils/generateTag"
|
||||
import { generateTag } from "../../../utils/generateTag"
|
||||
import { getCityByCityIdentifier } from "../../hotels/services/getCityByCityIdentifier"
|
||||
import {
|
||||
destinationCityPageRefsSchema,
|
||||
destinationCityPageSchema,
|
||||
} from "./output"
|
||||
import { generatePageTags } from "./utils"
|
||||
import { destinationCityPageSchema } from "./output"
|
||||
|
||||
import type {
|
||||
GetDestinationCityPageData,
|
||||
GetDestinationCityPageRefsSchema,
|
||||
} from "../../../types/destinationCityPage"
|
||||
import type { GetDestinationCityPageData } from "../../../types/destinationCityPage"
|
||||
import type { TrackingPageData } from "../../types"
|
||||
|
||||
export const destinationCityPageQueryRouter = router({
|
||||
get: contentStackUidWithServiceProcedure.query(async ({ ctx }) => {
|
||||
const { lang, uid, serviceToken } = ctx
|
||||
|
||||
const getDestinationCityPageRefsCounter = createCounter(
|
||||
"trpc.contentstack.destinationCityPage.get.refs"
|
||||
)
|
||||
const metricsGetDestinationCityPageRefs =
|
||||
getDestinationCityPageRefsCounter.init({ lang, uid })
|
||||
|
||||
metricsGetDestinationCityPageRefs.start()
|
||||
|
||||
const variables = { locale: lang, uid }
|
||||
const refsResponse = await request<GetDestinationCityPageRefsSchema>(
|
||||
GetDestinationCityPageRefs,
|
||||
variables,
|
||||
{
|
||||
key: generateRefsResponseTag(lang, uid),
|
||||
ttl: "max",
|
||||
}
|
||||
)
|
||||
|
||||
if (!refsResponse.data) {
|
||||
metricsGetDestinationCityPageRefs.noDataError()
|
||||
throw notFoundError({
|
||||
message: "GetDestinationCityPageRefs returned no data",
|
||||
errorDetails: variables,
|
||||
})
|
||||
}
|
||||
|
||||
const validatedRefsData = destinationCityPageRefsSchema.safeParse(
|
||||
refsResponse.data
|
||||
)
|
||||
if (!validatedRefsData.success) {
|
||||
metricsGetDestinationCityPageRefs.validationError(validatedRefsData.error)
|
||||
return null
|
||||
}
|
||||
|
||||
metricsGetDestinationCityPageRefs.success()
|
||||
|
||||
const tags = generatePageTags(validatedRefsData.data, lang)
|
||||
const cacheKey = generateTag(lang, uid)
|
||||
|
||||
const getDestinationCityPageCounter = createCounter(
|
||||
"trpc.contentstack.destinationCityPage.get"
|
||||
@@ -74,11 +28,12 @@ export const destinationCityPageQueryRouter = router({
|
||||
|
||||
metricsGetDestinationCityPage.start()
|
||||
|
||||
const variables = { locale: lang, uid }
|
||||
const response = await request<GetDestinationCityPageData>(
|
||||
GetDestinationCityPage,
|
||||
variables,
|
||||
{
|
||||
key: tags,
|
||||
key: `${cacheKey}:destinationCityPage`,
|
||||
ttl: "max",
|
||||
}
|
||||
)
|
||||
|
||||
@@ -3,72 +3,14 @@ import { createCounter } from "@scandic-hotels/common/telemetry"
|
||||
import { GetCityPageCount } from "../../../graphql/Query/DestinationCityPage/DestinationCityPageCount.graphql"
|
||||
import { GetCityPageUrls } from "../../../graphql/Query/DestinationCityPage/DestinationCityPageUrl.graphql"
|
||||
import { request } from "../../../graphql/request"
|
||||
import { DestinationCityPageEnum } from "../../../types/destinationCityPage"
|
||||
import { generateTag, generateTagsFromSystem } from "../../../utils/generateTag"
|
||||
import { batchedCityPageUrlsSchema, cityPageCountSchema } from "./output"
|
||||
|
||||
import type { Lang } from "@scandic-hotels/common/constants/language"
|
||||
|
||||
import type {
|
||||
DestinationCityPageRefs,
|
||||
GetCityPageCountData,
|
||||
GetCityPageUrlsData,
|
||||
} from "../../../types/destinationCityPage"
|
||||
import type { System } from "../schemas/system"
|
||||
|
||||
export function generatePageTags(
|
||||
validatedData: DestinationCityPageRefs,
|
||||
lang: Lang
|
||||
): string[] {
|
||||
const connections = getConnections(validatedData)
|
||||
return [
|
||||
// This tag is added for the city list data on country pages to invalidate the list when city page changes.
|
||||
generateTag(
|
||||
lang,
|
||||
`city_list_data:${validatedData.destination_city_page.destination_settings.city}`
|
||||
),
|
||||
generateTagsFromSystem(lang, connections),
|
||||
generateTag(lang, validatedData.destination_city_page.system.uid),
|
||||
].flat()
|
||||
}
|
||||
|
||||
export function getConnections({
|
||||
destination_city_page,
|
||||
}: DestinationCityPageRefs) {
|
||||
const connections: System["system"][] = [destination_city_page.system]
|
||||
if (destination_city_page.blocks) {
|
||||
destination_city_page.blocks.forEach((block) => {
|
||||
switch (block.__typename) {
|
||||
case DestinationCityPageEnum.ContentStack.blocks.Accordion: {
|
||||
if (block.accordion.length) {
|
||||
connections.push(...block.accordion.filter((c) => !!c))
|
||||
}
|
||||
break
|
||||
}
|
||||
case DestinationCityPageEnum.ContentStack.blocks.Content:
|
||||
{
|
||||
if (block?.content?.length) {
|
||||
// TS has trouble infering the filtered types
|
||||
// @ts-ignore
|
||||
connections.push(...block.content)
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
})
|
||||
}
|
||||
if (destination_city_page.sidepeek_content) {
|
||||
destination_city_page.sidepeek_content?.content?.embedded_itemsConnection.edges.forEach(
|
||||
({ node }) => {
|
||||
if (node.system) {
|
||||
connections.push(node.system)
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
return connections
|
||||
}
|
||||
|
||||
export async function getCityPageCount(lang: Lang) {
|
||||
const getCityPageCountCounter = createCounter(
|
||||
|
||||
Reference in New Issue
Block a user