feat(SW-159): merged contentstack and api requests for hotel query
This commit is contained in:
@@ -1,10 +1,24 @@
|
||||
import { metrics } from "@opentelemetry/api"
|
||||
|
||||
import * as api from "@/lib/api"
|
||||
import { badRequestError } from "@/server/errors/trpc"
|
||||
import { publicProcedure, router, serviceProcedure } from "@/server/trpc"
|
||||
import { GetHotelPage } from "@/lib/graphql/Query/HotelPage.graphql"
|
||||
import { request } from "@/lib/graphql/request"
|
||||
import {
|
||||
badRequestError,
|
||||
notFound,
|
||||
serverErrorByStatus,
|
||||
} from "@/server/errors/trpc"
|
||||
import {
|
||||
contentStackUidWithServiceProcedure,
|
||||
publicProcedure,
|
||||
router,
|
||||
} from "@/server/trpc"
|
||||
import { toApiLang } from "@/server/utils"
|
||||
|
||||
import {
|
||||
HotelPageDataRaw,
|
||||
validateHotelPageSchema,
|
||||
} from "../contentstack/hotelPage/output"
|
||||
import {
|
||||
getFiltersInputSchema,
|
||||
getHotelInputSchema,
|
||||
@@ -24,23 +38,54 @@ const getHotelCounter = meter.createCounter("trpc.hotel.get")
|
||||
const getHotelSuccessCounter = meter.createCounter("trpc.hotel.get-success")
|
||||
const getHotelFailCounter = meter.createCounter("trpc.hotel.get-fail")
|
||||
|
||||
const getHotelId = async (locale: string, uid: string | null | undefined) => {
|
||||
const rawContentStackData = await request<HotelPageDataRaw>(GetHotelPage, {
|
||||
locale,
|
||||
uid,
|
||||
})
|
||||
|
||||
if (!rawContentStackData.data) {
|
||||
throw notFound(rawContentStackData)
|
||||
}
|
||||
|
||||
const hotelPageData = validateHotelPageSchema.safeParse(
|
||||
rawContentStackData.data
|
||||
)
|
||||
|
||||
if (!hotelPageData.success) {
|
||||
console.error(
|
||||
`Failed to validate Hotel Page - (uid: ${uid}, lang: ${locale})`
|
||||
)
|
||||
console.error(hotelPageData.error)
|
||||
return null
|
||||
}
|
||||
|
||||
return hotelPageData.data.hotel_page.hotel_page_id
|
||||
}
|
||||
|
||||
export const hotelQueryRouter = router({
|
||||
get: serviceProcedure
|
||||
get: contentStackUidWithServiceProcedure
|
||||
.input(getHotelInputSchema)
|
||||
.query(async ({ ctx, input }) => {
|
||||
const { hotelId, language, include } = input
|
||||
getHotelCounter.add(1, { hotelId, language, include })
|
||||
const { lang, uid } = ctx
|
||||
const { include } = input
|
||||
const hotelId = await getHotelId(lang, uid)
|
||||
|
||||
const apiLang = toApiLang(language)
|
||||
if (!hotelId) {
|
||||
throw notFound(`Hotel not found for uid: ${uid}`)
|
||||
}
|
||||
|
||||
const apiLang = toApiLang(lang)
|
||||
const params: Record<string, string> = {
|
||||
hotelId,
|
||||
language: apiLang,
|
||||
}
|
||||
|
||||
if (include) {
|
||||
params.include = include.join(",")
|
||||
}
|
||||
|
||||
getHotelCounter.add(1, { hotelId, language, include })
|
||||
getHotelCounter.add(1, { hotelId, lang, include })
|
||||
console.info(
|
||||
"api.hotels.hotel start",
|
||||
JSON.stringify({
|
||||
@@ -62,7 +107,7 @@ export const hotelQueryRouter = router({
|
||||
const text = await apiResponse.text()
|
||||
getHotelFailCounter.add(1, {
|
||||
hotelId,
|
||||
language,
|
||||
lang,
|
||||
include,
|
||||
error_type: "http_error",
|
||||
error: JSON.stringify({
|
||||
@@ -82,7 +127,7 @@ export const hotelQueryRouter = router({
|
||||
},
|
||||
})
|
||||
)
|
||||
return null
|
||||
throw serverErrorByStatus(apiResponse.status, apiResponse)
|
||||
}
|
||||
const apiJson = await apiResponse.json()
|
||||
const validatedHotelData = getHotelDataSchema.safeParse(apiJson)
|
||||
@@ -90,7 +135,7 @@ export const hotelQueryRouter = router({
|
||||
if (!validatedHotelData.success) {
|
||||
getHotelFailCounter.add(1, {
|
||||
hotelId,
|
||||
language,
|
||||
lang,
|
||||
include,
|
||||
error_type: "validation_error",
|
||||
error: JSON.stringify(validatedHotelData.error),
|
||||
@@ -116,7 +161,7 @@ export const hotelQueryRouter = router({
|
||||
if (!validatedRoom.success) {
|
||||
getHotelFailCounter.add(1, {
|
||||
hotelId,
|
||||
language,
|
||||
lang,
|
||||
include,
|
||||
error_type: "validation_error",
|
||||
error: JSON.stringify(
|
||||
@@ -140,7 +185,7 @@ export const hotelQueryRouter = router({
|
||||
})
|
||||
: []
|
||||
|
||||
getHotelSuccessCounter.add(1, { hotelId, language, include })
|
||||
getHotelSuccessCounter.add(1, { hotelId, lang, include })
|
||||
console.info(
|
||||
"api.hotels.hotel success",
|
||||
JSON.stringify({
|
||||
|
||||
Reference in New Issue
Block a user