Feat/SW-2271 hotel list filtering
* feat(SW-2271): Changes to hotel data types in preperation for filtering * feat(SW-2271): Added filter and sort functionality Approved-by: Matilda Landström
This commit is contained in:
@@ -239,9 +239,13 @@ export const getHotelsByCSFilterInput = z.object({
|
||||
})
|
||||
.nullish(),
|
||||
hotelsToInclude: z.array(z.string()),
|
||||
contentType: z
|
||||
.enum(["hotel", "restaurant", "meeting"])
|
||||
.optional()
|
||||
.default("hotel"),
|
||||
})
|
||||
export interface GetHotelsByCSFilterInput
|
||||
extends z.infer<typeof getHotelsByCSFilterInput> {}
|
||||
extends z.input<typeof getHotelsByCSFilterInput> {}
|
||||
|
||||
export const nearbyHotelIdsInput = z.object({
|
||||
hotelId: z.string(),
|
||||
|
||||
@@ -616,40 +616,24 @@ export const roomFeaturesSchema = z
|
||||
return data.data.attributes.roomFeatures
|
||||
})
|
||||
|
||||
export const destinationPagesHotelDataSchema = z
|
||||
.object({
|
||||
data: z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
location: locationSchema,
|
||||
cityIdentifier: z.string().optional(),
|
||||
tripadvisor: z.number().optional(),
|
||||
detailedFacilities: detailedFacilitiesSchema,
|
||||
galleryImages: z
|
||||
.array(imageSchema)
|
||||
.nullish()
|
||||
.transform((arr) => (arr ? arr.filter(Boolean) : [])),
|
||||
address: addressSchema,
|
||||
hotelType: z.string(),
|
||||
type: z.literal("hotels"), // No enum here but the standard return appears to be "hotels".
|
||||
url: z.string().optional(),
|
||||
hotelContent: z
|
||||
.object({
|
||||
texts: z.object({
|
||||
descriptions: z.object({
|
||||
short: z.string().optional(),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
.optional(),
|
||||
}),
|
||||
})
|
||||
.transform(({ data: { hotelContent, ...data } }) => {
|
||||
return {
|
||||
hotel: {
|
||||
...data,
|
||||
hotelDescription: hotelContent?.texts.descriptions?.short,
|
||||
},
|
||||
url: data.url ?? "",
|
||||
}
|
||||
})
|
||||
export const hotelListingHotelDataSchema = z.object({
|
||||
hotel: z.object({
|
||||
id: z.string(),
|
||||
name: z.string(),
|
||||
countryCode: z.string(),
|
||||
location: locationSchema,
|
||||
cityIdentifier: z.string().nullable(),
|
||||
tripadvisor: z.number().nullable(),
|
||||
detailedFacilities: detailedFacilitiesSchema,
|
||||
galleryImages: z
|
||||
.array(imageSchema)
|
||||
.nullish()
|
||||
.transform((arr) => (arr ? arr.filter(Boolean) : [])),
|
||||
address: addressSchema,
|
||||
hotelType: z.string(),
|
||||
type: z.literal("hotels"), // No enum here but the standard return appears to be "hotels".
|
||||
description: z.string().nullable(),
|
||||
}),
|
||||
url: z.string().nullable(),
|
||||
meetingUrl: z.string().nullable(),
|
||||
})
|
||||
|
||||
@@ -53,19 +53,17 @@ import { getVerifiedUser } from "../user/utils"
|
||||
import { additionalDataSchema } from "./schemas/hotel/include/additionalData"
|
||||
import { meetingRoomsSchema } from "./schemas/meetingRoom"
|
||||
import {
|
||||
getBedTypes,
|
||||
getCitiesByCountry,
|
||||
getCountries,
|
||||
getHotel,
|
||||
getHotelIdsByCityId,
|
||||
getHotelIdsByCityIdentifier,
|
||||
getHotelIdsByCountry,
|
||||
getHotelsByHotelIds,
|
||||
getLocations,
|
||||
} from "./utils"
|
||||
import {
|
||||
getBedTypes,
|
||||
getHotelsAvailabilityByCity,
|
||||
getHotelsAvailabilityByHotelIds,
|
||||
getHotelsByHotelIds,
|
||||
getLocations,
|
||||
getPackages,
|
||||
getRoomsAvailability,
|
||||
getSelectedRoomAvailability,
|
||||
@@ -73,10 +71,7 @@ import {
|
||||
selectRateRedirectURL,
|
||||
} from "./utils"
|
||||
|
||||
import type {
|
||||
DestinationPagesHotelData,
|
||||
HotelDataWithUrl,
|
||||
} from "../../types/hotel"
|
||||
import type { HotelListingHotelData } from "../../types/hotel"
|
||||
import type { CityLocation } from "../../types/locations"
|
||||
import type { Room } from "../../types/room"
|
||||
|
||||
@@ -570,7 +565,7 @@ export const hotelQueryRouter = router({
|
||||
get: contentStackBaseWithServiceProcedure
|
||||
.input(getHotelsByCSFilterInput)
|
||||
.query(async function ({ ctx, input }) {
|
||||
const { locationFilter, hotelsToInclude } = input
|
||||
const { locationFilter, hotelsToInclude, contentType } = input
|
||||
|
||||
const language = ctx.lang
|
||||
let hotelsToFetch: string[] = []
|
||||
@@ -669,29 +664,16 @@ export const hotelQueryRouter = router({
|
||||
|
||||
return []
|
||||
}
|
||||
const hotelPages = await getHotelPageUrls(language)
|
||||
const hotels = await Promise.all(
|
||||
hotelsToFetch.map(async (hotelId) => {
|
||||
const hotelData = await getHotel(
|
||||
{ hotelId, isCardOnlyPayment: false, language },
|
||||
ctx.serviceToken
|
||||
)
|
||||
const hotelPage = hotelPages.find(
|
||||
(page) => page.hotelId === hotelId
|
||||
)
|
||||
|
||||
return hotelData
|
||||
? {
|
||||
...hotelData,
|
||||
url: hotelPage?.url ?? null,
|
||||
}
|
||||
: null
|
||||
})
|
||||
)
|
||||
const hotels = await getHotelsByHotelIds({
|
||||
hotelIds: hotelsToFetch,
|
||||
lang: language,
|
||||
serviceToken: ctx.serviceToken,
|
||||
contentType,
|
||||
})
|
||||
|
||||
metricsGetHotelsByCSFilter.success()
|
||||
|
||||
return hotels.filter((hotel): hotel is HotelDataWithUrl => !!hotel)
|
||||
return hotels
|
||||
}),
|
||||
}),
|
||||
getDestinationsMapData: serviceProcedure
|
||||
@@ -713,7 +695,7 @@ export const hotelQueryRouter = router({
|
||||
}
|
||||
|
||||
const countryNames = countries.data.map((country) => country.name)
|
||||
const hotelData: DestinationPagesHotelData[] = (
|
||||
const hotelData: HotelListingHotelData[] = (
|
||||
await Promise.all(
|
||||
countryNames.map(async (country) => {
|
||||
const hotelIds = await getHotelIdsByCountry({
|
||||
|
||||
@@ -48,8 +48,11 @@ import type {
|
||||
RoomsAvailabilityOutputSchema,
|
||||
} from "../../types/availability"
|
||||
import type { BedTypeSelection } from "../../types/bedTypeSelection"
|
||||
import type { Room as RoomCategory } from "../../types/hotel"
|
||||
import type { DestinationPagesHotelData, HotelInput } from "../../types/hotel"
|
||||
import type {
|
||||
HotelInput,
|
||||
HotelListingHotelData,
|
||||
Room as RoomCategory,
|
||||
} from "../../types/hotel"
|
||||
import type {
|
||||
CitiesGroupedByCountry,
|
||||
CityLocation,
|
||||
@@ -348,13 +351,15 @@ export async function getHotelsByHotelIds({
|
||||
hotelIds,
|
||||
lang,
|
||||
serviceToken,
|
||||
contentType = "hotel",
|
||||
}: {
|
||||
hotelIds: string[]
|
||||
lang: Lang
|
||||
serviceToken: string
|
||||
contentType?: "hotel" | "restaurant" | "meeting"
|
||||
}) {
|
||||
const cacheClient = await getCacheClient()
|
||||
const cacheKey = `${lang}:getHotelsByHotelIds:hotels:${hotelIds.sort().join(",")}`
|
||||
const cacheKey = `${lang}:getHotelsByHotelIds:hotels:${contentType}:${hotelIds.sort().join(",")}`
|
||||
|
||||
return await cacheClient.cacheOrGet(
|
||||
cacheKey,
|
||||
@@ -362,7 +367,7 @@ export async function getHotelsByHotelIds({
|
||||
const hotelPages = await getHotelPageUrls(lang)
|
||||
const chunkedHotelIds = chunk(hotelIds, 10)
|
||||
|
||||
const hotels: DestinationPagesHotelData[] = []
|
||||
const hotels: HotelListingHotelData[] = []
|
||||
for (const hotelIdChunk of chunkedHotelIds) {
|
||||
const chunkedHotels = await Promise.all(
|
||||
hotelIdChunk.map(async (hotelId) => {
|
||||
@@ -378,22 +383,59 @@ export async function getHotelsByHotelIds({
|
||||
const hotelPage = hotelPages.find(
|
||||
(page) => page.hotelId === hotelId
|
||||
)
|
||||
const { hotel, cities } = hotelResponse
|
||||
const data: DestinationPagesHotelData = {
|
||||
const { hotel, cities, additionalData } = hotelResponse
|
||||
|
||||
const content = {
|
||||
description: hotel.hotelContent?.texts.descriptions?.short,
|
||||
galleryImages: hotel.galleryImages,
|
||||
url: hotelPage?.url ?? "",
|
||||
openInNewTab: false,
|
||||
}
|
||||
|
||||
if (contentType === "restaurant") {
|
||||
const restaurantDescription =
|
||||
additionalData?.restaurantsOverviewPage
|
||||
.restaurantsContentDescriptionShort
|
||||
const restaurantImages =
|
||||
additionalData.restaurantImages?.heroImages
|
||||
if (restaurantDescription) {
|
||||
content.description = restaurantDescription
|
||||
}
|
||||
if (restaurantImages && restaurantImages.length > 0) {
|
||||
content.galleryImages = restaurantImages
|
||||
}
|
||||
} else if (contentType === "meeting") {
|
||||
const meetingDescription =
|
||||
hotel.hotelContent.texts.meetingDescription?.short
|
||||
const meetingImages =
|
||||
additionalData?.conferencesAndMeetings?.heroImages
|
||||
if (meetingDescription) {
|
||||
content.description = meetingDescription
|
||||
}
|
||||
if (meetingImages && meetingImages.length > 0) {
|
||||
content.galleryImages = meetingImages
|
||||
}
|
||||
}
|
||||
|
||||
const data: HotelListingHotelData = {
|
||||
hotel: {
|
||||
id: hotel.id,
|
||||
galleryImages: hotel.galleryImages,
|
||||
countryCode: hotel.countryCode,
|
||||
galleryImages: content.galleryImages,
|
||||
name: hotel.name,
|
||||
tripadvisor: hotel.ratings?.tripAdvisor?.rating,
|
||||
detailedFacilities: hotel.detailedFacilities || [],
|
||||
tripadvisor: hotel.ratings?.tripAdvisor?.rating || null,
|
||||
detailedFacilities: hotel.detailedFacilities.sort(
|
||||
(a, b) => b.sortOrder - a.sortOrder
|
||||
),
|
||||
location: hotel.location,
|
||||
hotelType: hotel.hotelType,
|
||||
type: hotel.type,
|
||||
address: hotel.address,
|
||||
cityIdentifier: cities?.[0]?.cityIdentifier,
|
||||
hotelDescription: hotel.hotelContent?.texts.descriptions?.short,
|
||||
cityIdentifier: cities[0]?.cityIdentifier || null,
|
||||
description: content.description || null,
|
||||
},
|
||||
url: hotelPage?.url ?? "",
|
||||
url: content.url,
|
||||
meetingUrl: additionalData.meetingRooms.meetingOnlineLink || null,
|
||||
}
|
||||
|
||||
return data
|
||||
@@ -402,9 +444,7 @@ export async function getHotelsByHotelIds({
|
||||
|
||||
hotels.push(...chunkedHotels)
|
||||
}
|
||||
return hotels.filter(
|
||||
(hotel): hotel is DestinationPagesHotelData => !!hotel
|
||||
)
|
||||
return hotels.filter((hotel): hotel is HotelListingHotelData => !!hotel)
|
||||
},
|
||||
"1d"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user