Files
web/apps/scandic-web/utils/hotelSearchDetails.ts
Anton Gunnarsson 41efb3a7b3 Merged in feat/sw-3207-refactor-select-hotel-tracking (pull request #2587)
feat(SW-3207): Refactor select-hotel tracking

* Refactor select-hotel tracking


Approved-by: Bianca Widstam
2025-08-06 08:35:48 +00:00

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,
}
}