214 lines
6.3 KiB
TypeScript
214 lines
6.3 KiB
TypeScript
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 { toApiLang } from "@/server/utils"
|
|
|
|
import {
|
|
getFiltersInputSchema,
|
|
getHotelInputSchema,
|
|
getRatesInputSchema,
|
|
} from "./input"
|
|
import {
|
|
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")
|
|
|
|
export const hotelQueryRouter = router({
|
|
getHotel: serviceProcedure
|
|
.input(getHotelInputSchema)
|
|
.query(async ({ ctx, input }) => {
|
|
const { hotelId, language, include } = input
|
|
getHotelCounter.add(1, { hotelId, language, include })
|
|
|
|
const apiLang = toApiLang(language)
|
|
const params: Record<string, string> = {
|
|
hotelId,
|
|
language: apiLang,
|
|
}
|
|
if (include) {
|
|
params.include = include.join(",")
|
|
}
|
|
|
|
getHotelCounter.add(1, { hotelId, language, 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,
|
|
language,
|
|
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,
|
|
},
|
|
})
|
|
)
|
|
return null
|
|
}
|
|
const apiJson = await apiResponse.json()
|
|
const validatedHotelData = getHotelDataSchema.safeParse(apiJson)
|
|
|
|
if (!validatedHotelData.success) {
|
|
getHotelFailCounter.add(1, {
|
|
hotelId,
|
|
language,
|
|
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 roomCategories = included
|
|
? included
|
|
.filter((item) => item.type === "roomcategories")
|
|
.map((roomCategory) => {
|
|
const validatedRoom = roomSchema.safeParse(roomCategory)
|
|
if (!validatedRoom.success) {
|
|
getHotelFailCounter.add(1, {
|
|
hotelId,
|
|
language,
|
|
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, language, include })
|
|
console.info(
|
|
"api.hotels.hotel success",
|
|
JSON.stringify({
|
|
query: { hotelId, params: params },
|
|
})
|
|
)
|
|
return {
|
|
hotel: validatedHotelData.data.data.attributes,
|
|
roomCategories: roomCategories,
|
|
}
|
|
}),
|
|
getRates: 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
|
|
}),
|
|
getFilters: 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
|
|
}),
|
|
})
|