feat(SW-3207): Refactor select-hotel tracking * Refactor select-hotel tracking Approved-by: Bianca Widstam
78 lines
2.0 KiB
TypeScript
78 lines
2.0 KiB
TypeScript
import { notFound } from "next/navigation"
|
|
|
|
import { safeTry } from "@scandic-hotels/common/utils/safeTry"
|
|
import { SEARCH_TYPE_REDEMPTION } from "@scandic-hotels/trpc/constants/booking"
|
|
import {
|
|
type HotelLocation,
|
|
isHotelLocation,
|
|
type Location,
|
|
} from "@scandic-hotels/trpc/types/locations"
|
|
|
|
import { getLocations } from "@/lib/trpc/memoizedRequests"
|
|
|
|
import type { BookingSearchType } from "@scandic-hotels/booking-flow/searchType"
|
|
import type { Child } from "@scandic-hotels/trpc/types/child"
|
|
|
|
export type ChildrenInRoom = (Child[] | null)[] | null
|
|
|
|
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
|
|
): Promise<HotelSearchDetails | null> {
|
|
const [locations, error] = await safeTry(getLocations())
|
|
if (!locations || error) {
|
|
return null
|
|
}
|
|
|
|
const hotel = params.hotelId
|
|
? ((locations.find(
|
|
(location) =>
|
|
isHotelLocation(location) &&
|
|
"operaId" in location &&
|
|
location.operaId === params.hotelId
|
|
) as HotelLocation | undefined) ?? null)
|
|
: null
|
|
|
|
if (isAlternativeHotels && !hotel) {
|
|
return notFound()
|
|
}
|
|
|
|
const cityIdentifier = isAlternativeHotels
|
|
? hotel?.relationships.city.cityIdentifier
|
|
: params.city
|
|
|
|
const city = cityIdentifier
|
|
? (locations.find(
|
|
(location) =>
|
|
"cityIdentifier" in location &&
|
|
location.cityIdentifier?.toLowerCase() ===
|
|
cityIdentifier.toLowerCase()
|
|
) ?? null)
|
|
: null
|
|
|
|
if (!city && !hotel) return notFound()
|
|
if (isAlternativeHotels && (!city || !hotel)) return notFound()
|
|
|
|
return {
|
|
city,
|
|
cityIdentifier,
|
|
hotel,
|
|
redemption: params.searchType === SEARCH_TYPE_REDEMPTION,
|
|
}
|
|
}
|