211 lines
5.8 KiB
TypeScript
211 lines
5.8 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 { generateRefsResponseTag } from "@/utils/generateTag"
|
|
|
|
import { getCityByCityIdentifier } from "../../hotels/utils"
|
|
import {
|
|
destinationCityPageRefsSchema,
|
|
destinationCityPageSchema,
|
|
} from "./output"
|
|
import {
|
|
getDestinationCityPageCounter,
|
|
getDestinationCityPageFailCounter,
|
|
getDestinationCityPageRefsCounter,
|
|
getDestinationCityPageRefsFailCounter,
|
|
getDestinationCityPageRefsSuccessCounter,
|
|
getDestinationCityPageSuccessCounter,
|
|
} from "./telemetry"
|
|
import { generatePageTags } from "./utils"
|
|
|
|
import {
|
|
TrackingChannelEnum,
|
|
type TrackingSDKPageData,
|
|
} from "@/types/components/tracking"
|
|
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 },
|
|
{
|
|
key: generateRefsResponseTag(lang, uid),
|
|
ttl: "max",
|
|
}
|
|
)
|
|
|
|
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,
|
|
},
|
|
{
|
|
key: tags,
|
|
ttl: "max",
|
|
}
|
|
)
|
|
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 destinationCityPage = validatedResponse.data.destination_city_page
|
|
const cityIdentifier = destinationCityPage.destination_settings.city
|
|
if (!cityIdentifier) {
|
|
return null
|
|
}
|
|
|
|
const city = await getCityByCityIdentifier({
|
|
cityIdentifier,
|
|
lang,
|
|
serviceToken,
|
|
})
|
|
|
|
if (!city) {
|
|
getDestinationCityPageFailCounter.add(1, {
|
|
lang,
|
|
uid: `${uid}`,
|
|
error_type: "not_found",
|
|
error: `Couldn't find city with cityIdentifier: ${cityIdentifier}`,
|
|
})
|
|
|
|
console.error(
|
|
"contentstack.destinationCityPage not found error",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
error: `Couldn't find city with cityIdentifier: ${cityIdentifier}`,
|
|
})
|
|
)
|
|
return null
|
|
}
|
|
|
|
getDestinationCityPageSuccessCounter.add(1, { lang, uid: `${uid}` })
|
|
console.info(
|
|
"contentstack.destinationCityPage success",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
})
|
|
)
|
|
|
|
const system = destinationCityPage.system
|
|
const pageName = `destinations|${city.country}|${city.name}`
|
|
|
|
const tracking: TrackingSDKPageData = {
|
|
pageId: system.uid,
|
|
domainLanguage: system.locale,
|
|
publishDate: system.updated_at,
|
|
createDate: system.created_at,
|
|
channel: TrackingChannelEnum.hotels,
|
|
pageType: "citypage",
|
|
pageName,
|
|
siteSections: pageName,
|
|
siteVersion: "new-web",
|
|
}
|
|
|
|
return {
|
|
destinationCityPage,
|
|
cityIdentifier,
|
|
city,
|
|
tracking,
|
|
}
|
|
}),
|
|
})
|