Merged in fix(SW-2057)-My-stay-crashes-when-at-least-2-children-has-the-same-bed-type (pull request #1649)

feat(SW-2057) Fixed issues with age:bedType for children and that you couldn't check for availability in MyStay

* feat(SW-2057) Fixed issues with age:bedType for children and that you couldn't check for availability in MyStay


Approved-by: Niclas Edenvin
This commit is contained in:
Pontus Dreij
2025-03-27 07:35:02 +00:00
parent 1429f7ec32
commit af2bbcddc3
4 changed files with 55 additions and 12 deletions

View File

@@ -1,3 +1,5 @@
import { ChildBedTypeEnum } from "@/constants/booking"
export function formatChildBedPreferences({
childrenAges,
childBedPreferences,
@@ -11,13 +13,51 @@ export function formatChildBedPreferences({
}) {
if (childrenAges.length === 0) return ""
const preferences = childrenAges
.map((age, index) => {
const bed = childBedPreferences[index].bedType
if (!bed) return null
return `${age}:${bed}`
})
.filter(Boolean)
// Find the bed types from preferences
const cribBed = childBedPreferences.find(
(pref) => pref.bedType === ChildBedTypeEnum.Crib
)?.bedType
const parentsBed = childBedPreferences.find(
(pref) => pref.bedType === ChildBedTypeEnum.ParentsBed
)?.bedType
const extraBed = childBedPreferences.find(
(pref) => pref.bedType === ChildBedTypeEnum.ExtraBed
)?.bedType
return `[${preferences.join(", ")}]`
// Step 1: Assign cribs to all children <= 2 years
const preferences = childrenAges.map((age, index) => {
if (age <= 2 && cribBed) return `${age}:${cribBed}`
return { age, index }
})
// Filter out children who still need bed assignment
const remainingChildren = preferences.filter(
(pref): pref is { age: number; index: number } => typeof pref === "object"
)
// Step 2: Assign adults bed to one child <= 5 years if available
const hasParentsBed = childBedPreferences.some(
(pref) => pref.bedType === ChildBedTypeEnum.ParentsBed && pref.quantity > 0
)
if (hasParentsBed && parentsBed) {
const youngChildIndex = remainingChildren.findIndex(
(child) => child.age <= 5
)
if (youngChildIndex >= 0) {
const child = remainingChildren[youngChildIndex]
preferences[child.index] = `${child.age}:${parentsBed}`
remainingChildren.splice(youngChildIndex, 1)
}
}
// Step 3: Assign extra beds to remaining children
if (extraBed) {
remainingChildren.forEach(({ age, index }) => {
preferences[index] = `${age}:${extraBed}`
})
}
// Filter out any null values and join the results
return `[${preferences.filter((pref) => typeof pref === "string").join(", ")}]`
}