Skip to main content

createReservation

Creates a new reservation after validating room availability, dates, and guest data.

Function Signature

function createReservation(hotel, roomNumber, guestData, checkIn, checkOut)

Parameters

hotel
object
required
The hotel object where the reservation will be created
roomNumber
number
required
The room number to reserve
guestData
object
required
Guest information object containing:
checkIn
string
required
Check-in date in ISO format (e.g., ‘2026-04-10’)
checkOut
string
required
Check-out date in ISO format (e.g., ‘2026-04-15’)

Validations

The function performs comprehensive validation through validateReservation() (validation.js:63-102):
  1. Room Exists: Verifies the room number exists in the hotel
  2. Room Available: Checks the room is not reserved for the specified dates
  3. Valid Date Range:
    • Check-in must be today or in the future
    • Check-out must be on or after check-in
  4. Maximum Stay: Cannot exceed 14 nights
  5. Guest Data: Validates email, phone, and DNI format

Return Value

reservation
object | undefined
Returns the created reservation object if successful, or undefined if validation fails

Error Cases

When validation fails, the function logs the error message and returns undefined:
  • "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

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('Reservation:', reservation)

Expected Output

{
  id: 'RES-20260410T120000000Z-42',
  roomNumber: 301,
  guest: {
    name: 'Ana García',
    email: '[email protected]',
    phone: '+34 612345678',
    dni: '12345678A'
  },
  checkIn: '2026-04-10',
  checkOut: '2026-04-15',
  nights: 5,
  totalPrice: 500,
  status: 'confirmed',
  extras: []
}
The reservation is immediately added to hotel.reservations array when created successfully.

addExtras

Adds extra services to an existing reservation and recalculates the total price.

Function Signature

function addExtras(hotel, reservationId, extras)

Parameters

hotel
object
required
The hotel object containing the reservation
reservationId
string
required
The unique ID of the reservation (e.g., ‘RES-001’)
extras
array
required
Array of extra service objects. Each extra must contain:

How Pricing Works

  1. Calculate Extras Price: For each extra, multiply price * quantity
  2. Sum All Extras: Add all extras prices together (utils.js:122-127)
  3. Update Total Price: Add extras total to the original room totalPrice (utils.js:114-117)

Return Value

reservation
object | undefined
Returns the updated reservation object with extras added and price recalculated, or undefined if reservation doesn’t exist

Error Cases

If the reservation doesn’t exist, logs "La reserva no existe, no se pueden añadir extras" and returns undefined

Example

const updatedReservation = addExtras(hotel, 'RES-001', [
  { name: 'Breakfast', price: 10, quantity: 5 },
  { name: 'Parking', price: 15, quantity: 5 }
])

console.log('Reserva con extras añadidos', updatedReservation)

Expected Output

{
  id: 'RES-001',
  roomNumber: 301,
  guest: { /* guest data */ },
  checkIn: '2026-04-10',
  checkOut: '2026-04-15',
  nights: 5,
  totalPrice: 625,  // Original 500 + (10*5) + (15*5) = 625
  status: 'confirmed',
  extras: [
    { name: 'Breakfast', price: 10, quantity: 5 },
    { name: 'Parking', price: 15, quantity: 5 }
  ]
}
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

// Original reservation: 5 nights × €100/night = €500
// Breakfast: €10 × 5 days = €50
// Parking: €15 × 5 days = €75
// New total: €500 + €50 + €75 = €625
  • 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)

Build docs developers (and LLMs) love