279 lines
8.0 KiB
TypeScript
279 lines
8.0 KiB
TypeScript
import { metrics } from "@opentelemetry/api"
|
|
|
|
import * as api from "@/lib/api"
|
|
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 { extractHotelImages } from "@/server/routers/utils/hotels"
|
|
import { toApiLang } from "@/server/utils"
|
|
|
|
import {
|
|
HotelPageDataRaw,
|
|
validateHotelPageSchema,
|
|
} from "../contentstack/hotelPage/output"
|
|
import {
|
|
getAvailabilityInputSchema,
|
|
getFiltersInputSchema,
|
|
getHotelInputSchema,
|
|
getRatesInputSchema,
|
|
} from "./input"
|
|
import {
|
|
getAvailabilitySchema,
|
|
getFiltersSchema,
|
|
getHotelDataSchema,
|
|
getRatesSchema,
|
|
roomSchema,
|
|
} from "./output"
|
|
import tempFilterData from "./tempFilterData.json"
|
|
import tempRatesData from "./tempRatesData.json"
|
|
|
|
const meter = metrics.getMeter("trpc.hotels")
|
|
const getHotelCounter = meter.createCounter("trpc.hotel.get")
|
|
const getHotelSuccessCounter = meter.createCounter("trpc.hotel.get-success")
|
|
const getHotelFailCounter = meter.createCounter("trpc.hotel.get-fail")
|
|
|
|
async function getHotelId(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: contentStackUidWithServiceProcedure
|
|
.input(getHotelInputSchema)
|
|
.query(async ({ ctx, input }) => {
|
|
const { lang, uid } = ctx
|
|
const { include } = input
|
|
const hotelId = await getHotelId(lang, uid)
|
|
|
|
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, lang, include })
|
|
console.info(
|
|
"api.hotels.hotel start",
|
|
JSON.stringify({
|
|
query: { hotelId, params },
|
|
})
|
|
)
|
|
const apiResponse = await api.get(
|
|
`${api.endpoints.v1.hotels}/${hotelId}`,
|
|
{
|
|
cache: "no-store",
|
|
headers: {
|
|
Authorization: `Bearer ${ctx.serviceToken}`,
|
|
},
|
|
},
|
|
params
|
|
)
|
|
|
|
if (!apiResponse.ok) {
|
|
const text = await apiResponse.text()
|
|
getHotelFailCounter.add(1, {
|
|
hotelId,
|
|
lang,
|
|
include,
|
|
error_type: "http_error",
|
|
error: JSON.stringify({
|
|
status: apiResponse.status,
|
|
statusText: apiResponse.statusText,
|
|
text,
|
|
}),
|
|
})
|
|
console.error(
|
|
"api.hotels.hotel error",
|
|
JSON.stringify({
|
|
query: { hotelId, params },
|
|
error: {
|
|
status: apiResponse.status,
|
|
statusText: apiResponse.statusText,
|
|
text,
|
|
},
|
|
})
|
|
)
|
|
throw serverErrorByStatus(apiResponse.status, apiResponse)
|
|
}
|
|
const apiJson = await apiResponse.json()
|
|
const validatedHotelData = getHotelDataSchema.safeParse(apiJson)
|
|
|
|
if (!validatedHotelData.success) {
|
|
getHotelFailCounter.add(1, {
|
|
hotelId,
|
|
lang,
|
|
include,
|
|
error_type: "validation_error",
|
|
error: JSON.stringify(validatedHotelData.error),
|
|
})
|
|
|
|
console.error(
|
|
"api.hotels.hotel validation error",
|
|
JSON.stringify({
|
|
query: { hotelId, params },
|
|
error: validatedHotelData.error,
|
|
})
|
|
)
|
|
throw badRequestError()
|
|
}
|
|
|
|
const included = validatedHotelData.data.included || []
|
|
|
|
const hotelAttributes = validatedHotelData.data.data.attributes
|
|
|
|
const images = extractHotelImages(hotelAttributes)
|
|
|
|
const roomCategories = included
|
|
? included
|
|
.filter((item) => item.type === "roomcategories")
|
|
.map((roomCategory) => {
|
|
const validatedRoom = roomSchema.safeParse(roomCategory)
|
|
if (!validatedRoom.success) {
|
|
getHotelFailCounter.add(1, {
|
|
hotelId,
|
|
lang,
|
|
include,
|
|
error_type: "validation_error",
|
|
error: JSON.stringify(
|
|
validatedRoom.error.issues.map(({ code, message }) => ({
|
|
code,
|
|
message,
|
|
}))
|
|
),
|
|
})
|
|
console.error(
|
|
"api.hotels.hotel validation error",
|
|
JSON.stringify({
|
|
query: { hotelId, params },
|
|
error: validatedRoom.error,
|
|
})
|
|
)
|
|
throw badRequestError()
|
|
}
|
|
|
|
return validatedRoom.data
|
|
})
|
|
: []
|
|
|
|
getHotelSuccessCounter.add(1, { hotelId, lang, include })
|
|
console.info(
|
|
"api.hotels.hotel success",
|
|
JSON.stringify({
|
|
query: { hotelId, params: params },
|
|
})
|
|
)
|
|
return {
|
|
hotelName: hotelAttributes.name,
|
|
hotelDescription: hotelAttributes.hotelContent.texts.descriptions.short,
|
|
hotelLocation: hotelAttributes.location,
|
|
hotelAddress: hotelAttributes.address,
|
|
hotelRatings: hotelAttributes.ratings,
|
|
hotelDetailedFacilities: hotelAttributes.detailedFacilities,
|
|
hotelImages: images,
|
|
roomCategories,
|
|
}
|
|
}),
|
|
rates: router({
|
|
get: publicProcedure
|
|
.input(getRatesInputSchema)
|
|
.query(async ({ input, ctx }) => {
|
|
// TODO: Do a real API call when the endpoint is ready
|
|
// const { hotelId } = input
|
|
|
|
// const params = new URLSearchParams()
|
|
// const apiLang = toApiLang(language)
|
|
// params.set("hotelId", hotelId.toString())
|
|
// params.set("language", apiLang)
|
|
|
|
console.info("api.hotels.rates start", JSON.stringify({}))
|
|
const validatedHotelData = getRatesSchema.safeParse(tempRatesData)
|
|
|
|
if (!tempRatesData) {
|
|
console.error(
|
|
"api.hotels.rates error",
|
|
JSON.stringify({ error: null })
|
|
)
|
|
//Can't return null here since consuming component does not handle null yet
|
|
// return null
|
|
}
|
|
if (!validatedHotelData.success) {
|
|
console.error(
|
|
"api.hotels.rates validation error",
|
|
JSON.stringify({
|
|
error: validatedHotelData.error,
|
|
})
|
|
)
|
|
throw badRequestError()
|
|
}
|
|
console.info("api.hotels.rates success", JSON.stringify({}))
|
|
return validatedHotelData.data
|
|
}),
|
|
}),
|
|
filters: router({
|
|
get: publicProcedure
|
|
.input(getFiltersInputSchema)
|
|
.query(async ({ input, ctx }) => {
|
|
console.info("api.hotels.filters start", JSON.stringify({}))
|
|
|
|
if (!tempFilterData) {
|
|
console.error(
|
|
"api.hotels.filters error",
|
|
JSON.stringify({ error: null })
|
|
)
|
|
//Can't return null here since consuming component does not handle null yet
|
|
// return null
|
|
}
|
|
const validateFilterData = getFiltersSchema.safeParse(tempFilterData)
|
|
|
|
if (!validateFilterData.success) {
|
|
console.error(
|
|
"api.hotels.filters validation error",
|
|
JSON.stringify({
|
|
error: validateFilterData.error,
|
|
})
|
|
)
|
|
throw badRequestError()
|
|
}
|
|
console.info("api.hotels.rates success", JSON.stringify({}))
|
|
return validateFilterData.data
|
|
}),
|
|
}),
|
|
})
|