feat: merge stores, fix auto navigation, split summary
This commit is contained in:
354
stores/enter-details/helpers.ts
Normal file
354
stores/enter-details/helpers.ts
Normal file
@@ -0,0 +1,354 @@
|
||||
import { z } from "zod"
|
||||
|
||||
import { Lang } from "@/constants/languages"
|
||||
import { breakfastPackageSchema } from "@/server/routers/hotels/output"
|
||||
|
||||
import { bedTypeSchema } from "@/components/HotelReservation/EnterDetails/BedType/schema"
|
||||
import { breakfastStoreSchema } from "@/components/HotelReservation/EnterDetails/Breakfast/schema"
|
||||
import {
|
||||
guestDetailsSchema,
|
||||
signedInDetailsSchema,
|
||||
} from "@/components/HotelReservation/EnterDetails/Details/schema"
|
||||
import { getLang } from "@/i18n/serverContext"
|
||||
|
||||
import type { BookingData } from "@/types/components/hotelReservation/enterDetails/bookingData"
|
||||
import { RoomPackageCodeEnum } from "@/types/components/hotelReservation/selectRate/roomFilter"
|
||||
import { CurrencyEnum } from "@/types/enums/currency"
|
||||
import { StepEnum } from "@/types/enums/step"
|
||||
import type { DetailsState, RoomRate } from "@/types/stores/enter-details"
|
||||
import type { SafeUser } from "@/types/user"
|
||||
|
||||
export function langToCurrency() {
|
||||
const lang = getLang()
|
||||
switch (lang) {
|
||||
case Lang.da:
|
||||
return CurrencyEnum.DKK
|
||||
case Lang.de:
|
||||
case Lang.en:
|
||||
case Lang.fi:
|
||||
return CurrencyEnum.EUR
|
||||
case Lang.no:
|
||||
return CurrencyEnum.NOK
|
||||
case Lang.sv:
|
||||
return CurrencyEnum.SEK
|
||||
default:
|
||||
throw new Error(`Unexpected lang: ${lang}`)
|
||||
}
|
||||
}
|
||||
|
||||
export function extractGuestFromUser(user: NonNullable<SafeUser>) {
|
||||
return {
|
||||
countryCode: user.address.countryCode?.toString(),
|
||||
email: user.email,
|
||||
firstName: user.firstName,
|
||||
lastName: user.lastName,
|
||||
join: false,
|
||||
membershipNo: user.membership?.membershipNumber,
|
||||
phoneNumber: user.phoneNumber ?? "",
|
||||
}
|
||||
}
|
||||
|
||||
export function navigate(step: StepEnum, searchParams: string) {
|
||||
window.history.pushState({ step }, "", `${step}?${searchParams}`)
|
||||
}
|
||||
|
||||
export function checkIsSameBooking(prev: BookingData, next: BookingData) {
|
||||
return (
|
||||
prev.fromDate === next.fromDate ||
|
||||
prev.toDate === next.toDate ||
|
||||
prev.hotel === next.hotel ||
|
||||
prev.rooms[0].adults === next.rooms[0].adults ||
|
||||
prev.rooms[0].children === next.rooms[0].children ||
|
||||
prev.rooms[0].roomTypeCode === next.rooms[0].roomTypeCode
|
||||
)
|
||||
}
|
||||
|
||||
export function validateSteps(currentState: DetailsState, isMember: boolean) {
|
||||
const validPaths = [StepEnum.selectBed]
|
||||
const validatedBedType = bedTypeSchema.safeParse(currentState)
|
||||
if (validatedBedType.success) {
|
||||
currentState.isValid["select-bed"] = true
|
||||
validPaths.push(currentState.steps[1])
|
||||
}
|
||||
|
||||
const validatedBreakfast = breakfastStoreSchema.safeParse(currentState)
|
||||
if (validatedBreakfast.success) {
|
||||
currentState.isValid.breakfast = true
|
||||
validPaths.push(StepEnum.details)
|
||||
}
|
||||
|
||||
const detailsSchema = isMember ? signedInDetailsSchema : guestDetailsSchema
|
||||
const validatedDetails = detailsSchema.safeParse(currentState.guest)
|
||||
// Need to add the breakfast check here too since
|
||||
// when a member comes into the flow, their data is
|
||||
// already added and valid, and thus to avoid showing a
|
||||
// step the user hasn't been on yet as complete
|
||||
if (currentState.isValid.breakfast && validatedDetails.success) {
|
||||
currentState.isValid.details = true
|
||||
validPaths.push(StepEnum.payment)
|
||||
}
|
||||
|
||||
return validPaths
|
||||
}
|
||||
|
||||
export function add(...nums: (number | string | undefined)[]) {
|
||||
return nums.reduce((total: number, num) => {
|
||||
if (typeof num === "undefined") {
|
||||
num = 0
|
||||
}
|
||||
total = total + parseInt(`${num}`)
|
||||
return total
|
||||
}, 0)
|
||||
}
|
||||
|
||||
export function subtract(...nums: (number | string | undefined)[]) {
|
||||
return nums.reduce((total: number, num, idx) => {
|
||||
if (typeof num === "undefined") {
|
||||
num = 0
|
||||
}
|
||||
if (idx === 0) {
|
||||
return parseInt(`${num}`)
|
||||
}
|
||||
total = total - parseInt(`${num}`)
|
||||
if (total < 0) {
|
||||
return 0
|
||||
}
|
||||
return total
|
||||
}, 0)
|
||||
}
|
||||
|
||||
export function getInitialRoomPrice(roomRate: RoomRate, isMember: boolean) {
|
||||
if (isMember && roomRate.memberRate) {
|
||||
return {
|
||||
euro: {
|
||||
currency: CurrencyEnum.EUR,
|
||||
price: roomRate.memberRate.requestedPrice?.pricePerStay ?? 0,
|
||||
},
|
||||
local: {
|
||||
currency: roomRate.memberRate.localPrice.currency,
|
||||
price: roomRate.memberRate.localPrice.pricePerStay,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
euro: {
|
||||
currency: CurrencyEnum.EUR,
|
||||
price: roomRate.publicRate.requestedPrice?.pricePerStay ?? 0,
|
||||
},
|
||||
local: {
|
||||
currency: roomRate.publicRate.localPrice.currency,
|
||||
price: roomRate.publicRate.localPrice.pricePerStay,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export function getInitialTotalPrice(roomRate: RoomRate, isMember: boolean) {
|
||||
if (isMember && roomRate.memberRate) {
|
||||
return {
|
||||
euro: {
|
||||
currency: CurrencyEnum.EUR,
|
||||
price: roomRate.memberRate.requestedPrice?.pricePerStay ?? 0,
|
||||
},
|
||||
local: {
|
||||
currency: roomRate.memberRate.localPrice.currency,
|
||||
price: roomRate.memberRate.localPrice.pricePerStay,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
euro: {
|
||||
currency: CurrencyEnum.EUR,
|
||||
price: roomRate.publicRate.requestedPrice?.pricePerStay ?? 0,
|
||||
},
|
||||
local: {
|
||||
currency: roomRate.publicRate.localPrice.currency,
|
||||
price: roomRate.publicRate.localPrice.pricePerStay,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
export function calcTotalMemberPrice(state: DetailsState) {
|
||||
if (!state.roomRate.memberRate) {
|
||||
return {
|
||||
roomPrice: state.roomPrice,
|
||||
totalPrice: state.totalPrice,
|
||||
}
|
||||
}
|
||||
|
||||
const roomAndTotalPrice = {
|
||||
roomPrice: state.roomPrice,
|
||||
totalPrice: state.totalPrice,
|
||||
}
|
||||
if (state.roomRate.memberRate.requestedPrice?.pricePerStay) {
|
||||
roomAndTotalPrice.roomPrice.euro = {
|
||||
currency: CurrencyEnum.EUR,
|
||||
price: state.roomRate.memberRate.requestedPrice.pricePerStay,
|
||||
}
|
||||
|
||||
let totalPriceEuro = state.roomRate.memberRate.requestedPrice.pricePerStay
|
||||
if (state.breakfast) {
|
||||
totalPriceEuro = add(
|
||||
totalPriceEuro,
|
||||
state.breakfast.requestedPrice.totalPrice
|
||||
)
|
||||
}
|
||||
|
||||
if (state.packages) {
|
||||
totalPriceEuro = state.packages.reduce((total, pkg) => {
|
||||
if (pkg.requestedPrice.totalPrice) {
|
||||
total = add(total, pkg.requestedPrice.totalPrice)
|
||||
}
|
||||
return total
|
||||
}, totalPriceEuro)
|
||||
}
|
||||
|
||||
roomAndTotalPrice.totalPrice.euro = {
|
||||
currency: CurrencyEnum.EUR,
|
||||
price: totalPriceEuro,
|
||||
}
|
||||
}
|
||||
|
||||
const roomPriceLocal = state.roomRate.memberRate.localPrice
|
||||
roomAndTotalPrice.roomPrice.local = {
|
||||
currency: roomPriceLocal.currency,
|
||||
price: roomPriceLocal.pricePerStay,
|
||||
}
|
||||
|
||||
let totalPriceLocal = roomPriceLocal.pricePerStay
|
||||
if (state.breakfast) {
|
||||
totalPriceLocal = add(
|
||||
totalPriceLocal,
|
||||
state.breakfast.localPrice.totalPrice
|
||||
)
|
||||
}
|
||||
|
||||
if (state.packages) {
|
||||
totalPriceLocal = state.packages.reduce((total, pkg) => {
|
||||
if (pkg.localPrice.totalPrice) {
|
||||
total = add(total, pkg.localPrice.totalPrice)
|
||||
}
|
||||
return total
|
||||
}, totalPriceLocal)
|
||||
}
|
||||
roomAndTotalPrice.totalPrice.local = {
|
||||
currency: roomPriceLocal.currency,
|
||||
price: totalPriceLocal,
|
||||
}
|
||||
|
||||
return roomAndTotalPrice
|
||||
}
|
||||
|
||||
export function getHydratedMemberPrice(
|
||||
memberRate: NonNullable<RoomRate["memberRate"]>,
|
||||
breakfast: DetailsState["breakfast"],
|
||||
packages: DetailsState["packages"]
|
||||
) {
|
||||
const memberPrice = {
|
||||
euro: {
|
||||
currency: CurrencyEnum.EUR,
|
||||
price: memberRate.requestedPrice?.pricePerStay ?? 0,
|
||||
},
|
||||
local: {
|
||||
currency: memberRate.localPrice.currency,
|
||||
price: memberRate.localPrice.pricePerStay,
|
||||
},
|
||||
}
|
||||
|
||||
if (breakfast) {
|
||||
memberPrice.euro.price = add(
|
||||
memberPrice.euro.price,
|
||||
breakfast.requestedPrice.totalPrice
|
||||
)
|
||||
memberPrice.local.price = add(
|
||||
memberPrice.local.price,
|
||||
breakfast.localPrice.totalPrice
|
||||
)
|
||||
}
|
||||
|
||||
if (packages) {
|
||||
packages.forEach((pkg) => {
|
||||
memberPrice.euro.price = add(
|
||||
memberPrice.euro.price,
|
||||
pkg.requestedPrice.totalPrice
|
||||
)
|
||||
memberPrice.local.price = add(
|
||||
memberPrice.local.price,
|
||||
pkg.localPrice.totalPrice
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
return {
|
||||
roomPrice: {
|
||||
euro: {
|
||||
currency: CurrencyEnum.EUR,
|
||||
price: memberRate.requestedPrice?.pricePerStay ?? 0,
|
||||
},
|
||||
local: {
|
||||
currency: memberRate.localPrice.currency,
|
||||
price: memberRate.localPrice.pricePerStay,
|
||||
},
|
||||
},
|
||||
totalPrice: memberPrice,
|
||||
}
|
||||
}
|
||||
|
||||
export const persistedStateSchema = z
|
||||
.object({
|
||||
bedType: z
|
||||
.object({
|
||||
description: z.string(),
|
||||
roomTypeCode: z.string(),
|
||||
})
|
||||
.optional(),
|
||||
booking: z.object({
|
||||
hotel: z.string(),
|
||||
fromDate: z.string(),
|
||||
toDate: z.string(),
|
||||
rooms: z.array(
|
||||
z.object({
|
||||
adults: z.number().int(),
|
||||
counterRateCode: z.string(),
|
||||
rateCode: z.string(),
|
||||
roomTypeCode: z.string(),
|
||||
children: z
|
||||
.array(
|
||||
z.object({
|
||||
age: z.number().int(),
|
||||
bed: z.string(),
|
||||
})
|
||||
)
|
||||
.optional(),
|
||||
packages: z.array(z.nativeEnum(RoomPackageCodeEnum)).optional(),
|
||||
})
|
||||
),
|
||||
}),
|
||||
breakfast: breakfastPackageSchema.or(z.literal(false)).optional(),
|
||||
guest: z.object({
|
||||
countryCode: z.string().default(""),
|
||||
email: z.string().default(""),
|
||||
firstName: z.string().default(""),
|
||||
lastName: z.string().default(""),
|
||||
membershipNo: z.string().default(""),
|
||||
phoneNumber: z.string().default(""),
|
||||
join: z
|
||||
.boolean()
|
||||
.optional()
|
||||
.transform((_) => false),
|
||||
dateOfBirth: z.string().default(""),
|
||||
zipCode: z.string().default(""),
|
||||
}),
|
||||
totalPrice: z.object({
|
||||
euro: z.object({
|
||||
currency: z.literal(CurrencyEnum.EUR),
|
||||
price: z.number().int(),
|
||||
}),
|
||||
local: z.object({
|
||||
currency: z.nativeEnum(CurrencyEnum),
|
||||
price: z.number().int(),
|
||||
}),
|
||||
}),
|
||||
})
|
||||
.optional()
|
||||
Reference in New Issue
Block a user