Files
web/apps/scandic-web/components/HotelReservation/MyStay/utils/mapRoomDetails.ts
Pontus Dreij a8358de04a Merged in feat(SW-2084)-disable-options-modify-my-stay (pull request #1662)
feat(SW-2084) logic to disable Manage stay options

* feat(SW-2084) logic to disable Manage stay options

* feat(SW-2084) cleanup logic for checks

* feat(SW-2084) check if date has passed

* feat(SW-2084) change to datetimeIsInThePast


Approved-by: Niclas Edenvin
2025-03-31 07:44:46 +00:00

146 lines
4.5 KiB
TypeScript

import { BookingStatusEnum, CancellationRuleEnum } from "@/constants/booking"
import { dt } from "@/lib/dt"
import { formatChildBedPreferences } from "../utils"
import { convertToChildType } from "./convertToChildType"
import type { BreakfastPackage } from "@/types/components/hotelReservation/breakfast"
import { RoomPackageCodeEnum } from "@/types/components/hotelReservation/selectRate/roomFilter"
import { BreakfastPackageEnum } from "@/types/enums/breakfast"
import { PackageTypeEnum } from "@/types/enums/packages"
import type { Room } from "@/types/hotel"
import type { BookingConfirmation } from "@/types/trpc/routers/booking/confirmation"
import type { Room as MyStayRoom } from "@/stores/my-stay/myStayRoomDetailsStore"
interface MapRoomDetailsParams {
booking: BookingConfirmation["booking"]
room: (Room & { bedType: Room["roomTypes"][number] }) | null
roomNumber: number
}
export function mapRoomDetails({
booking,
room,
roomNumber,
}: MapRoomDetailsParams): MyStayRoom {
const nights = dt(booking.checkOutDate)
.startOf("day")
.diff(dt(booking.checkInDate).startOf("day"), "days")
const breakfastPkg = booking.packages.find(
(pkg) =>
pkg.code === BreakfastPackageEnum.REGULAR_BREAKFAST ||
pkg.code === BreakfastPackageEnum.FREE_MEMBER_BREAKFAST ||
pkg.code === BreakfastPackageEnum.SPECIAL_PACKAGE_BREAKFAST
)
const featuresPkg = booking.packages.filter(
(pkg) =>
pkg.code === RoomPackageCodeEnum.PET_ROOM ||
pkg.code === RoomPackageCodeEnum.ALLERGY_ROOM ||
pkg.code === RoomPackageCodeEnum.ACCESSIBILITY_ROOM
)
const breakfast: BreakfastPackage | false = breakfastPkg
? {
code: breakfastPkg.code,
description: breakfastPkg.description,
localPrice: {
currency: breakfastPkg.currency,
price: breakfastPkg.unitPrice,
totalPrice: breakfastPkg.totalPrice,
},
requestedPrice: {
currency: breakfastPkg.currency,
price: breakfastPkg.unitPrice,
totalPrice: breakfastPkg.totalPrice,
},
packageType: PackageTypeEnum.BreakfastAdult,
}
: false
const isCancelled = booking.reservationStatus === BookingStatusEnum.Cancelled
const childrenAsString = formatChildBedPreferences({
childrenAges: booking.childrenAges,
childBedPreferences: booking.childBedPreferences,
})
const childrenInRoom = convertToChildType(
booking.childrenAges,
booking.childBedPreferences
)
const isPrePaid =
!!booking.guaranteeInfo?.paymentMethodDescription ||
booking.rateDefinition.cancellationRule !==
CancellationRuleEnum.CancellableBefore6PM
return {
hotelId: booking.hotelId,
roomTypeCode: booking.roomTypeCode,
adults: booking.adults,
childrenAges: booking.childrenAges,
checkInDate: booking.checkInDate,
checkOutDate: booking.checkOutDate,
confirmationNumber: booking.confirmationNumber,
cancellationNumber: booking.cancellationNumber,
createDateTime: booking.createDateTime,
rateDefinition: booking.rateDefinition,
guaranteeInfo: booking.guaranteeInfo,
linkedReservations: booking.linkedReservations,
bookingCode: booking.bookingCode,
isCancelable: booking.isCancelable,
multiRoom: booking.multiRoom,
canChangeDate: booking.canChangeDate,
guest: booking.guest,
currencyCode: booking.currencyCode,
vatPercentage: booking.vatPercentage,
mainRoom: booking.mainRoom,
roomName: room?.name ?? "",
roomNumber,
isCancelled,
childrenInRoom,
childrenAsString,
terms: booking.rateDefinition.cancellationText,
packages: featuresPkg.map((pkg) => ({
code: pkg.code as RoomPackageCodeEnum,
description: pkg.description,
inventories: [],
itemCode: "",
localPrice: {
currency: pkg.currency,
price: pkg.unitPrice,
totalPrice: pkg.totalPrice,
},
requestedPrice: {
currency: pkg.currency,
price: pkg.unitPrice,
totalPrice: pkg.totalPrice,
},
})),
bedType: {
description: room?.bedType.mainBed.description ?? "",
roomTypeCode: room?.bedType.code ?? "",
},
roomPrice: {
perNight: {
local: {
currency: booking.currencyCode,
price: isCancelled ? 0 : booking.roomPrice / nights,
},
requested: undefined,
},
perStay: {
local: {
currency: booking.currencyCode,
price: isCancelled ? 0 : booking.roomPrice,
},
requested: undefined,
},
},
breakfast,
isPrePaid,
}
}