Counter name is now searchable and add counter for redirects * refactor: createCounter() only takes one argument, the name of the counter. Makes it easier to search for * feat: add counter when we do a redirect from redirect-service Approved-by: Linus Flood
67 lines
2.0 KiB
TypeScript
67 lines
2.0 KiB
TypeScript
import { createCounter } from "@scandic-hotels/common/telemetry"
|
|
|
|
import { router } from "../../.."
|
|
import { notFound } from "../../../errors"
|
|
import { GetHotelPage } from "../../../graphql/Query/HotelPage/HotelPage.graphql"
|
|
import { request } from "../../../graphql/request"
|
|
import { contentstackExtendedProcedureUID } from "../../../procedures"
|
|
import { generateTag } from "../../../utils/generateTag"
|
|
import { getCampaignPagesByHotelPageUid } from "../campaignPage/utils"
|
|
import { hotelPageSchema } from "./output"
|
|
import { getSortedCampaigns } from "./utils"
|
|
|
|
import type { GetHotelPageData } from "../../../types/hotelPage"
|
|
|
|
export const hotelPageQueryRouter = router({
|
|
get: contentstackExtendedProcedureUID.query(async ({ ctx }) => {
|
|
const { lang, uid } = ctx
|
|
|
|
const getHotelPageCounter = createCounter("trpc.contentstack.hotelPage.get")
|
|
const metricsGetHotelPage = getHotelPageCounter.init({ lang, uid })
|
|
|
|
metricsGetHotelPage.start()
|
|
|
|
const hotelPageResponse = await request<GetHotelPageData>(
|
|
GetHotelPage,
|
|
{
|
|
locale: lang,
|
|
uid,
|
|
},
|
|
{
|
|
key: generateTag(lang, uid),
|
|
ttl: "max",
|
|
}
|
|
)
|
|
|
|
if (!hotelPageResponse.data) {
|
|
const notFoundError = notFound(hotelPageResponse)
|
|
metricsGetHotelPage.noDataError()
|
|
throw notFoundError
|
|
}
|
|
|
|
const validatedHotelPage = hotelPageSchema.safeParse(hotelPageResponse.data)
|
|
|
|
if (!validatedHotelPage.success) {
|
|
metricsGetHotelPage.validationError(validatedHotelPage.error)
|
|
return null
|
|
}
|
|
|
|
metricsGetHotelPage.success()
|
|
|
|
const hotelCampaigns = await getCampaignPagesByHotelPageUid(uid, lang)
|
|
|
|
const hotelPage = validatedHotelPage.data.hotel_page
|
|
const { prioritizedCampaigns, ...campaignsBlockContent } =
|
|
hotelPage.campaigns
|
|
return {
|
|
...hotelPage,
|
|
campaignsBlock: hotelCampaigns?.length
|
|
? {
|
|
...campaignsBlockContent,
|
|
campaigns: getSortedCampaigns(prioritizedCampaigns, hotelCampaigns),
|
|
}
|
|
: null,
|
|
}
|
|
}),
|
|
})
|