Skip to main content

Endpoint

curl --request POST \
  --url 'https://api.example.com/flota/sync' \
  --header 'Authorization: Bearer <access_token>'

Authentication

Authorization
string
required
Bearer token for authentication. Format: Bearer <access_token>

Description

This endpoint synchronizes vehicles from the areas_placas table by:
  1. Retrieving all license plates with status ACTIVADA (activated)
  2. Checking which plates already have corresponding vehicle records
  3. Creating new vehicle records for plates that don’t exist in the vehiculo table
  4. Returning a summary of the sync operation
The sync process respects the unique constraint on placa_id in the vehiculo table, preventing duplicate vehicle records for the same license plate.

Request Body

This endpoint does not require a request body.

Response

Returns a summary object with sync statistics.
message
string
Success message indicating the sync operation completed
total_active_plates
number
Total number of active license plates found in the system
newly_synced
number
Number of new vehicle records created during this sync operation
already_existing
number
Number of plates that already had corresponding vehicle records before the sync

Response Example

Successful Sync

{
  "message": "Sync successful",
  "total_active_plates": 150,
  "newly_synced": 12,
  "already_existing": 138
}

No Active Plates

When there are no active plates to sync:
{
  "message": "No active plates found"
}

Examples

Perform vehicle sync

curl --request POST \
  --url 'https://api.example.com/flota/sync' \
  --header 'Authorization: Bearer <access_token>'

Error Responses

401 Unauthorized

{
  "error": "Unauthorized",
  "details": "Invalid or missing access token"
}

500 Internal Server Error

{
  "error": "Internal Server Error",
  "details": "Supabase client not initialized in request"
}

Sync Process Details

Step 1: Retrieve Active Plates

The endpoint queries the areas_placas table for all plates with estado = 'ACTIVADA':
SELECT id, placa, estado
FROM areas_placas
WHERE estado = 'ACTIVADA'

Step 2: Check Existing Vehicles

Retrieves all existing vehicle records to identify which plates already have vehicles:
SELECT placa_id
FROM vehiculo

Step 3: Filter and Insert

Filters out plates that already have vehicle records and inserts new records for the remaining plates:
INSERT INTO vehiculo (placa_id)
VALUES (...)

Step 4: Return Summary

Provides a summary showing:
  • How many active plates exist in total
  • How many new vehicles were created
  • How many already existed

Use Cases

Initial Fleet Setup

When first setting up the fleet management system, sync all active plates to create vehicle records:
curl --request POST \
  --url 'https://api.example.com/flota/sync' \
  --header 'Authorization: Bearer <access_token>'
Expected result for initial setup:
{
  "message": "Sync successful",
  "total_active_plates": 150,
  "newly_synced": 150,
  "already_existing": 0
}

Regular Maintenance

Run periodically to ensure new activated plates get vehicle records:
curl --request POST \
  --url 'https://api.example.com/flota/sync' \
  --header 'Authorization: Bearer <access_token>'
Expected result:
{
  "message": "Sync successful",
  "total_active_plates": 155,
  "newly_synced": 5,
  "already_existing": 150
}

Notes

  • The sync operation is idempotent - running it multiple times will not create duplicate records
  • Only plates with status ACTIVADA are considered for sync
  • The unique constraint vehiculo_placa_id_key on the placa_id column prevents duplicate vehicle records
  • Newly created vehicle records will have empresa_id and operacion_id as null initially
  • After syncing, you should update vehicle records with company and operation information using the update endpoint (if available)
  • The operation uses a Set data structure to efficiently filter out existing plates before insertion

Build docs developers (and LLMs) love