Overview
TheCrearReserva use case handles the creation of new restaurant reservations with comprehensive business logic validation. It validates table availability, capacity constraints, business hours, advance booking limits, and optionally sends confirmation emails to both customers and restaurant owners.
Constructor
Dependencies
Repository for managing reservation data persistence and queries
Repository for table data. Used to validate table existence and capacity constraints
Repository for business hours. Used to validate if the restaurant is open at the requested time
Repository for business configuration. Used to get max advance booking days, average duration, and business details
Email service for sending confirmation notifications to customers and restaurant owners
ejecutar() Method
Creates a new reservation with comprehensive validation.Parameters
Unique identifier of the table to reserve
Date for the reservation
Time for the reservation (only hours and minutes are used)
Number of people for the reservation. Must be greater than 0
Customer’s email address for notifications
Customer’s name
Customer’s verified phone number
Initial state of the reservation. Can be
pendiente or confirmadaUnique identifier of the restaurant business
Return Value
Returns aFuture<Reserva> containing the created reservation with a generated ID and all configured properties.
Validation Logic
The use case performs the following validations in order:1. Future Date Validation
- Validates that the reservation date and time are in the future
- Exception:
'La fecha y hora deben ser futuras.'
2. Maximum Advance Booking
- Validates that the reservation is not beyond the configured maximum advance days (default: 14 days)
- Exception:
'Solo se pueden hacer reservas hasta dentro de {maxDiasAnticipacion} días como máximo.'
3. Number of People Validation
- Validates that
numeroPersonasis greater than 0 - Exception:
'El número de personas debe ser mayor a cero.'
4. Table Existence
- If
mesaRepositoriois provided, validates that the table exists - Exception:
'La mesa seleccionada no existe.'
5. Table Capacity
- Validates that the table can accommodate the group using
mesa.puedeAcomodar()business rules:- Table must have enough capacity
- Table cannot be too large (max +3 capacity difference)
- Exceptions:
- Too small:
'La mesa seleccionada tiene capacidad para {capacidad} persona(s), pero has indicado {numeroPersonas} personas. Por favor, selecciona una mesa con mayor capacidad.' - Too large:
'La mesa seleccionada tiene capacidad para {capacidad} personas, pero solo necesitas {numeroPersonas}. La diferencia es de {diferencia} lugares. Por favor, selecciona una mesa más adecuada (máximo +3 lugares de diferencia).'
- Too small:
6. Business Hours
- If
horarioAperturaRepositoriois provided, validates that the restaurant is open at the requested time - Exception: Custom message from
horarioAperturaRepositorio.obtenerMensajeHorarioCerrado()
7. Table Availability
- Validates that the table is not already reserved during the time slot (considering duration)
- Exception:
'La mesa seleccionada ya está reservada en ese horario. Por favor elige otra mesa u otro horario.'
Email Notifications
IfservicioEmail is provided, the use case sends confirmation emails to:
- The customer (if
contactoClienteis provided) - The restaurant owner (if configured in business settings)
Usage Example
Error Handling
All validation errors are thrown asException objects with descriptive Spanish messages. Always wrap the ejecutar() call in a try-catch block:
Related Types
- EstadoReserva - Enum for reservation states
- Reserva - Reservation entity