feat(SW-2863): Move contentstack router to trpc package * Add exports to packages and lint rule to prevent relative imports * Add env to trpc package * Add eslint to trpc package * Apply lint rules * Use direct imports from trpc package * Add lint-staged config to trpc * Move lang enum to common * Restructure trpc package folder structure * WIP first step * update internal imports in trpc * Fix most errors in scandic-web Just 100 left... * Move Props type out of trpc * Fix CategorizedFilters types * Move more schemas in hotel router * Fix deps * fix getNonContentstackUrls * Fix import error * Fix entry error handling * Fix generateMetadata metrics * Fix alertType enum * Fix duplicated types * lint:fix * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package * Fix broken imports * Merge branch 'master' into feat/sw-2863-move-contentstack-router-to-trpc-package Approved-by: Linus Flood
73 lines
2.2 KiB
TypeScript
73 lines
2.2 KiB
TypeScript
import { ChildBedTypeEnum } from "@scandic-hotels/trpc/enums/childBedTypeEnum"
|
|
|
|
import { CancellationRuleEnum } from "@/constants/booking"
|
|
|
|
export function formatChildBedPreferences({
|
|
childrenAges,
|
|
childBedPreferences,
|
|
}: {
|
|
childrenAges: number[]
|
|
childBedPreferences: Array<{
|
|
bedType: string
|
|
quantity: number
|
|
code: string | null
|
|
}>
|
|
}) {
|
|
if (childrenAges.length === 0) return ""
|
|
|
|
// 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
|
|
|
|
// 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(", ")}]`
|
|
}
|
|
|
|
export function hasModifiableRate(cancellationRule: string | null): boolean {
|
|
return (
|
|
cancellationRule === CancellationRuleEnum.CancellableBefore6PM ||
|
|
cancellationRule === CancellationRuleEnum.Changeable
|
|
)
|
|
}
|