Overview
TheReservaRepositorio defines the contract for reservation management operations, including creating, updating, canceling reservations, and checking table availability. The Firestore implementation (ReservaRepositorioFirestore) provides persistent storage using Cloud Firestore.
Interface
lib/dominio/repositorios/reserva_repositorio.dart
Methods
crearReserva
Creates a new reservation in the system.The reservation object to create. The ID will be generated automatically.
Returns the created reservation with its assigned ID from Firestore.
- Converts the
Reservaentity to a Firestore-compatible map - Adds timestamps (
createdAt,updatedAt) using server timestamp - Returns a copy of the reservation with the generated document ID
Example Usage
obtenerReserva
Retrieves all reservations from the system, ordered by date and time (most recent first).Returns a list of all reservations, sorted by
fechaHora in descending order. Returns an empty list if there’s an error.Example Usage
cancelarReserva
Cancels an existing reservation by updating its status to “cancelada”.The ID of the reservation to cancel.
Completes when the reservation has been canceled. Throws an exception if the operation fails.
- Updates only the
estadofield to ‘cancelada’ - Updates the
updatedAttimestamp - Does not delete the reservation document (soft delete)
Example Usage
actualizarReserva
Updates an existing reservation’s details.The reservation object with updated information. Must include a valid ID.
Completes when the update is successful. Throws an exception on failure.
estado- Reservation statusnumeroPersonas- Number of guestscontactoCliente- Client contact emailnombreCliente- Client nameupdatedAt- Timestamp (auto-updated)
Example Usage
obtenerReservaPorId
Retrieves a specific reservation by its ID.The unique identifier of the reservation.
Returns the reservation if found, or
null if it doesn’t exist or there’s an error.Example Usage
obtenerReservasPorMesaYHorario
Retrieves all active reservations (confirmed or pending) for a specific table on a given date.The ID of the table to check.
The date to check (time component is ignored, checks entire day).
The time to check (currently used for context, but queries entire day).
Returns all non-canceled reservations for the table on the specified date.
- Queries reservations from midnight to midnight of the specified date
- Filters out canceled reservations (
estado != EstadoReserva.cancelada) - Uses Firestore composite queries on
mesaIdandfechaHora
Example Usage
mesaDisponible
Checks if a table is available for a specific time slot, considering the duration of the reservation and detecting time collisions with existing reservations.The ID of the table to check.
The date of the potential reservation.
The start time of the potential reservation.
The duration of the reservation in minutes.
Returns
true if the table is available, false if there’s a time collision with existing reservations.- Calculates the time range for the new reservation:
[hora, hora + duracionMinutos] - Retrieves all active reservations for the table on that date
- For each existing reservation, calculates its time range:
[fechaHora, horaFin] - Checks if intervals overlap:
horaInicioNueva < horaFinExistente AND horaFinNueva > horaInicioExistente - Returns
falseif any collision is detected,trueotherwise
Example Usage
obtenerReservasPorTelefonoYNegocio
Retrieves all reservations made by a specific phone number at a particular restaurant.The client’s phone number (must match exactly).
The restaurant’s unique identifier.
Returns all reservations for the phone number at that restaurant, ordered by date (most recent first).
- Display client’s reservation history
- Verify client identity for modifications
- Check for duplicate reservations
Example Usage
Firestore Implementation
Collection Structure
Reservations are stored in thereservas collection with the following schema:
Firestore Document
Estado Enum Mapping
| Dart Enum | Firestore String |
|---|---|
EstadoReserva.pendiente | "pendiente" |
EstadoReserva.confirmada | "confirmada" |
EstadoReserva.cancelada | "cancelada" |
Required Firestore Indexes
For optimal query performance, create these composite indexes:-
Mesa and Date Range Query
- Collection:
reservas - Fields:
mesaId(Ascending),fechaHora(Ascending)
- Collection:
-
Phone and Business Query
- Collection:
reservas - Fields:
telefonoCliente(Ascending),negocioId(Ascending),fechaHora(Descending)
- Collection:
Related Types
Reserva Entity
See Also
- MesaRepositorio - Table management operations
- NegocioRepositorio - Restaurant management
- HorarioAperturaRepositorio - Business hours validation