refactor: add hotel scope to service token fetch & enable hotel API

This commit is contained in:
Chuma McPhoy
2024-08-19 09:38:06 +02:00
parent 2407f40b75
commit aa38e82698
4 changed files with 21 additions and 2548 deletions

View File

@@ -1,4 +1,4 @@
// import * as api from "@/lib/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"
@@ -15,7 +15,6 @@ import {
RoomSchema,
} from "./output"
import tempFilterData from "./tempFilterData.json"
import tempHotelData from "./tempHotelData.json"
import tempRatesData from "./tempRatesData.json"
export const hotelQueryRouter = router({
@@ -33,39 +32,32 @@ export const hotelQueryRouter = router({
params.set("include", include.join(","))
}
// Enable once Hotel API has support for service token.
// const apiResponse = await api.get(
// `${api.endpoints.v1.hotels}/${hotelId}`,
// {
// cache: "no-store",
// headers: {
// Authorization: `Bearer ${ctx.serviceToken}`,
// },
// },
// params
// )
// if (!apiResponse.ok) {
// console.info(`API Response Failed - Getting Hotel`)
// console.error(apiResponse)
// return null
// }
// const apiJson = await apiResponse.json()
// const validatedHotelData = getHotelDataSchema.safeParse(apiJson)
const { included, ...apiJsonWithoutIncluded } = tempHotelData
const validatedHotelData = getHotelDataSchema.safeParse(
apiJsonWithoutIncluded
const apiResponse = await api.get(
`${api.endpoints.v1.hotels}/${hotelId}`,
{
cache: "no-store",
headers: {
Authorization: `Bearer ${ctx.serviceToken}`,
},
},
params
)
if (!apiResponse.ok) {
console.info(`API Response Failed - Getting Hotel`)
console.error(apiResponse)
return null
}
const apiJson = await apiResponse.json()
const validatedHotelData = getHotelDataSchema.safeParse(apiJson)
if (!validatedHotelData.success) {
console.error(`Get Individual Hotel Data - Verified Data Error`)
console.error(validatedHotelData.error)
throw badRequestError()
}
// const included = validatedHotelData.data.included || []
const included = validatedHotelData.data.included || []
const roomCategories = included
? included

File diff suppressed because it is too large Load Diff

View File

@@ -16,6 +16,7 @@ export async function fetchServiceToken(): Promise<ServiceTokenResponse> {
grant_type: "client_credentials",
client_id: env.CURITY_CLIENT_ID_SERVICE,
client_secret: env.CURITY_CLIENT_SECRET_SERVICE,
scope: ["hotel"].join(","),
}),
next: {
revalidate: SERVICE_TOKEN_REVALIDATE_SECONDS,

View File

@@ -7,8 +7,7 @@ export const langInput = z.object({
})
/**
* Helper function to convert Lang enum to uppercase
* Needed for the Hotel endpoint.
* Helper function to convert Lang enum to API lang enum.
*/
export const toApiLang = (lang: Lang): string => {
const result = toApiLangMap[lang]