createReservation
Creates a new reservation after validating room availability, dates, and guest data.Function Signature
Parameters
The hotel object where the reservation will be created
The room number to reserve
Guest information object containing:
Check-in date in ISO format (e.g., ‘2026-04-10’)
Check-out date in ISO format (e.g., ‘2026-04-15’)
Validations
The function performs comprehensive validation throughvalidateReservation() (validation.js:63-102):
- Room Exists: Verifies the room number exists in the hotel
- Room Available: Checks the room is not reserved for the specified dates
- Valid Date Range:
- Check-in must be today or in the future
- Check-out must be on or after check-in
- Maximum Stay: Cannot exceed 14 nights
- Guest Data: Validates email, phone, and DNI format
Return Value
Returns the created reservation object if successful, or
undefined if validation failsError Cases
When validation fails, the function logs the error message and returnsundefined:
"La habitación número {roomNumber} no existe."- Room doesn’t exist"La habitación número {roomNumber} está reservada para esas fechas."- Room already reserved"El rango de fechas no es válido."- Invalid date range"Solo se puede reservar hasta 14 noches."- Exceeds maximum stay"Los datos del huesped no son válidos"- Invalid guest data
Example
Expected Output
addExtras
Adds extra services to an existing reservation and recalculates the total price.Function Signature
Parameters
The hotel object containing the reservation
The unique ID of the reservation (e.g., ‘RES-001’)
Array of extra service objects. Each extra must contain:
How Pricing Works
- Calculate Extras Price: For each extra, multiply
price * quantity - Sum All Extras: Add all extras prices together (utils.js:122-127)
- Update Total Price: Add extras total to the original room
totalPrice(utils.js:114-117)
Return Value
Returns the updated reservation object with extras added and price recalculated, or
undefined if reservation doesn’t existError Cases
Example
Expected Output
Extras are appended to the existing
extras array using spread operator, so you can call this function multiple times to add more extras.Calculation Example
Related Functions
getReservationById()- Finds the reservation by ID (utils.js:132-137)getExtrasPrice()- Calculates total extras price (utils.js:122-127)updateTotalPriceWithExtras()- Updates reservation total (utils.js:114-117)