feat(SW-664): Hotel listing component and queries for content pages
This commit is contained in:
@@ -18,6 +18,7 @@ import {
|
||||
dynamicContentRefsSchema,
|
||||
dynamicContentSchema as blockDynamicContentSchema,
|
||||
} from "../schemas/blocks/dynamicContent"
|
||||
import { hotelListingSchema } from "../schemas/blocks/hotelListing"
|
||||
import {
|
||||
shortcutsRefsSchema,
|
||||
shortcutsSchema,
|
||||
@@ -103,6 +104,12 @@ export const contentPageAccordion = z
|
||||
})
|
||||
.merge(accordionSchema)
|
||||
|
||||
export const contentPageHotelListing = z
|
||||
.object({
|
||||
__typename: z.literal(ContentPageEnum.ContentStack.blocks.HotelListing),
|
||||
})
|
||||
.merge(hotelListingSchema)
|
||||
|
||||
export const blocksSchema = z.discriminatedUnion("__typename", [
|
||||
contentPageAccordion,
|
||||
contentPageCards,
|
||||
@@ -112,6 +119,7 @@ export const blocksSchema = z.discriminatedUnion("__typename", [
|
||||
contentPageTable,
|
||||
contentPageTextCols,
|
||||
contentPageUspGrid,
|
||||
contentPageHotelListing,
|
||||
])
|
||||
|
||||
export const contentPageSidebarContent = z
|
||||
|
||||
@@ -2,6 +2,8 @@ import { z } from "zod"
|
||||
|
||||
import { discriminatedUnionArray } from "@/lib/discriminatedUnion"
|
||||
|
||||
import { removeMultipleSlashes } from "@/utils/url"
|
||||
|
||||
import {
|
||||
activitiesCardRefSchema,
|
||||
activitiesCardSchema,
|
||||
@@ -58,3 +60,26 @@ export const hotelPageRefsSchema = z.object({
|
||||
url: z.string(),
|
||||
}),
|
||||
})
|
||||
|
||||
export const hotelPageUrlSchema = z
|
||||
.object({
|
||||
all_hotel_page: z.object({
|
||||
items: z
|
||||
.array(
|
||||
z.object({
|
||||
url: z.string(),
|
||||
system: systemSchema,
|
||||
})
|
||||
)
|
||||
.max(1),
|
||||
}),
|
||||
})
|
||||
.transform((data) => {
|
||||
const page = data.all_hotel_page.items[0]
|
||||
if (!page) {
|
||||
return null
|
||||
}
|
||||
|
||||
const lang = page.system.locale
|
||||
return removeMultipleSlashes(`/${lang}/${page.url}`)
|
||||
})
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import { metrics } from "@opentelemetry/api"
|
||||
|
||||
import { GetHotelPage } from "@/lib/graphql/Query/HotelPage/HotelPage.graphql"
|
||||
import { request } from "@/lib/graphql/request"
|
||||
import { notFound } from "@/server/errors/trpc"
|
||||
@@ -8,20 +6,13 @@ import { contentstackExtendedProcedureUID, router } from "@/server/trpc"
|
||||
import { generateTag } from "@/utils/generateTag"
|
||||
|
||||
import { hotelPageSchema } from "./output"
|
||||
import {
|
||||
getHotelPageCounter,
|
||||
getHotelPageFailCounter,
|
||||
getHotelPageSuccessCounter,
|
||||
} from "./telemetry"
|
||||
|
||||
import { GetHotelPageData } from "@/types/trpc/routers/contentstack/hotelPage"
|
||||
|
||||
// OpenTelemetry metrics
|
||||
const meter = metrics.getMeter("trpc.contentstack.hotelPage")
|
||||
const getHotelPageCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPage.get"
|
||||
)
|
||||
const getHotelPageSuccessCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPage.get-success"
|
||||
)
|
||||
const getHotelPageFailCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPage.get-fail"
|
||||
)
|
||||
import type { GetHotelPageData } from "@/types/trpc/routers/contentstack/hotelPage"
|
||||
|
||||
export const hotelPageQueryRouter = router({
|
||||
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
||||
|
||||
33
server/routers/contentstack/hotelPage/telemetry.ts
Normal file
33
server/routers/contentstack/hotelPage/telemetry.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { metrics } from "@opentelemetry/api"
|
||||
|
||||
const meter = metrics.getMeter("trpc.contentstack.hotelPage")
|
||||
|
||||
export const getHotelPageRefsCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPage.get"
|
||||
)
|
||||
export const getHotelPageRefsFailCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPage.get-fail"
|
||||
)
|
||||
export const getHotelPageRefsSuccessCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPage.get-success"
|
||||
)
|
||||
|
||||
export const getHotelPageCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPage.get"
|
||||
)
|
||||
export const getHotelPageSuccessCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPage.get-success"
|
||||
)
|
||||
export const getHotelPageFailCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPage.get-fail"
|
||||
)
|
||||
|
||||
export const getHotelPageUrlCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPageUrl.get"
|
||||
)
|
||||
export const getHotelPageUrlSuccessCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPageUrl.get-success"
|
||||
)
|
||||
export const getHotelPageUrlFailCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPageUrl.get-fail"
|
||||
)
|
||||
@@ -1,37 +1,32 @@
|
||||
import { metrics } from "@opentelemetry/api"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
import { GetHotelPageRefs } from "@/lib/graphql/Query/HotelPage/HotelPage.graphql"
|
||||
import { GetHotelPageUrl } from "@/lib/graphql/Query/HotelPage/HotelPageUrl.graphql"
|
||||
import { request } from "@/lib/graphql/request"
|
||||
import { notFound } from "@/server/errors/trpc"
|
||||
|
||||
import { generateTag, generateTagsFromSystem } from "@/utils/generateTag"
|
||||
import {
|
||||
generateHotelUrlTag,
|
||||
generateTag,
|
||||
generateTagsFromSystem,
|
||||
} from "@/utils/generateTag"
|
||||
|
||||
import { hotelPageRefsSchema } from "./output"
|
||||
import { hotelPageRefsSchema, hotelPageUrlSchema } from "./output"
|
||||
import {
|
||||
getHotelPageRefsCounter,
|
||||
getHotelPageRefsFailCounter,
|
||||
getHotelPageRefsSuccessCounter,
|
||||
getHotelPageUrlCounter,
|
||||
getHotelPageUrlFailCounter,
|
||||
getHotelPageUrlSuccessCounter,
|
||||
} from "./telemetry"
|
||||
|
||||
import { HotelPageEnum } from "@/types/enums/hotelPage"
|
||||
import { System } from "@/types/requests/system"
|
||||
import {
|
||||
import type { System } from "@/types/requests/system"
|
||||
import type {
|
||||
GetHotelPageRefsSchema,
|
||||
GetHotelPageUrlData,
|
||||
HotelPageRefs,
|
||||
} from "@/types/trpc/routers/contentstack/hotelPage"
|
||||
|
||||
const meter = metrics.getMeter("trpc.hotelPage")
|
||||
// OpenTelemetry metrics: HotelPage
|
||||
|
||||
export const getHotelPageCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPage.get"
|
||||
)
|
||||
|
||||
const getHotelPageRefsCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPage.get"
|
||||
)
|
||||
const getHotelPageRefsFailCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPage.get-fail"
|
||||
)
|
||||
const getHotelPageRefsSuccessCounter = meter.createCounter(
|
||||
"trpc.contentstack.hotelPage.get-success"
|
||||
)
|
||||
import type { Lang } from "@/constants/languages"
|
||||
|
||||
export async function fetchHotelPageRefs(lang: Lang, uid: string) {
|
||||
getHotelPageRefsCounter.add(1, { lang, uid })
|
||||
@@ -140,3 +135,64 @@ export function getConnections({ hotel_page }: HotelPageRefs) {
|
||||
}
|
||||
return connections
|
||||
}
|
||||
|
||||
export async function getHotelPageUrl(lang: Lang, hotelId: string) {
|
||||
getHotelPageUrlCounter.add(1, { lang, hotelId })
|
||||
console.info(
|
||||
"contentstack.hotelPageUrl start",
|
||||
JSON.stringify({ query: { lang, hotelId } })
|
||||
)
|
||||
const response = await request<GetHotelPageUrlData>(
|
||||
GetHotelPageUrl,
|
||||
{
|
||||
locale: lang,
|
||||
hotelId,
|
||||
},
|
||||
{
|
||||
cache: "force-cache",
|
||||
next: {
|
||||
tags: [generateHotelUrlTag(lang, hotelId)],
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
if (!response.data) {
|
||||
getHotelPageUrlFailCounter.add(1, {
|
||||
lang,
|
||||
hotelId,
|
||||
error_type: "not_found",
|
||||
error: `Hotel page not found for hotelId: ${hotelId}`,
|
||||
})
|
||||
console.error(
|
||||
"contentstack.hotelPageUrl not found error",
|
||||
JSON.stringify({ query: { lang, hotelId } })
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
const validatedHotelPageUrl = hotelPageUrlSchema.safeParse(response.data)
|
||||
|
||||
if (!validatedHotelPageUrl.success) {
|
||||
getHotelPageUrlFailCounter.add(1, {
|
||||
lang,
|
||||
hotelId,
|
||||
error_type: "validation_error",
|
||||
error: JSON.stringify(validatedHotelPageUrl.error),
|
||||
})
|
||||
console.error(
|
||||
"contentstack.hotelPageUrl validation error",
|
||||
JSON.stringify({
|
||||
query: { lang, hotelId },
|
||||
error: validatedHotelPageUrl.error,
|
||||
})
|
||||
)
|
||||
return null
|
||||
}
|
||||
getHotelPageUrlSuccessCounter.add(1, { lang, hotelId })
|
||||
console.info(
|
||||
"contentstack.hotelPageUrl success",
|
||||
JSON.stringify({ query: { lang, hotelId } })
|
||||
)
|
||||
|
||||
return validatedHotelPageUrl.data
|
||||
}
|
||||
|
||||
62
server/routers/contentstack/schemas/blocks/hotelListing.ts
Normal file
62
server/routers/contentstack/schemas/blocks/hotelListing.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { BlocksEnums } from "@/types/enums/blocks"
|
||||
import { Country } from "@/types/enums/country"
|
||||
|
||||
export const locationFilterSchema = z
|
||||
.object({
|
||||
country: z.nativeEnum(Country).nullable(),
|
||||
city_denmark: z.string().optional().nullable(),
|
||||
city_finland: z.string().optional().nullable(),
|
||||
city_germany: z.string().optional().nullable(),
|
||||
city_poland: z.string().optional().nullable(),
|
||||
city_norway: z.string().optional().nullable(),
|
||||
city_sweden: z.string().optional().nullable(),
|
||||
excluded: z.array(z.string()),
|
||||
})
|
||||
.transform((data) => {
|
||||
const cities = [
|
||||
data.city_denmark,
|
||||
data.city_finland,
|
||||
data.city_germany,
|
||||
data.city_poland,
|
||||
data.city_norway,
|
||||
data.city_sweden,
|
||||
].filter((city): city is string => Boolean(city))
|
||||
|
||||
// When there are multiple city values, we return null as the filter is invalid.
|
||||
if (cities.length > 1) {
|
||||
return null
|
||||
}
|
||||
|
||||
return {
|
||||
country: cities.length ? null : data.country,
|
||||
city: cities.length ? cities[0] : null,
|
||||
excluded: data.excluded,
|
||||
}
|
||||
})
|
||||
|
||||
export const hotelListingSchema = z.object({
|
||||
typename: z
|
||||
.literal(BlocksEnums.block.HotelListing)
|
||||
.default(BlocksEnums.block.HotelListing),
|
||||
hotel_listing: z
|
||||
.object({
|
||||
heading: z.string().optional(),
|
||||
location_filter: locationFilterSchema,
|
||||
manual_filter: z
|
||||
.object({
|
||||
hotels: z.array(z.string()),
|
||||
})
|
||||
.transform((data) => ({ hotels: data.hotels.filter(Boolean) })),
|
||||
content_type: z.enum(["hotel", "restaurant", "meeting"]),
|
||||
})
|
||||
.transform(({ heading, location_filter, manual_filter, content_type }) => {
|
||||
return {
|
||||
heading,
|
||||
locationFilter: location_filter,
|
||||
hotelsToInclude: manual_filter.hotels,
|
||||
contentType: content_type,
|
||||
}
|
||||
}),
|
||||
})
|
||||
Reference in New Issue
Block a user