Merged in feat/SW-1333-hotel-endpoint (pull request #1206)
Feat(SW-133): Add additionalData endpoint Approved-by: Erik Tiekstra Approved-by: Fredrik Thorsson
This commit is contained in:
@@ -146,7 +146,7 @@ export const bookingQueryRouter = router({
|
||||
included: hotelData.included,
|
||||
},
|
||||
room: getBookedHotelRoom(
|
||||
hotelData.included?.rooms,
|
||||
hotelData.included.rooms,
|
||||
booking.data.roomTypeCode
|
||||
),
|
||||
}
|
||||
|
||||
@@ -98,3 +98,8 @@ export const getMeetingRoomsInputSchema = z.object({
|
||||
hotelId: z.string(),
|
||||
language: z.string(),
|
||||
})
|
||||
|
||||
export const getAdditionalDataInputSchema = z.object({
|
||||
hotelId: z.string(),
|
||||
language: z.string(),
|
||||
})
|
||||
|
||||
@@ -3,6 +3,7 @@ import { z } from "zod"
|
||||
import { ChildBedTypeEnum, type PaymentMethodEnum } from "@/constants/booking"
|
||||
import { toLang } from "@/server/utils"
|
||||
|
||||
import { additionalDataSchema } from "./schemas/additionalData"
|
||||
import { imageSchema } from "./schemas/image"
|
||||
import { restaurantSchema } from "./schemas/restaurants"
|
||||
import { roomSchema } from "./schemas/room"
|
||||
@@ -12,7 +13,7 @@ import { getPoiGroupByCategoryName } from "./utils"
|
||||
import { RoomPackageCodeEnum } from "@/types/components/hotelReservation/selectRate/roomFilter"
|
||||
import { FacilityEnum } from "@/types/enums/facilities"
|
||||
import { PackageTypeEnum } from "@/types/enums/packages"
|
||||
import type { RestaurantData, RoomData } from "@/types/hotel"
|
||||
import type { AdditionalData, RestaurantData, RoomData } from "@/types/hotel"
|
||||
|
||||
const ratingsSchema = z
|
||||
.object({
|
||||
@@ -119,7 +120,7 @@ const hotelContentSchema = z.object({
|
||||
restaurantsOverviewPageLink: z.string().optional(),
|
||||
restaurantsContentDescriptionShort: z.string().optional(),
|
||||
restaurantsContentDescriptionMedium: z.string().optional(),
|
||||
}),
|
||||
}), // TODO remove, use new /additionalData endpoint
|
||||
})
|
||||
|
||||
const detailedFacilitySchema = z.object({
|
||||
@@ -134,12 +135,12 @@ const detailedFacilitySchema = z.object({
|
||||
export const facilitySchema = z.object({
|
||||
headingText: z.string().default(""),
|
||||
heroImages: z.array(imageSchema),
|
||||
})
|
||||
}) // TODO remove, use new /additionalData endpoint
|
||||
|
||||
export const gallerySchema = z.object({
|
||||
heroImages: z.array(imageSchema),
|
||||
smallerImages: z.array(imageSchema),
|
||||
})
|
||||
}) // TODO remove, use new /additionalData endpoint
|
||||
|
||||
const healthFacilitySchema = z.object({
|
||||
type: z.string(),
|
||||
@@ -289,16 +290,6 @@ export const parkingSchema = z.object({
|
||||
pricing: parkingPricingSchema,
|
||||
})
|
||||
|
||||
const specialNeedSchema = z.object({
|
||||
name: z.string(),
|
||||
details: z.string(),
|
||||
})
|
||||
|
||||
const specialNeedGroupSchema = z.object({
|
||||
name: z.string(),
|
||||
specialNeeds: z.array(specialNeedSchema),
|
||||
})
|
||||
|
||||
const socialMediaSchema = z.object({
|
||||
instagram: z.string().optional(),
|
||||
facebook: z.string().optional(),
|
||||
@@ -392,11 +383,10 @@ type DetailedFacility = { id: FacilityEnum } & z.infer<
|
||||
typeof detailedFacilitySchema
|
||||
>
|
||||
export const hotelAttributesSchema = z.object({
|
||||
accessibilityElevatorPitchText: z.string().optional(),
|
||||
address: addressSchema,
|
||||
cityId: z.string(),
|
||||
cityName: z.string(),
|
||||
conferencesAndMeetings: facilitySchema.optional(),
|
||||
conferencesAndMeetings: facilitySchema.optional(), // TODO remove, use new /additionalData endpoint
|
||||
contactInformation: contactInformationSchema,
|
||||
detailedFacilities: z.array(detailedFacilitySchema).transform(
|
||||
(facilities) =>
|
||||
@@ -407,13 +397,11 @@ export const hotelAttributesSchema = z.object({
|
||||
)
|
||||
.sort((a, b) => b.sortOrder - a.sortOrder) as DetailedFacility[]
|
||||
),
|
||||
gallery: gallerySchema.optional(),
|
||||
gallery: gallerySchema.optional(), // TODO remove, use new /additionalData endpoint
|
||||
galleryImages: z.array(imageSchema).optional(),
|
||||
healthAndWellness: facilitySchema.optional(),
|
||||
healthFacilities: z.array(healthFacilitySchema),
|
||||
hotelContent: hotelContentSchema,
|
||||
hotelFacts: hotelFactsSchema,
|
||||
hotelRoomElevatorPitchText: z.string().optional(),
|
||||
hotelType: z.string().optional(),
|
||||
isActive: z.boolean(),
|
||||
isPublished: z.boolean(),
|
||||
@@ -430,23 +418,26 @@ export const hotelAttributesSchema = z.object({
|
||||
),
|
||||
ratings: ratingsSchema,
|
||||
rewardNight: rewardNightSchema,
|
||||
restaurantImages: facilitySchema.optional(),
|
||||
restaurantImages: facilitySchema.optional(), // TODO remove, use new /additionalData endpoint
|
||||
socialMedia: socialMediaSchema,
|
||||
specialAlerts: specialAlertsSchema,
|
||||
specialNeedGroups: z.array(specialNeedGroupSchema),
|
||||
vat: z.number(),
|
||||
})
|
||||
|
||||
const includedSchema = z
|
||||
.array(z.union([roomSchema, restaurantSchema]))
|
||||
.array(z.union([roomSchema, restaurantSchema, additionalDataSchema]))
|
||||
.transform((data) => {
|
||||
const rooms = data.filter((d) => d.type === "roomcategories") as RoomData[]
|
||||
const restaurants = data.filter(
|
||||
(d) => d.type === "restaurants"
|
||||
) as RestaurantData[]
|
||||
const additionalData = data.filter(
|
||||
(d) => d.type === "additionalData"
|
||||
) as AdditionalData[]
|
||||
return {
|
||||
rooms,
|
||||
restaurants,
|
||||
additionalData,
|
||||
}
|
||||
})
|
||||
|
||||
@@ -467,7 +458,13 @@ export const getHotelDataSchema = z.object({
|
||||
}),
|
||||
// NOTE: We can pass an "include" param to the hotel API to retrieve
|
||||
// additional data for an individual hotel.
|
||||
included: includedSchema.optional(),
|
||||
included: includedSchema.optional().transform((incl) => {
|
||||
return {
|
||||
restaurants: incl?.restaurants,
|
||||
rooms: incl?.rooms,
|
||||
additionalData: incl?.additionalData[0],
|
||||
}
|
||||
}),
|
||||
})
|
||||
|
||||
export const childrenSchema = z.object({
|
||||
|
||||
@@ -16,7 +16,9 @@ import { cache } from "@/utils/cache"
|
||||
|
||||
import { getHotelPageUrl } from "../contentstack/hotelPage/utils"
|
||||
import { getVerifiedUser, parsedUser } from "../user/query"
|
||||
import { additionalDataSchema } from "./schemas/additionalData"
|
||||
import {
|
||||
getAdditionalDataInputSchema,
|
||||
getBreakfastPackageInputSchema,
|
||||
getCityCoordinatesInputSchema,
|
||||
getHotelDataInputSchema,
|
||||
@@ -39,6 +41,9 @@ import {
|
||||
getRoomsAvailabilitySchema,
|
||||
} from "./output"
|
||||
import {
|
||||
additionalDataCounter,
|
||||
additionalDataFailCounter,
|
||||
additionalDataSuccessCounter,
|
||||
breakfastPackagesCounter,
|
||||
breakfastPackagesFailCounter,
|
||||
breakfastPackagesSuccessCounter,
|
||||
@@ -86,7 +91,7 @@ export const getHotelData = cache(
|
||||
async (input: HotelDataInput, serviceToken: string) => {
|
||||
const { hotelId, language, isCardOnlyPayment } = input
|
||||
|
||||
const includes = ["RoomCategories", "Restaurants"] // "RoomCategories","NearbyHotels","Restaurants","City",
|
||||
const includes = ["RoomCategories", "Restaurants", "AdditionalData"] //"NearbyHotels","Restaurants","City",
|
||||
const params = new URLSearchParams({
|
||||
hotelId,
|
||||
language,
|
||||
@@ -182,8 +187,10 @@ export const getHotelData = cache(
|
||||
hotelData.data.attributes.merchantInformationData.alternatePaymentOptions =
|
||||
[]
|
||||
}
|
||||
if (hotelData.data.attributes.gallery) {
|
||||
const smallerImages = hotelData.data.attributes.gallery.smallerImages
|
||||
|
||||
const gallery = hotelData.included.additionalData?.gallery
|
||||
if (gallery) {
|
||||
const smallerImages = gallery.smallerImages
|
||||
const hotelGalleryImages =
|
||||
hotelData.data.attributes.hotelType === HotelTypeEnum.Signature
|
||||
? smallerImages.slice(0, 10)
|
||||
@@ -604,7 +611,7 @@ export const hotelQueryRouter = router({
|
||||
|
||||
const bedTypes = availableRoomsInCategory
|
||||
.map((availRoom) => {
|
||||
const matchingRoom = hotelData?.included?.rooms
|
||||
const matchingRoom = hotelData?.included.rooms
|
||||
?.find((room) =>
|
||||
room.roomTypes
|
||||
.map((roomType) => roomType.code)
|
||||
@@ -1253,4 +1260,82 @@ export const hotelQueryRouter = router({
|
||||
|
||||
return validatedMeetingRooms.data.data
|
||||
}),
|
||||
additionalData: safeProtectedServiceProcedure
|
||||
.input(getAdditionalDataInputSchema)
|
||||
.query(async function ({ ctx, input }) {
|
||||
const { hotelId, language } = input
|
||||
|
||||
const params: Record<string, string | string[]> = {
|
||||
hotelId,
|
||||
language,
|
||||
}
|
||||
const metricsData = { ...params, hotelId: input.hotelId }
|
||||
additionalDataCounter.add(1, metricsData)
|
||||
console.info(
|
||||
"api.hotels.additionalData start",
|
||||
JSON.stringify({ query: { hotelId, params } })
|
||||
)
|
||||
|
||||
const apiResponse = await api.get(
|
||||
api.endpoints.v1.Hotel.Hotels.additionalData(input.hotelId),
|
||||
{
|
||||
cache: undefined,
|
||||
headers: {
|
||||
Authorization: `Bearer ${ctx.serviceToken}`,
|
||||
},
|
||||
next: {
|
||||
revalidate: env.CACHE_TIME_HOTELS,
|
||||
},
|
||||
},
|
||||
params
|
||||
)
|
||||
|
||||
if (!apiResponse.ok) {
|
||||
const text = await apiResponse.text()
|
||||
additionalDataFailCounter.add(1, {
|
||||
...metricsData,
|
||||
error_type: "http_error",
|
||||
error: JSON.stringify({
|
||||
status: apiResponse.status,
|
||||
statusText: apiResponse.statusText,
|
||||
text,
|
||||
}),
|
||||
})
|
||||
console.error(
|
||||
"api.hotels.additionalData error",
|
||||
JSON.stringify({
|
||||
query: { params },
|
||||
error: {
|
||||
status: apiResponse.status,
|
||||
statusText: apiResponse.statusText,
|
||||
text,
|
||||
},
|
||||
})
|
||||
)
|
||||
return null
|
||||
}
|
||||
|
||||
const apiJson = await apiResponse.json()
|
||||
const validatedAdditionalData = additionalDataSchema.safeParse(apiJson)
|
||||
|
||||
if (!validatedAdditionalData.success) {
|
||||
console.error(
|
||||
"api.hotels.additionalData validation error",
|
||||
JSON.stringify({
|
||||
query: { params },
|
||||
error: validatedAdditionalData.error,
|
||||
})
|
||||
)
|
||||
throw badRequestError()
|
||||
}
|
||||
additionalDataSuccessCounter.add(1, {
|
||||
hotelId,
|
||||
})
|
||||
console.info(
|
||||
"api.hotels.additionalData success",
|
||||
JSON.stringify({ query: { params } })
|
||||
)
|
||||
|
||||
return validatedAdditionalData.data
|
||||
}),
|
||||
})
|
||||
|
||||
67
server/routers/hotels/schemas/additionalData.ts
Normal file
67
server/routers/hotels/schemas/additionalData.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { imageSchema } from "./image"
|
||||
|
||||
const specialNeedSchema = z.object({
|
||||
name: z.string(),
|
||||
details: z.string(),
|
||||
})
|
||||
|
||||
const specialNeedGroupSchema = z.object({
|
||||
name: z.string(),
|
||||
specialNeeds: z.array(specialNeedSchema),
|
||||
})
|
||||
|
||||
export const gallerySchema = z.object({
|
||||
heroImages: z.array(imageSchema),
|
||||
smallerImages: z.array(imageSchema),
|
||||
})
|
||||
|
||||
export const facilitySchema = z.object({
|
||||
headingText: z.string().default(""),
|
||||
heroImages: z.array(imageSchema),
|
||||
})
|
||||
|
||||
export const restaurantsOverviewPageSchema = z.object({
|
||||
restaurantsOverviewPageLinkText: z.string().optional(),
|
||||
restaurantsOverviewPageLink: z.string().optional(),
|
||||
restaurantsContentDescriptionShort: z.string().optional(),
|
||||
restaurantsContentDescriptionMedium: z.string().optional(),
|
||||
})
|
||||
|
||||
const extraPageSchema = z.object({
|
||||
elevatorPitch: z.string().optional(),
|
||||
mainBody: z.string().optional(),
|
||||
})
|
||||
|
||||
export const additionalDataSchema = z
|
||||
.object({
|
||||
attributes: z.object({
|
||||
name: z.string(),
|
||||
id: z.string(),
|
||||
displayWebPage: z.object({
|
||||
healthGym: z.boolean(),
|
||||
meetingRoom: z.boolean(),
|
||||
parking: z.boolean(),
|
||||
specialNeeds: z.boolean(),
|
||||
}),
|
||||
specialNeedGroups: z.array(specialNeedGroupSchema),
|
||||
gallery: gallerySchema.optional(),
|
||||
conferencesAndMeetings: facilitySchema.optional(),
|
||||
healthAndWellness: facilitySchema.optional(),
|
||||
restaurantImages: facilitySchema.optional(),
|
||||
restaurantsOverviewPage: restaurantsOverviewPageSchema,
|
||||
meetingRooms: extraPageSchema,
|
||||
healthAndFitness: extraPageSchema,
|
||||
hotelParking: extraPageSchema,
|
||||
hotelSpecialNeeds: extraPageSchema,
|
||||
accessibilityElevatorPitchText: z.string().optional(),
|
||||
hotelRoomElevatorPitchText: z.string().optional(),
|
||||
}),
|
||||
type: z.literal("additionalData"),
|
||||
})
|
||||
.transform(({ attributes, type }) => ({
|
||||
...attributes,
|
||||
type,
|
||||
id: attributes.id,
|
||||
}))
|
||||
@@ -36,6 +36,7 @@ export const restaurantSchema = z
|
||||
attributes: z.object({
|
||||
name: z.string(),
|
||||
isPublished: z.boolean().default(false),
|
||||
restaurantPage: z.boolean(),
|
||||
email: z.string().optional(),
|
||||
phoneNumber: z.string().optional(),
|
||||
externalBreakfast: z
|
||||
|
||||
@@ -82,3 +82,13 @@ export const meetingRoomsSuccessCounter = meter.createCounter(
|
||||
export const meetingRoomsFailCounter = meter.createCounter(
|
||||
"trpc.hotels.meetingRooms-fail"
|
||||
)
|
||||
|
||||
export const additionalDataCounter = meter.createCounter(
|
||||
"trpc.hotels.additionalData"
|
||||
)
|
||||
export const additionalDataSuccessCounter = meter.createCounter(
|
||||
"trpc.hotels.additionalData-success"
|
||||
)
|
||||
export const additionalDataFailCounter = meter.createCounter(
|
||||
"trpc.hotels.additionalData-fail"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user