Skip to main content

Overview

The deleteParkingSpot endpoint permanently removes a parking spot document from Firestore. This operation cannot be undone.

Endpoint

POST /delete-parking-spot

Authentication

CORS-enabled endpoint with wildcard origin (*). Should be restricted to admin users in production.

Request Body

id
string
required
The unique identifier of the parking spot to delete (e.g., “A-01”, “I-16”). Will be trimmed and converted to uppercase.

Response

success
boolean
Indicates whether the deletion was successful
message
string
Result message: “Puesto eliminado exitosamente”
error
string
Error message (only present on failure)

Success Response

  • 200 OK - Spot deleted successfully
  • 204 No Content - OPTIONS preflight response

Error Responses

  • 400 Bad Request - Missing required parameter id
  • 500 Internal Server Error - Database or server error

Important Notes

This operation is permanent and cannot be undone. The spot document and all its data (including reservation history) will be permanently deleted from Firestore.
  • Firestore’s delete() succeeds even if the document doesn’t exist (idempotent operation)
  • No validation is performed to check if spot has active reservations
  • Historical data in occupancy_history is not affected

Code Example

curl -X POST https://YOUR_REGION-YOUR_PROJECT.cloudfunctions.net/delete-parking-spot \
  -H "Content-Type: application/json" \
  -d '{
    "id": "A-01"
  }'

Success Response Example

{
  "success": true,
  "message": "Puesto A-01 eliminado exitosamente"
}

Error Response Example

Missing ID

{
  "error": "Falta el ID del puesto"
}

Safe Deletion Workflow

For production systems, consider implementing a soft-delete pattern:
// Instead of deleting, mark as inactive
await fetch('/create-parking-spot', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body: JSON.stringify({
    id: 'A-01',
    status: -1, // Custom "deleted" status
    deleted_at: new Date().toISOString(),
    active: false
  })
});
Then filter inactive spots in your queries:
const spots = await fetch('/get-parking-status')
  .then(r => r.json())
  .then(data => data.filter(spot => spot.active !== false));

Bulk Deletion Pattern

For deleting multiple spots:
const spotIds = ['A-01', 'A-02', 'A-03'];

const results = await Promise.all(
  spotIds.map(id =>
    fetch('/delete-parking-spot', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify({ id })
    }).then(r => r.json())
  )
);

const successful = results.filter(r => r.success).length;
console.log(`Deleted ${successful}/${spotIds.length} spots`);

Data Processing

  • ID normalization: Trimmed and converted to uppercase (“a-01” → “A-01”)
  • Idempotent: Calling delete on a non-existent spot returns success
  • No cascade: Related data in other collections is not affected

Pre-Deletion Checklist

Before deleting a spot, consider:
  1. Active Reservations: Check if spot has status === 2
  2. Sensor Integration: Update or remove sensor mappings
  3. Analytics: Export historical data if needed
  4. User Notifications: Alert users with active reservations
  5. Map Updates: Update frontend map displays
// Check before deleting
const spots = await fetch('/get-parking-status').then(r => r.json());
const spot = spots.find(s => s.id === 'A-01');

if (spot.status === 2) {
  console.error('Cannot delete: spot has active reservation');
  // Release reservation first
  await fetch('/release-parking-spot', {
    method: 'POST',
    body: JSON.stringify({ spot_id: 'A-01' })
  });
}

// Now safe to delete
await fetch('/delete-parking-spot', {
  method: 'POST',
  body: JSON.stringify({ id: 'A-01' })
});

Build docs developers (and LLMs) love