feat(SW-664): Hotel listing component and queries for content pages

This commit is contained in:
Erik Tiekstra
2024-12-11 14:46:38 +01:00
parent 118f1afafa
commit 3939bf7cdc
32 changed files with 989 additions and 140 deletions
+80 -24
View File
@@ -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
}