The JS Mini Hotel system implements a three-tier cancellation policy based on how far in advance a guest cancels their reservation. This guide explains the policy, penalty calculations, and the cancellation process.
Three-Tier Cancellation Policy
The cancellation penalty depends on the number of days between the cancellation date and the check-in date:
More than 7 days No charge Free cancellation if you cancel more than 7 days before check-in.
3-7 days 50% penalty Half of the total reservation price is charged.
Less than 3 days 100% penalty Full reservation price is charged.
Penalty Calculation Logic
The cancellation charge is calculated using the getCancellationCharge() function:
export function getCancellationCharge ( reservation ) {
const now = Temporal . Now . plainDateISO ()
if ( reservation === undefined ) {
return
}
const totalPrice = reservation . totalPrice
const duration = now . until ( Temporal . PlainDate . from ( reservation . checkIn ))
if ( duration . days < 3 ) {
return totalPrice
} else if ( duration . days < 8 ) {
return 0.5 * totalPrice
} else {
return 0
}
}
How It Works
Get current date
The function captures the current date using Temporal.Now.plainDateISO()
Calculate days until check-in
It calculates the duration between now and the check-in date: const duration = now . until ( Temporal . PlainDate . from ( reservation . checkIn ))
Apply policy tiers
Based on duration.days, it applies the appropriate penalty:
< 3 days : Return full price (totalPrice)
3-7 days : Return half price (0.5 * totalPrice)
> 7 days : Return zero (0)
The totalPrice includes both the room charges and any extras that were added to the reservation.
Cancelling a Reservation
To cancel a reservation, use the cancelReservation() function:
function cancelReservation ( hotel , reservationId ) {
const reservation = getReservationById ( hotel , reservationId )
const cancellationCharge = getCancellationCharge ( reservation )
if ( ! reservation ) {
return 'La reserva no existe, no se puede cancelar'
}
const room = getRoomByNumber ( hotel , reservation . roomNumber )
reservation . status = RESERVATION_STATUS . CANCELLED
room . status = ROOM_STATUS . AVAILABLE
archiveReservation ( hotel , reservation )
return `Cancelación realizada con una penalización de ${ cancellationCharge } €`
}
What Happens During Cancellation
Reservation status changes
The reservation status is updated to 'cancelled': reservation . status = RESERVATION_STATUS . CANCELLED
The room status is reset to 'available': room . status = ROOM_STATUS . AVAILABLE
This makes the room available for new reservations.
The reservation is moved from active reservations to the history: export function archiveReservation ( hotel , reservation ) {
hotel . history . push ( reservation )
deleteReservation ( hotel , reservation )
}
This maintains a record of cancelled reservations for reporting purposes.
Penalty is calculated and returned
The function calculates the cancellation charge and returns a confirmation message: return `Cancelación realizada con una penalización de ${ cancellationCharge } €`
Cancellation Scenarios
Let’s walk through different cancellation scenarios with real examples:
Scenario 1: Early Cancellation (No Penalty)
Setup
Cancellation
Calculation
// Today is March 3, 2026
// Reservation check-in: April 10, 2026
// Days until check-in: 38 days
const reservation = {
id: 'RES-001' ,
roomNumber: 301 ,
guest: { name: 'Ana García' , email: '[email protected] ' },
checkIn: '2026-04-10' ,
checkOut: '2026-04-15' ,
nights: 5 ,
totalPrice: 750 ,
status: 'confirmed' ,
extras: []
}
Scenario 2: Medium Notice (50% Penalty)
Setup
Cancellation
Calculation
// Today is April 5, 2026
// Reservation check-in: April 10, 2026
// Days until check-in: 5 days
const reservation = {
id: 'RES-002' ,
roomNumber: 201 ,
checkIn: '2026-04-10' ,
checkOut: '2026-04-12' ,
totalPrice: 160 , // 2 nights × €80
status: 'confirmed'
}
Scenario 3: Late Cancellation (100% Penalty)
Setup
Cancellation
Calculation
// Today is April 9, 2026
// Reservation check-in: April 10, 2026
// Days until check-in: 1 day
const reservation = {
id: 'RES-003' ,
roomNumber: 101 ,
checkIn: '2026-04-10' ,
checkOut: '2026-04-13' ,
totalPrice: 150 , // 3 nights × €50
status: 'confirmed'
}
Setup
Cancellation
Calculation
// Today is April 7, 2026
// Reservation check-in: April 10, 2026
// Days until check-in: 3 days
const reservation = {
id: 'RES-004' ,
roomNumber: 301 ,
checkIn: '2026-04-10' ,
checkOut: '2026-04-15' ,
nights: 5 ,
totalPrice: 875 , // €750 room + €125 extras
status: 'confirmed' ,
extras: [
{ name: 'Breakfast' , price: 10 , quantity: 5 },
{ name: 'Parking' , price: 15 , quantity: 5 }
]
}
The penalty is calculated on the total price , which includes both room charges and extras. Guests cannot get a refund on extras while cancelling the room.
Error Handling
The cancellation function handles invalid reservation IDs:
const result = cancelReservation ( hotel , 'RES-INVALID' )
console . log ( result )
// "La reserva no existe, no se puede cancelar"
Policy Boundary Cases
Pay special attention to these boundary cases:
Exactly 3 days
Exactly 7 days
Exactly 8 days
Cancelling exactly 3 days before check-in: // duration.days = 3
// Condition: duration.days < 8 is true (50% penalty)
// Result: 50% penalty applied
Cancelling exactly 7 days before check-in: // duration.days = 7
// Condition: duration.days < 8 is true (50% penalty)
// Result: 50% penalty applied
Cancelling exactly 8 days before check-in: // duration.days = 8
// Condition: duration.days < 8 is false
// Goes to else block (no penalty)
// Result: No penalty
The policy is: < 3 days = 100%, 3-7 days = 50%, ≥ 8 days = 0%
Complete Cancellation Example
Here’s a complete example with logging:
// Original example from source code
console . log ( 'Reserva cancelada' , cancelReservation ( hotel , 'RES-001' ))
// 1. Create a reservation
const reservation = createReservation (
hotel ,
301 ,
{
name: 'Ana García' ,
email: '[email protected] ' ,
phone: '+34 612345678' ,
dni: '12345678A'
},
'2026-04-10' ,
'2026-04-15'
)
console . log ( 'Created:' , reservation . id )
console . log ( 'Total price:' , reservation . totalPrice )
// 2. Add extras
addExtras ( hotel , reservation . id , [
{ name: 'Breakfast' , price: 10 , quantity: 5 }
])
console . log ( 'Price with extras:' , reservation . totalPrice )
// 3. Cancel the reservation
const cancellationMessage = cancelReservation ( hotel , reservation . id )
console . log ( cancellationMessage )
// 4. Verify cancellation
const cancelledReservation = getReservationById ( hotel , reservation . id )
console . log ( 'Found in active reservations:' , cancelledReservation )
// undefined (moved to history)
Checking Cancellation Charge Before Cancelling
You can check the cancellation charge without actually cancelling:
const reservation = getReservationById ( hotel , 'RES-001' )
const charge = getCancellationCharge ( reservation )
if ( charge === 0 ) {
console . log ( 'Free cancellation available' )
cancelReservation ( hotel , 'RES-001' )
} else {
console . log ( `Cancellation will cost € ${ charge } ` )
// Ask user for confirmation before cancelling
}
Next Steps
Creating Reservations Learn how to create reservations that can be cancelled
Generating Reports View cancelled reservations in guest history reports