feat(SW-1454): added hotel listing * feat(SW-1454): added hotel listing Approved-by: Fredrik Thorsson
170 lines
4.7 KiB
TypeScript
170 lines
4.7 KiB
TypeScript
import {
|
|
GetDestinationCityPage,
|
|
GetDestinationCityPageRefs,
|
|
} from "@/lib/graphql/Query/DestinationCityPage/DestinationCityPage.graphql"
|
|
import { request } from "@/lib/graphql/request"
|
|
import { notFound } from "@/server/errors/trpc"
|
|
import { contentStackUidWithServiceProcedure, router } from "@/server/trpc"
|
|
|
|
import { generateTag } from "@/utils/generateTag"
|
|
|
|
import {
|
|
destinationCityPageRefsSchema,
|
|
destinationCityPageSchema,
|
|
} from "./output"
|
|
import {
|
|
getDestinationCityPageCounter,
|
|
getDestinationCityPageFailCounter,
|
|
getDestinationCityPageRefsCounter,
|
|
getDestinationCityPageRefsFailCounter,
|
|
getDestinationCityPageRefsSuccessCounter,
|
|
getDestinationCityPageSuccessCounter,
|
|
} from "./telemetry"
|
|
import { generatePageTags, getHotelListData } from "./utils"
|
|
|
|
import type {
|
|
GetDestinationCityPageData,
|
|
GetDestinationCityPageRefsSchema,
|
|
} from "@/types/trpc/routers/contentstack/destinationCityPage"
|
|
|
|
export const destinationCityPageQueryRouter = router({
|
|
get: contentStackUidWithServiceProcedure.query(async ({ ctx }) => {
|
|
const { lang, uid, serviceToken } = ctx
|
|
|
|
getDestinationCityPageRefsCounter.add(1, { lang, uid })
|
|
console.info(
|
|
"contentstack.destinationCityPage.refs start",
|
|
JSON.stringify({ query: { lang, uid } })
|
|
)
|
|
|
|
const refsResponse = await request<GetDestinationCityPageRefsSchema>(
|
|
GetDestinationCityPageRefs,
|
|
{ locale: lang, uid },
|
|
{
|
|
cache: "force-cache",
|
|
next: {
|
|
tags: [generateTag(lang, uid)],
|
|
},
|
|
}
|
|
)
|
|
|
|
if (!refsResponse.data) {
|
|
const notFoundError = notFound(refsResponse)
|
|
getDestinationCityPageRefsFailCounter.add(1, {
|
|
lang,
|
|
uid: `${uid}`,
|
|
error_type: "not_found",
|
|
error: JSON.stringify({ code: notFoundError.code }),
|
|
})
|
|
console.error(
|
|
"contentstack.destinationCityPage.refs not found error",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
error: { code: notFoundError.code },
|
|
})
|
|
)
|
|
throw notFoundError
|
|
}
|
|
|
|
const validatedRefsData = destinationCityPageRefsSchema.safeParse(
|
|
refsResponse.data
|
|
)
|
|
if (!validatedRefsData.success) {
|
|
getDestinationCityPageRefsFailCounter.add(1, {
|
|
lang,
|
|
uid: `${uid}`,
|
|
error_type: "validation_error",
|
|
error: JSON.stringify(validatedRefsData.error),
|
|
})
|
|
console.error(
|
|
"contentstack.destinationCityPage.refs validation error",
|
|
JSON.stringify({ query: { lang, uid }, error: validatedRefsData.error })
|
|
)
|
|
return null
|
|
}
|
|
getDestinationCityPageRefsSuccessCounter.add(1, { lang, uid: `${uid}` })
|
|
console.info(
|
|
"contentstack.destinationCityPage.refs success",
|
|
JSON.stringify({ query: { lang, uid } })
|
|
)
|
|
|
|
const tags = generatePageTags(validatedRefsData.data, lang)
|
|
|
|
getDestinationCityPageCounter.add(1, { lang, uid: `${uid}` })
|
|
console.info(
|
|
"contentstack.destinationCityPage start",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
})
|
|
)
|
|
const response = await request<GetDestinationCityPageData>(
|
|
GetDestinationCityPage,
|
|
{
|
|
locale: lang,
|
|
uid,
|
|
},
|
|
{
|
|
cache: "force-cache",
|
|
next: {
|
|
tags,
|
|
},
|
|
}
|
|
)
|
|
if (!response.data) {
|
|
const notFoundError = notFound(response)
|
|
getDestinationCityPageFailCounter.add(1, {
|
|
lang,
|
|
uid: `${uid}`,
|
|
error_type: "not_found",
|
|
error: JSON.stringify({ code: notFoundError.code }),
|
|
})
|
|
console.error(
|
|
"contentstack.destinationCityPage not found error",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
error: { code: notFoundError.code },
|
|
})
|
|
)
|
|
throw notFoundError
|
|
}
|
|
|
|
const validatedResponse = destinationCityPageSchema.safeParse(response.data)
|
|
|
|
if (!validatedResponse.success) {
|
|
getDestinationCityPageFailCounter.add(1, {
|
|
lang,
|
|
uid: `${uid}`,
|
|
error_type: "validation_error",
|
|
error: JSON.stringify(validatedResponse.error),
|
|
})
|
|
console.error(
|
|
"contentstack.destinationCityPage validation error",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
error: validatedResponse.error,
|
|
})
|
|
)
|
|
return null
|
|
}
|
|
|
|
const hotels = await getHotelListData(
|
|
lang,
|
|
serviceToken,
|
|
validatedResponse.data.destinationCityPage.destination_settings.city
|
|
)
|
|
|
|
getDestinationCityPageSuccessCounter.add(1, { lang, uid: `${uid}` })
|
|
console.info(
|
|
"contentstack.destinationCityPage success",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
})
|
|
)
|
|
|
|
return {
|
|
...validatedResponse.data,
|
|
hotels,
|
|
}
|
|
}),
|
|
})
|