feat(SW-159): merged contentstack and api requests for hotel query
This commit is contained in:
@@ -1,7 +1,5 @@
|
|||||||
import { serverClient } from "@/lib/trpc/server"
|
import { serverClient } from "@/lib/trpc/server"
|
||||||
|
|
||||||
import { getLang } from "@/i18n/serverContext"
|
|
||||||
|
|
||||||
import { MOCK_FACILITIES } from "./Facilities/mockData"
|
import { MOCK_FACILITIES } from "./Facilities/mockData"
|
||||||
import AmenitiesList from "./AmenitiesList"
|
import AmenitiesList from "./AmenitiesList"
|
||||||
import Facilities from "./Facilities"
|
import Facilities from "./Facilities"
|
||||||
@@ -13,17 +11,7 @@ import TabNavigation from "./TabNavigation"
|
|||||||
import styles from "./hotelPage.module.css"
|
import styles from "./hotelPage.module.css"
|
||||||
|
|
||||||
export default async function HotelPage() {
|
export default async function HotelPage() {
|
||||||
const hotelPageIdentifierData =
|
|
||||||
await serverClient().contentstack.hotelPage.get()
|
|
||||||
|
|
||||||
if (!hotelPageIdentifierData) {
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
const lang = getLang()
|
|
||||||
|
|
||||||
const hotelData = await serverClient().hotel.get({
|
const hotelData = await serverClient().hotel.get({
|
||||||
hotelId: hotelPageIdentifierData.hotel_page_id,
|
|
||||||
language: lang,
|
|
||||||
include: ["RoomCategories"],
|
include: ["RoomCategories"],
|
||||||
})
|
})
|
||||||
if (!hotelData) {
|
if (!hotelData) {
|
||||||
|
|||||||
@@ -59,3 +59,17 @@ export function publicUnauthorizedError() {
|
|||||||
cause: new PublicUnauthorizedError(PUBLIC_UNAUTHORIZED),
|
cause: new PublicUnauthorizedError(PUBLIC_UNAUTHORIZED),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function serverErrorByStatus(status: number, cause?: unknown) {
|
||||||
|
switch (status) {
|
||||||
|
case 401:
|
||||||
|
return unauthorizedError(cause)
|
||||||
|
case 403:
|
||||||
|
return forbiddenError(cause)
|
||||||
|
case 404:
|
||||||
|
return notFound(cause)
|
||||||
|
case 500:
|
||||||
|
default:
|
||||||
|
return internalServerError(cause)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,10 +1,6 @@
|
|||||||
import { z } from "zod"
|
import { z } from "zod"
|
||||||
|
|
||||||
import { Lang } from "@/constants/languages"
|
|
||||||
|
|
||||||
export const getHotelInputSchema = z.object({
|
export const getHotelInputSchema = z.object({
|
||||||
hotelId: z.string(),
|
|
||||||
language: z.nativeEnum(Lang),
|
|
||||||
include: z
|
include: z
|
||||||
.array(z.enum(["RoomCategories", "NearbyHotels", "Restaurants", "City"]))
|
.array(z.enum(["RoomCategories", "NearbyHotels", "Restaurants", "City"]))
|
||||||
.optional(),
|
.optional(),
|
||||||
|
|||||||
@@ -233,7 +233,7 @@ const parkingPricingSchema = z.object({
|
|||||||
localCurrency: z.object({
|
localCurrency: z.object({
|
||||||
currency: z.string(),
|
currency: z.string(),
|
||||||
range: z.object({
|
range: z.object({
|
||||||
min: z.number(),
|
min: z.number().optional(),
|
||||||
max: z.number().optional(),
|
max: z.number().optional(),
|
||||||
}),
|
}),
|
||||||
ordinary: z.array(
|
ordinary: z.array(
|
||||||
@@ -284,8 +284,8 @@ const parkingSchema = z.object({
|
|||||||
type: z.string(),
|
type: z.string(),
|
||||||
name: z.string(),
|
name: z.string(),
|
||||||
address: z.string(),
|
address: z.string(),
|
||||||
numberOfParkingSpots: z.number(),
|
numberOfParkingSpots: z.number().optional(),
|
||||||
numberOfChargingSpaces: z.number(),
|
numberOfChargingSpaces: z.number().optional(),
|
||||||
distanceToHotel: z.number(),
|
distanceToHotel: z.number(),
|
||||||
canMakeReservation: z.boolean(),
|
canMakeReservation: z.boolean(),
|
||||||
pricing: parkingPricingSchema,
|
pricing: parkingPricingSchema,
|
||||||
|
|||||||
@@ -1,10 +1,24 @@
|
|||||||
import { metrics } from "@opentelemetry/api"
|
import { metrics } from "@opentelemetry/api"
|
||||||
|
|
||||||
import * as api from "@/lib/api"
|
import * as api from "@/lib/api"
|
||||||
import { badRequestError } from "@/server/errors/trpc"
|
import { GetHotelPage } from "@/lib/graphql/Query/HotelPage.graphql"
|
||||||
import { publicProcedure, router, serviceProcedure } from "@/server/trpc"
|
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 { toApiLang } from "@/server/utils"
|
||||||
|
|
||||||
|
import {
|
||||||
|
HotelPageDataRaw,
|
||||||
|
validateHotelPageSchema,
|
||||||
|
} from "../contentstack/hotelPage/output"
|
||||||
import {
|
import {
|
||||||
getFiltersInputSchema,
|
getFiltersInputSchema,
|
||||||
getHotelInputSchema,
|
getHotelInputSchema,
|
||||||
@@ -24,23 +38,54 @@ const getHotelCounter = meter.createCounter("trpc.hotel.get")
|
|||||||
const getHotelSuccessCounter = meter.createCounter("trpc.hotel.get-success")
|
const getHotelSuccessCounter = meter.createCounter("trpc.hotel.get-success")
|
||||||
const getHotelFailCounter = meter.createCounter("trpc.hotel.get-fail")
|
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({
|
export const hotelQueryRouter = router({
|
||||||
get: serviceProcedure
|
get: contentStackUidWithServiceProcedure
|
||||||
.input(getHotelInputSchema)
|
.input(getHotelInputSchema)
|
||||||
.query(async ({ ctx, input }) => {
|
.query(async ({ ctx, input }) => {
|
||||||
const { hotelId, language, include } = input
|
const { lang, uid } = ctx
|
||||||
getHotelCounter.add(1, { hotelId, language, include })
|
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> = {
|
const params: Record<string, string> = {
|
||||||
hotelId,
|
hotelId,
|
||||||
language: apiLang,
|
language: apiLang,
|
||||||
}
|
}
|
||||||
|
|
||||||
if (include) {
|
if (include) {
|
||||||
params.include = include.join(",")
|
params.include = include.join(",")
|
||||||
}
|
}
|
||||||
|
|
||||||
getHotelCounter.add(1, { hotelId, language, include })
|
getHotelCounter.add(1, { hotelId, lang, include })
|
||||||
console.info(
|
console.info(
|
||||||
"api.hotels.hotel start",
|
"api.hotels.hotel start",
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
@@ -62,7 +107,7 @@ export const hotelQueryRouter = router({
|
|||||||
const text = await apiResponse.text()
|
const text = await apiResponse.text()
|
||||||
getHotelFailCounter.add(1, {
|
getHotelFailCounter.add(1, {
|
||||||
hotelId,
|
hotelId,
|
||||||
language,
|
lang,
|
||||||
include,
|
include,
|
||||||
error_type: "http_error",
|
error_type: "http_error",
|
||||||
error: JSON.stringify({
|
error: JSON.stringify({
|
||||||
@@ -82,7 +127,7 @@ export const hotelQueryRouter = router({
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
return null
|
throw serverErrorByStatus(apiResponse.status, apiResponse)
|
||||||
}
|
}
|
||||||
const apiJson = await apiResponse.json()
|
const apiJson = await apiResponse.json()
|
||||||
const validatedHotelData = getHotelDataSchema.safeParse(apiJson)
|
const validatedHotelData = getHotelDataSchema.safeParse(apiJson)
|
||||||
@@ -90,7 +135,7 @@ export const hotelQueryRouter = router({
|
|||||||
if (!validatedHotelData.success) {
|
if (!validatedHotelData.success) {
|
||||||
getHotelFailCounter.add(1, {
|
getHotelFailCounter.add(1, {
|
||||||
hotelId,
|
hotelId,
|
||||||
language,
|
lang,
|
||||||
include,
|
include,
|
||||||
error_type: "validation_error",
|
error_type: "validation_error",
|
||||||
error: JSON.stringify(validatedHotelData.error),
|
error: JSON.stringify(validatedHotelData.error),
|
||||||
@@ -116,7 +161,7 @@ export const hotelQueryRouter = router({
|
|||||||
if (!validatedRoom.success) {
|
if (!validatedRoom.success) {
|
||||||
getHotelFailCounter.add(1, {
|
getHotelFailCounter.add(1, {
|
||||||
hotelId,
|
hotelId,
|
||||||
language,
|
lang,
|
||||||
include,
|
include,
|
||||||
error_type: "validation_error",
|
error_type: "validation_error",
|
||||||
error: JSON.stringify(
|
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(
|
console.info(
|
||||||
"api.hotels.hotel success",
|
"api.hotels.hotel success",
|
||||||
JSON.stringify({
|
JSON.stringify({
|
||||||
|
|||||||
@@ -4,13 +4,13 @@ import { ZodError } from "zod"
|
|||||||
|
|
||||||
import { env } from "@/env/server"
|
import { env } from "@/env/server"
|
||||||
|
|
||||||
|
import { type Context, createContext } from "./context"
|
||||||
import {
|
import {
|
||||||
badRequestError,
|
badRequestError,
|
||||||
internalServerError,
|
internalServerError,
|
||||||
sessionExpiredError,
|
sessionExpiredError,
|
||||||
unauthorizedError,
|
unauthorizedError,
|
||||||
} from "./errors/trpc"
|
} from "./errors/trpc"
|
||||||
import { type Context, createContext } from "./context"
|
|
||||||
import { fetchServiceToken } from "./tokenManager"
|
import { fetchServiceToken } from "./tokenManager"
|
||||||
import { transformer } from "./transformer"
|
import { transformer } from "./transformer"
|
||||||
|
|
||||||
@@ -146,3 +146,8 @@ export const protectedServerActionProcedure = serverActionProcedure.use(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// NOTE: This is actually save to use, just the implementation could change
|
||||||
|
// in minor version bumps. Please read: https://trpc.io/docs/faq#unstable
|
||||||
|
export const contentStackUidWithServiceProcedure =
|
||||||
|
contentstackExtendedProcedureUID.unstable_concat(serviceProcedure)
|
||||||
|
|||||||
Reference in New Issue
Block a user