Files
web/packages/booking-flow/lib/misc/getHotelSearchDetails.ts
Joakim Jäderberg 15a2da333d Merged in feature/SW-3572-hotel-data-endpoint (pull request #3051)
SW-3572 API route for listing hotels per city or country

* wip hotel data endpoint

* Correct route params type

* wip

* skip static paths call

* timeout when getting destinations take too long

* call noStore when we get a timeout

* add cache-control headers

* .

* .

* .

* wip

* wip

* wip

* wip

* add route for getting hotels per country

* include city when listing by country

* fix distance SI unit

* fix sorting

* Merge branch 'master' of bitbucket.org:scandic-swap/web into feature/SW-3572-hotel-data-endpoint

* packages/tracking passWithNoTests

* revalidate must be static value

* remove oxc reference

* cleanup

* cleanup hotel api route

* feat(SW-3572): cleanup error handling


Approved-by: Anton Gunnarsson
2025-11-03 12:10:22 +00:00

74 lines
1.9 KiB
TypeScript

import { safeTry } from "@scandic-hotels/common/utils/safeTry"
import { SEARCH_TYPE_REDEMPTION } from "@scandic-hotels/trpc/constants/booking"
import {
type HotelLocation,
isCityLocation,
isHotelLocation,
type Location,
} from "@scandic-hotels/trpc/types/locations"
import { getLocations } from "../trpc/memoizedRequests/getLocations"
import type { Lang } from "@scandic-hotels/common/constants/language"
import type { Child } from "@scandic-hotels/trpc/types/child"
import type { BookingSearchType } from "./searchType"
interface HotelSearchDetails {
city: Location | null
cityIdentifier: string | undefined
hotel: HotelLocation | null
redemption?: boolean
}
export async function getHotelSearchDetails(params: {
hotelId?: string
city?: string
rooms?: {
adults: number
childrenInRoom?: Child[]
}[]
searchType?: BookingSearchType
isAlternativeHotels?: boolean
lang: Lang
}): Promise<HotelSearchDetails | null> {
const [locations, error] = await safeTry(getLocations(params.lang))
if (!locations || error) {
return null
}
const hotel = params.hotelId
? (locations
.filter(isHotelLocation)
.find((location) => location.operaId === params.hotelId) ?? null)
: null
if (params.isAlternativeHotels && !hotel) {
return null
}
const cityIdentifier = params.isAlternativeHotels
? hotel?.relationships.city.cityIdentifier
: params.city
const city = cityIdentifier
? (locations
.filter(isCityLocation)
.find(
(location) =>
location.cityIdentifier?.toLowerCase() ===
cityIdentifier.toLowerCase()
) ?? null)
: null
if (!city && !hotel) return null
if (params.isAlternativeHotels && (!city || !hotel)) return null
return {
city,
cityIdentifier,
hotel,
redemption: params.searchType === SEARCH_TYPE_REDEMPTION,
}
}