feat: refactor of my stay
This commit is contained in:
committed by
Simon.Emanuelsson
parent
b5deb84b33
commit
ec087a3d15
@@ -0,0 +1,82 @@
|
||||
import type {
|
||||
Ancillary,
|
||||
SelectedAncillary,
|
||||
} from "@/types/components/myPages/myStay/ancillaries"
|
||||
import type { AncillaryFormData } from "@/components/HotelReservation/MyStay/Ancillaries/AddAncillaryFlow/schema"
|
||||
|
||||
export const generateDeliveryOptions = () => {
|
||||
const timeSlots = ["16:00-17:00", "17:00-18:00", "18:00-19:00", "19:00-20:00"]
|
||||
|
||||
return timeSlots.map((slot) => ({
|
||||
label: slot,
|
||||
value: slot,
|
||||
}))
|
||||
}
|
||||
|
||||
export function buildAncillaryPackages(
|
||||
data: AncillaryFormData,
|
||||
ancillary: SelectedAncillary | null
|
||||
) {
|
||||
const packages = []
|
||||
|
||||
if (ancillary?.id && data.quantityWithCard) {
|
||||
packages.push({
|
||||
code: ancillary.id,
|
||||
quantity: data.quantityWithCard,
|
||||
comment: data.optionalText || undefined,
|
||||
})
|
||||
}
|
||||
|
||||
if (ancillary?.loyaltyCode && data.quantityWithPoints) {
|
||||
packages.push({
|
||||
code: ancillary.loyaltyCode,
|
||||
quantity: data.quantityWithPoints,
|
||||
comment: data.optionalText || undefined,
|
||||
})
|
||||
}
|
||||
|
||||
return packages
|
||||
}
|
||||
|
||||
const ancillarySessionKey = "ancillarySessionData"
|
||||
export const getAncillarySessionData = ():
|
||||
| {
|
||||
formData?: AncillaryFormData
|
||||
selectedAncillary?: Ancillary["ancillaryContent"][number] | null
|
||||
}
|
||||
| undefined => {
|
||||
if (typeof window === "undefined") return undefined
|
||||
|
||||
try {
|
||||
const storedData = sessionStorage.getItem(ancillarySessionKey)
|
||||
return storedData ? JSON.parse(storedData) : undefined
|
||||
} catch (error) {
|
||||
console.error("Error reading from session storage:", error)
|
||||
return undefined
|
||||
}
|
||||
}
|
||||
|
||||
export function setAncillarySessionData({
|
||||
formData,
|
||||
selectedAncillary,
|
||||
}: {
|
||||
formData?: AncillaryFormData
|
||||
selectedAncillary?: Ancillary["ancillaryContent"][number] | null
|
||||
}) {
|
||||
if (typeof window === "undefined") return
|
||||
|
||||
try {
|
||||
const currentData = getAncillarySessionData() || {}
|
||||
sessionStorage.setItem(
|
||||
ancillarySessionKey,
|
||||
JSON.stringify({ ...currentData, formData, selectedAncillary })
|
||||
)
|
||||
} catch (error) {
|
||||
console.error("Error writing to session storage:", error)
|
||||
}
|
||||
}
|
||||
|
||||
export function clearAncillarySessionData() {
|
||||
if (typeof window === "undefined") return
|
||||
sessionStorage.removeItem(ancillarySessionKey)
|
||||
}
|
||||
@@ -1,27 +1,29 @@
|
||||
import { BookingStatusEnum } from "@/constants/booking"
|
||||
import { BookingStatusEnum, CancellationRuleEnum } from "@/constants/booking"
|
||||
import { dt } from "@/lib/dt"
|
||||
|
||||
import { formatChildBedPreferences } from "../utils"
|
||||
import { convertToChildType } from "./convertToChildType"
|
||||
import { getPriceType } from "./getPriceType"
|
||||
import { getBreakfastPackagesFromBookingFlow } from "./hasBreakfastPackage"
|
||||
|
||||
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 { RateEnum } from "@/types/enums/rate"
|
||||
import type { Room } from "@/types/hotel"
|
||||
import type { Room as MyStayRoom } from "@/types/stores/my-stay"
|
||||
import type { BookingConfirmation } from "@/types/trpc/routers/booking/confirmation"
|
||||
import type { Room as MyStayRoom } from "@/stores/my-stay/myStayRoomDetailsStore"
|
||||
|
||||
interface MapRoomDetailsParams {
|
||||
booking: BookingConfirmation["booking"]
|
||||
rates: Record<RateEnum, string>
|
||||
room: (Room & { bedType: Room["roomTypes"][number] }) | null
|
||||
roomNumber: number
|
||||
}
|
||||
|
||||
export function mapRoomDetails({
|
||||
booking,
|
||||
rates,
|
||||
room,
|
||||
roomNumber,
|
||||
}: MapRoomDetailsParams): MyStayRoom {
|
||||
@@ -29,45 +31,29 @@ export function mapRoomDetails({
|
||||
.startOf("day")
|
||||
.diff(dt(booking.checkInDate).startOf("day"), "days")
|
||||
|
||||
const breakfastPackages = getBreakfastPackagesFromBookingFlow(
|
||||
booking.packages
|
||||
)
|
||||
const featuresPackages = booking.packages.filter(
|
||||
(pkg) =>
|
||||
pkg.code === RoomPackageCodeEnum.PET_ROOM ||
|
||||
pkg.code === RoomPackageCodeEnum.ALLERGY_ROOM ||
|
||||
pkg.code === RoomPackageCodeEnum.ACCESSIBILITY_ROOM
|
||||
const validBreakfastPackages: string[] = [
|
||||
BreakfastPackageEnum.REGULAR_BREAKFAST,
|
||||
BreakfastPackageEnum.ANCILLARY_CHILD_PAYING_BREAKFAST,
|
||||
BreakfastPackageEnum.ANCILLARY_REGULAR_BREAKFAST,
|
||||
]
|
||||
const breakfastPackage = booking.packages.find((pkg) =>
|
||||
validBreakfastPackages.includes(pkg.code)
|
||||
)
|
||||
|
||||
const breakfast: BreakfastPackage | null = breakfastPackages?.length
|
||||
? {
|
||||
code: BreakfastPackageEnum.REGULAR_BREAKFAST,
|
||||
description: breakfastPackages[0].description,
|
||||
localPrice: {
|
||||
currency: breakfastPackages[0].currency,
|
||||
price: breakfastPackages.reduce(
|
||||
(acc, curr) => acc + curr.unitPrice,
|
||||
0
|
||||
),
|
||||
totalPrice: breakfastPackages.reduce(
|
||||
(acc, curr) => acc + curr.totalPrice,
|
||||
0
|
||||
),
|
||||
},
|
||||
requestedPrice: {
|
||||
currency: breakfastPackages[0].currency,
|
||||
price: breakfastPackages.reduce(
|
||||
(acc, curr) => acc + curr.unitPrice,
|
||||
0
|
||||
),
|
||||
totalPrice: breakfastPackages.reduce(
|
||||
(acc, curr) => acc + curr.totalPrice,
|
||||
0
|
||||
),
|
||||
},
|
||||
packageType: PackageTypeEnum.BreakfastAdult,
|
||||
}
|
||||
: null
|
||||
// We don't get `requestedPrice` in packages
|
||||
const breakfast: Omit<BreakfastPackage, "requestedPrice"> | null =
|
||||
breakfastPackage
|
||||
? {
|
||||
code: breakfastPackage.code,
|
||||
description: breakfastPackage.description,
|
||||
localPrice: {
|
||||
currency: breakfastPackage.currency,
|
||||
price: breakfastPackage.unitPrice,
|
||||
totalPrice: breakfastPackage.totalPrice,
|
||||
},
|
||||
packageType: PackageTypeEnum.BreakfastAdult,
|
||||
}
|
||||
: null
|
||||
|
||||
const isCancelled = booking.reservationStatus === BookingStatusEnum.Cancelled
|
||||
|
||||
@@ -87,55 +73,80 @@ export function mapRoomDetails({
|
||||
booking.vouchers
|
||||
)
|
||||
|
||||
let rate = ""
|
||||
if (booking.rateDefinition.cancellationRule) {
|
||||
switch (booking.rateDefinition.cancellationRule) {
|
||||
case CancellationRuleEnum.CancellableBefore6PM:
|
||||
rate = rates.flex
|
||||
break
|
||||
case CancellationRuleEnum.Changeable:
|
||||
rate = rates.change
|
||||
break
|
||||
case CancellationRuleEnum.NonCancellable:
|
||||
rate = rates.save
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
const featuresPackages = booking.packages.filter(
|
||||
(pkg) =>
|
||||
pkg.code === RoomPackageCodeEnum.PET_ROOM ||
|
||||
pkg.code === RoomPackageCodeEnum.ALLERGY_ROOM ||
|
||||
pkg.code === RoomPackageCodeEnum.ACCESSIBILITY_ROOM
|
||||
)
|
||||
|
||||
const packages = featuresPackages.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,
|
||||
},
|
||||
}))
|
||||
|
||||
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,
|
||||
cheques: booking.cheques,
|
||||
vouchers: booking.vouchers,
|
||||
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: featuresPackages.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 ?? "",
|
||||
},
|
||||
bookingCode: booking.bookingCode,
|
||||
breakfast,
|
||||
canChangeDate: booking.canChangeDate,
|
||||
cancellationNumber: booking.cancellationNumber,
|
||||
checkInDate: booking.checkInDate,
|
||||
checkOutDate: booking.checkOutDate,
|
||||
cheques: booking.cheques,
|
||||
childrenAges: booking.childrenAges,
|
||||
childrenAsString,
|
||||
childrenInRoom,
|
||||
confirmationNumber: booking.confirmationNumber,
|
||||
createDateTime: booking.createDateTime,
|
||||
currencyCode: booking.currencyCode,
|
||||
guaranteeInfo: booking.guaranteeInfo,
|
||||
guest: booking.guest,
|
||||
hotelId: booking.hotelId,
|
||||
isCancelable: booking.isCancelable,
|
||||
isCancelled,
|
||||
linkedReservations: booking.linkedReservations,
|
||||
mainRoom: booking.mainRoom,
|
||||
multiRoom: booking.multiRoom,
|
||||
packages,
|
||||
priceType,
|
||||
rate,
|
||||
rateDefinition: booking.rateDefinition,
|
||||
reservationStatus: booking.reservationStatus,
|
||||
room,
|
||||
roomName: room?.name ?? "",
|
||||
roomNumber,
|
||||
roomPoints: booking.roomPoints,
|
||||
roomPrice: {
|
||||
perNight: {
|
||||
@@ -153,10 +164,13 @@ export function mapRoomDetails({
|
||||
requested: undefined,
|
||||
},
|
||||
},
|
||||
totalPriceExVat: booking.totalPriceExVat,
|
||||
roomTypeCode: booking.roomTypeCode,
|
||||
terms: booking.rateDefinition.cancellationText,
|
||||
totalPoints: booking.totalPoints,
|
||||
totalPrice: booking.totalPrice,
|
||||
totalPriceExVat: booking.totalPriceExVat,
|
||||
vatAmount: booking.vatAmount,
|
||||
breakfast,
|
||||
priceType,
|
||||
vatPercentage: booking.vatPercentage,
|
||||
vouchers: booking.vouchers,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user