refactor: add hotel scope to service token fetch & enable hotel API
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
// import * as api from "@/lib/api"
|
import * as api from "@/lib/api"
|
||||||
import { badRequestError } from "@/server/errors/trpc"
|
import { badRequestError } from "@/server/errors/trpc"
|
||||||
import { publicProcedure, router, serviceProcedure } from "@/server/trpc"
|
import { publicProcedure, router, serviceProcedure } from "@/server/trpc"
|
||||||
import { toApiLang } from "@/server/utils"
|
import { toApiLang } from "@/server/utils"
|
||||||
@@ -15,7 +15,6 @@ import {
|
|||||||
RoomSchema,
|
RoomSchema,
|
||||||
} from "./output"
|
} from "./output"
|
||||||
import tempFilterData from "./tempFilterData.json"
|
import tempFilterData from "./tempFilterData.json"
|
||||||
import tempHotelData from "./tempHotelData.json"
|
|
||||||
import tempRatesData from "./tempRatesData.json"
|
import tempRatesData from "./tempRatesData.json"
|
||||||
|
|
||||||
export const hotelQueryRouter = router({
|
export const hotelQueryRouter = router({
|
||||||
@@ -33,39 +32,32 @@ export const hotelQueryRouter = router({
|
|||||||
params.set("include", include.join(","))
|
params.set("include", include.join(","))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Enable once Hotel API has support for service token.
|
const apiResponse = await api.get(
|
||||||
// const apiResponse = await api.get(
|
`${api.endpoints.v1.hotels}/${hotelId}`,
|
||||||
// `${api.endpoints.v1.hotels}/${hotelId}`,
|
{
|
||||||
// {
|
cache: "no-store",
|
||||||
// cache: "no-store",
|
headers: {
|
||||||
// headers: {
|
Authorization: `Bearer ${ctx.serviceToken}`,
|
||||||
// Authorization: `Bearer ${ctx.serviceToken}`,
|
},
|
||||||
// },
|
},
|
||||||
// },
|
params
|
||||||
// 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
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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) {
|
if (!validatedHotelData.success) {
|
||||||
console.error(`Get Individual Hotel Data - Verified Data Error`)
|
console.error(`Get Individual Hotel Data - Verified Data Error`)
|
||||||
console.error(validatedHotelData.error)
|
console.error(validatedHotelData.error)
|
||||||
throw badRequestError()
|
throw badRequestError()
|
||||||
}
|
}
|
||||||
|
|
||||||
// const included = validatedHotelData.data.included || []
|
const included = validatedHotelData.data.included || []
|
||||||
|
|
||||||
const roomCategories = included
|
const roomCategories = included
|
||||||
? included
|
? included
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -16,6 +16,7 @@ export async function fetchServiceToken(): Promise<ServiceTokenResponse> {
|
|||||||
grant_type: "client_credentials",
|
grant_type: "client_credentials",
|
||||||
client_id: env.CURITY_CLIENT_ID_SERVICE,
|
client_id: env.CURITY_CLIENT_ID_SERVICE,
|
||||||
client_secret: env.CURITY_CLIENT_SECRET_SERVICE,
|
client_secret: env.CURITY_CLIENT_SECRET_SERVICE,
|
||||||
|
scope: ["hotel"].join(","),
|
||||||
}),
|
}),
|
||||||
next: {
|
next: {
|
||||||
revalidate: SERVICE_TOKEN_REVALIDATE_SECONDS,
|
revalidate: SERVICE_TOKEN_REVALIDATE_SECONDS,
|
||||||
|
|||||||
@@ -7,8 +7,7 @@ export const langInput = z.object({
|
|||||||
})
|
})
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper function to convert Lang enum to uppercase
|
* Helper function to convert Lang enum to API lang enum.
|
||||||
* Needed for the Hotel endpoint.
|
|
||||||
*/
|
*/
|
||||||
export const toApiLang = (lang: Lang): string => {
|
export const toApiLang = (lang: Lang): string => {
|
||||||
const result = toApiLangMap[lang]
|
const result = toApiLangMap[lang]
|
||||||
|
|||||||
Reference in New Issue
Block a user