Overview
TheCancelarReserva use case handles reservation cancellations with two distinct execution paths:
- Customer cancellation (
ejecutar): Subject to minimum cancellation notice requirements - Administrator cancellation (
ejecutarComoAdmin): No time restrictions, can include cancellation reason
Constructor
Dependencies
Repository for managing reservation data and cancellation operations
Repository for business configuration. Used to get minimum cancellation hours and business details for notifications
Repository for table data. Used to get table names for email notifications
Email service for sending cancellation notifications
ejecutar() Method
Cancels a reservation on behalf of the customer with validation of cancellation policies.Parameters
Unique identifier of the reservation to cancel
Unique identifier of the restaurant business
Return Value
ReturnsFuture<void>. The method completes successfully if cancellation succeeds, or throws an exception if validation fails.
Validation Logic
Performs the following validations:1. Reservation Existence
- Validates that the reservation exists in the system
- Exception:
'Reserva no encontrada'
2. Reservation State
- Only
confirmada(confirmed) orpendiente(pending) reservations can be canceled - Exception:
'Solo se pueden cancelar reservas confirmadas o pendientes.'
3. Past Reservation
- Cannot cancel a reservation whose time has already passed
- Exception:
'No se puede cancelar una reserva cuya hora ya pasó.'
4. Minimum Cancellation Notice
- Must cancel with the configured minimum advance notice (default: 24 hours)
- Exception:
'Solo se puede cancelar con {minHorasParaCancelar} horas de anticipación.'
Email Notifications
Sends cancellation emails to:- Customer who made the reservation
- Restaurant owner (if email configured)
servicioEmail.notificarReservaCanceladaPorCliente().
Usage Example
ejecutarComoAdmin() Method
Cancels a reservation on behalf of the restaurant administrator without time restrictions.Parameters
Unique identifier of the reservation to cancel
Unique identifier of the restaurant business
Reason for cancellation to include in customer notification email
Return Value
ReturnsFuture<void>. The method completes successfully if cancellation succeeds, or throws an exception if the reservation doesn’t exist.
Validation Logic
Performs minimal validation:1. Reservation Existence
- Validates that the reservation exists in the system
- Exception:
'Reserva no encontrada'
- Has no time restrictions (can cancel past reservations)
- Has no state restrictions (can cancel any reservation)
- Has no minimum notice period
Email Notifications
Sends notification email to:- Customer who made the reservation (informing them the restaurant canceled)
servicioEmail.notificarReservaCanceladaPorRestaurante() with optional cancellation reason.
Usage Example
Error Handling
Both methods throwException objects with descriptive Spanish messages. Always use try-catch:
Comparison: Customer vs Admin Cancellation
| Feature | ejecutar() (Customer) | ejecutarComoAdmin() (Admin) |
|---|---|---|
| Time restrictions | Yes (min hours notice) | No |
| State restrictions | Yes (only pending/confirmed) | No |
| Can cancel past reservations | No | Yes |
| Requires reason | No | Optional |
| Email recipient | Customer + Owner | Customer only |
| Email type | Canceled by customer | Canceled by restaurant |
Related Types
- EstadoReserva - Enum for reservation states
- Reserva - Reservation entity