Merged in fix/allow-single-rateCode (pull request #1438)
fix: allow rates that only have either of member or public to be selectable * fix: allow rates that only have either of member or public to be selectable Approved-by: Michael Zetterberg
This commit is contained in:
committed by
Linus Flood
parent
3f01266a75
commit
c3e3fa62ec
@@ -105,7 +105,7 @@ function everyRateHasBreakfastIncluded(
|
||||
userType: "member" | "public"
|
||||
) {
|
||||
const rateDefinition = rateDefinitions.find(
|
||||
(rd) => rd.rateCode === product.productType[userType]?.rateCode
|
||||
(rd) => rd.rateCode === product[userType]?.rateCode
|
||||
)
|
||||
if (!rateDefinition) {
|
||||
return false
|
||||
@@ -113,10 +113,7 @@ function everyRateHasBreakfastIncluded(
|
||||
return rateDefinition.breakfastIncluded
|
||||
}
|
||||
|
||||
function getRate(rate: RateDefinition | undefined) {
|
||||
if (!rate) {
|
||||
return null
|
||||
}
|
||||
function getRate(rate: RateDefinition) {
|
||||
switch (rate.cancellationRule) {
|
||||
case "CancellableBefore6PM":
|
||||
return "flex"
|
||||
@@ -156,77 +153,79 @@ export const roomsAvailabilitySchema = z
|
||||
type: z.string().optional(),
|
||||
}),
|
||||
})
|
||||
.transform((o) => {
|
||||
const cancellationRuleLookup = o.data.attributes.rateDefinitions.reduce(
|
||||
(acc, val) => {
|
||||
// @ts-expect-error - index of cancellationRule TS
|
||||
acc[val.rateCode] = cancellationRules[val.cancellationRule]
|
||||
return acc
|
||||
},
|
||||
{}
|
||||
)
|
||||
.transform(({ data: { attributes } }) => {
|
||||
const rateDefinitions = attributes.rateDefinitions
|
||||
const cancellationRuleLookup = rateDefinitions.reduce((acc, val) => {
|
||||
// @ts-expect-error - index of cancellationRule TS
|
||||
acc[val.rateCode] = cancellationRules[val.cancellationRule]
|
||||
return acc
|
||||
}, {})
|
||||
|
||||
o.data.attributes.roomConfigurations =
|
||||
o.data.attributes.roomConfigurations.map((room) => {
|
||||
attributes.roomConfigurations = attributes.roomConfigurations.map(
|
||||
(room) => {
|
||||
if (room.products.length) {
|
||||
room.breakfastIncludedInAllRatesMember = room.products.every(
|
||||
(product) =>
|
||||
everyRateHasBreakfastIncluded(
|
||||
product,
|
||||
o.data.attributes.rateDefinitions,
|
||||
"member"
|
||||
)
|
||||
everyRateHasBreakfastIncluded(product, rateDefinitions, "member")
|
||||
)
|
||||
room.breakfastIncludedInAllRatesPublic = room.products.every(
|
||||
(product) =>
|
||||
everyRateHasBreakfastIncluded(
|
||||
product,
|
||||
o.data.attributes.rateDefinitions,
|
||||
"public"
|
||||
)
|
||||
everyRateHasBreakfastIncluded(product, rateDefinitions, "public")
|
||||
)
|
||||
|
||||
room.products = room.products.map((product) => {
|
||||
const publicRateDefinition = o.data.attributes.rateDefinitions.find(
|
||||
(rate) =>
|
||||
product.productType.public.rateCode
|
||||
? rate.rateCode === product.productType.public.rateCode
|
||||
: rate.rateCode === product.productType.public.oldRateCode
|
||||
)
|
||||
const publicRate = getRate(publicRateDefinition)
|
||||
const memberRateDefinition = o.data.attributes.rateDefinitions.find(
|
||||
(rate) =>
|
||||
product.productType.member?.rateCode
|
||||
? rate.rateCode === product.productType.member?.rateCode
|
||||
: rate.rateCode === product.productType.member?.oldRateCode
|
||||
)
|
||||
const memberRate = getRate(memberRateDefinition)
|
||||
|
||||
if (publicRate) {
|
||||
product.productType.public.rate = publicRate
|
||||
const publicRate = product.public
|
||||
if (publicRate?.rateCode) {
|
||||
const publicRateDefinition = rateDefinitions.find(
|
||||
(rateDefinition) =>
|
||||
rateDefinition.rateCode === publicRate.rateCode
|
||||
)
|
||||
if (publicRateDefinition) {
|
||||
const rate = getRate(publicRateDefinition)
|
||||
if (rate) {
|
||||
product.rate = rate
|
||||
if (rate === "flex") {
|
||||
product.isFlex = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (memberRate && product.productType.member) {
|
||||
product.productType.member.rate = memberRate
|
||||
|
||||
const memberRate = product.member
|
||||
if (memberRate?.rateCode) {
|
||||
const memberRateDefinition = rateDefinitions.find(
|
||||
(rate) => rate.rateCode === memberRate.rateCode
|
||||
)
|
||||
if (memberRateDefinition) {
|
||||
const rate = getRate(memberRateDefinition)
|
||||
if (rate) {
|
||||
product.rate = rate
|
||||
if (rate === "flex") {
|
||||
product.isFlex = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return product
|
||||
})
|
||||
|
||||
// CancellationRule is the same for public and member per product
|
||||
// Sorting to guarantee order based on rate
|
||||
room.products = room.products.sort(
|
||||
(a, b) =>
|
||||
// @ts-expect-error - index
|
||||
cancellationRuleLookup[a.public?.rateCode || a.member?.rateCode] -
|
||||
// @ts-expect-error - index
|
||||
cancellationRuleLookup[b.public?.rateCode || b.member?.rateCode]
|
||||
)
|
||||
}
|
||||
|
||||
// CancellationRule is the same for public and member per product
|
||||
// Sorting to guarantee order based on rate
|
||||
room.products = room.products.sort(
|
||||
(a, b) =>
|
||||
// @ts-expect-error - index
|
||||
cancellationRuleLookup[a.productType.public.rateCode] -
|
||||
// @ts-expect-error - index
|
||||
cancellationRuleLookup[b.productType.public.rateCode]
|
||||
)
|
||||
|
||||
return room
|
||||
})
|
||||
}
|
||||
)
|
||||
|
||||
return o.data.attributes
|
||||
return attributes
|
||||
})
|
||||
|
||||
export const ratesSchema = z.array(rateSchema)
|
||||
|
||||
Reference in New Issue
Block a user