Feat/SW-1449 destination page * feat(SW-1449): Added destination country page * feat(SW-1449): added destination city page Approved-by: Fredrik Thorsson Approved-by: Matilda Landström
180 lines
5.0 KiB
TypeScript
180 lines
5.0 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 { contentstackExtendedProcedureUID, router } from "@/server/trpc"
|
|
|
|
import { generateTag } from "@/utils/generateTag"
|
|
|
|
import {
|
|
destinationCityPageRefsSchema,
|
|
destinationCityPageSchema,
|
|
} from "./output"
|
|
import {
|
|
getDestinationCityPageCounter,
|
|
getDestinationCityPageFailCounter,
|
|
getDestinationCityPageRefsCounter,
|
|
getDestinationCityPageRefsFailCounter,
|
|
getDestinationCityPageRefsSuccessCounter,
|
|
getDestinationCityPageSuccessCounter,
|
|
} from "./telemetry"
|
|
|
|
import {
|
|
TrackingChannelEnum,
|
|
type TrackingSDKPageData,
|
|
} from "@/types/components/tracking"
|
|
import type {
|
|
GetDestinationCityPageData,
|
|
GetDestinationCityPageRefsSchema,
|
|
} from "@/types/trpc/routers/contentstack/destinationCityPage"
|
|
|
|
export const destinationCityPageQueryRouter = router({
|
|
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
|
const { lang, uid } = 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 } })
|
|
)
|
|
|
|
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: [generateTag(lang, uid)],
|
|
},
|
|
}
|
|
)
|
|
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 destinationCityPage = destinationCityPageSchema.safeParse(
|
|
response.data
|
|
)
|
|
|
|
if (!destinationCityPage.success) {
|
|
getDestinationCityPageFailCounter.add(1, {
|
|
lang,
|
|
uid: `${uid}`,
|
|
error_type: "validation_error",
|
|
error: JSON.stringify(destinationCityPage.error),
|
|
})
|
|
console.error(
|
|
"contentstack.destinationCityPage validation error",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
error: destinationCityPage.error,
|
|
})
|
|
)
|
|
return null
|
|
}
|
|
|
|
getDestinationCityPageSuccessCounter.add(1, { lang, uid: `${uid}` })
|
|
console.info(
|
|
"contentstack.destinationCityPage success",
|
|
JSON.stringify({
|
|
query: { lang, uid },
|
|
})
|
|
)
|
|
|
|
const system = destinationCityPage.data.destination_city_page.system
|
|
const tracking: TrackingSDKPageData = {
|
|
pageId: system.uid,
|
|
domainLanguage: lang,
|
|
publishDate: system.updated_at,
|
|
createDate: system.created_at,
|
|
channel: TrackingChannelEnum["destination-page"],
|
|
pageType: "staticcontentpage",
|
|
pageName: destinationCityPage.data.trackingProps.url,
|
|
siteSections: destinationCityPage.data.trackingProps.url,
|
|
siteVersion: "new-web",
|
|
}
|
|
|
|
return {
|
|
destinationCityPage: destinationCityPage.data.destination_city_page,
|
|
tracking,
|
|
}
|
|
}),
|
|
})
|