Overview
ThereserveParkingSpot endpoint creates a time-limited reservation on an available parking spot. It uses Firestore transactions to prevent race conditions and ensure only one reservation can be made at a time.
Endpoint
Authentication
CORS-enabled endpoint. Should be protected with user authentication in production.Request Body
The unique identifier of the parking spot to reserve (e.g., “A-01”, “I-16”)
Vehicle license plate number for the reservation
Duration of the reservation in minutes. Will be converted to an expiration timestamp.
Response
Indicates whether the reservation was successful
Success message: “Reserva realizada con éxito”
Error message (only present on failure)
Success Response
- 200 OK - Reservation created successfully
Error Responses
- 400 Bad Request - Missing required parameters (
spot_id,license_plate, orduration_minutes) - 405 Method Not Allowed - Request method is not POST
- 409 Conflict - Spot cannot be reserved due to:
- Spot does not exist
- Spot is already occupied by a vehicle
- Spot already has an active reservation
Transaction Logic
The function uses Firestore transactions to ensure atomic operations:- Read: Fetch current spot data within transaction
- Validate: Check spot exists and status is not
0(Occupied) or2(Reserved) - Calculate: Compute expiration time = current time +
duration_minutes - Write: Update spot to status
2with reservation metadata - Commit: Transaction commits only if no conflicts occurred
Code Example
Success Response Example
Error Response Examples
Missing Parameters
Spot Already Occupied
Spot Already Reserved
Spot Does Not Exist
Firestore Data Structure
After successful reservation, the spot document is updated:Integration with Sensor Data
When a vehicle with a reservation arrives:- Sensor detects vehicle and sends status
0(Occupied) via Ingest Parking Data - Ingest function recognizes reserved spot and confirms arrival
- Status changes from
2(Reserved) to0(Occupied) - Reservation metadata is deleted to save storage
Related Endpoints
- Release Parking Spot - Cancel an active reservation
- Get Parking Status - Check which spots are available
- Ingest Parking Data - Handle sensor updates for reserved spots