feat (SW-2864): Move booking router to trpc package * Add env to trpc package * Add eslint to trpc package * Apply lint rules * Use direct imports from trpc package * Add lint-staged config to trpc * Move lang enum to common * Restructure trpc package folder structure * WIP first step * update internal imports in trpc * Fix most errors in scandic-web Just 100 left... * Move Props type out of trpc * Fix CategorizedFilters types * Move more schemas in hotel router * Fix deps * fix getNonContentstackUrls * Fix import error * Fix entry error handling * Fix generateMetadata metrics * Fix alertType enum * Fix duplicated types * lint:fix * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package * Fix broken imports * Move booking router to trpc package * Merge branch 'master' into feat/sw-2864-move-hotels-router-to-trpc-package Approved-by: Linus Flood
107 lines
2.8 KiB
TypeScript
107 lines
2.8 KiB
TypeScript
import { notFound } from "next/navigation"
|
|
|
|
import { safeTry } from "@scandic-hotels/common/utils/safeTry"
|
|
import { REDEMPTION } from "@scandic-hotels/trpc/constants/booking"
|
|
import { generateChildrenString } from "@scandic-hotels/trpc/routers/hotels/helpers"
|
|
import {
|
|
type HotelLocation,
|
|
isHotelLocation,
|
|
type Location,
|
|
} from "@scandic-hotels/trpc/types/locations"
|
|
|
|
import { getLocations } from "@/lib/trpc/memoizedRequests"
|
|
|
|
import type { Child } from "@scandic-hotels/trpc/types/child"
|
|
|
|
import type { BookingSearchType } from "@/types/components/hotelReservation/booking"
|
|
|
|
export type ChildrenInRoom = (Child[] | null)[] | null
|
|
export type ChildrenInRoomString = (string | null)[] | null
|
|
|
|
interface HotelSearchDetails {
|
|
adultsInRoom: number[]
|
|
bookingCode?: string
|
|
childrenInRoom: ChildrenInRoom
|
|
childrenInRoomString: ChildrenInRoomString
|
|
city: Location | null
|
|
cityIdentifier: string | undefined
|
|
hotel: HotelLocation | null
|
|
noOfRooms: number
|
|
redemption?: boolean
|
|
}
|
|
|
|
export async function getHotelSearchDetails(
|
|
params: {
|
|
hotelId?: string
|
|
city?: string
|
|
rooms?: {
|
|
adults: number
|
|
childrenInRoom?: Child[]
|
|
}[]
|
|
bookingCode?: string
|
|
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()
|
|
|
|
let adultsInRoom: number[] = []
|
|
let childrenInRoom: ChildrenInRoom = null
|
|
let childrenInRoomString: ChildrenInRoomString = null
|
|
|
|
const { rooms } = params
|
|
|
|
if (rooms?.length) {
|
|
adultsInRoom = rooms.map((room) => room.adults ?? 0)
|
|
|
|
childrenInRoom = rooms.map((room) => room.childrenInRoom ?? null)
|
|
childrenInRoomString = rooms.map((room) =>
|
|
room.childrenInRoom ? generateChildrenString(room.childrenInRoom) : null
|
|
)
|
|
}
|
|
|
|
return {
|
|
adultsInRoom,
|
|
bookingCode: params.bookingCode ?? undefined,
|
|
childrenInRoom,
|
|
childrenInRoomString,
|
|
city,
|
|
cityIdentifier,
|
|
hotel,
|
|
noOfRooms: rooms?.length ?? 0,
|
|
redemption: params.searchType === REDEMPTION,
|
|
}
|
|
}
|