# Process Fire Incidents Function

Orchestrates the complete fire alert notification workflow.

# Endpoint

POST /functions/v1/process-fire-incidents

# Description

This function:

  1. Finds unprocessed fire incidents
  2. Looks up building owner information via Actovia API
  3. Sends email and SMS notifications to building owners
  4. Marks incidents as processed
  5. Logs all notification attempts

# Request

No parameters required - typically called by cron scheduler.

POST /functions/v1/process-fire-incidents
Authorization: Bearer YOUR_SERVICE_ROLE_KEY
Content-Type: application/json

{}

# Response

Success Response:

{
  "success": true,
  "processed": 5,
  "notifications_sent": {
    "email": 5,
    "sms": 3
  },
  "errors": []
}

Error Response:

{
  "success": false,
  "error": "Processing failed",
  "processed": 2,
  "errors": [
    "Failed to get owner for 123 Main St",
    "SMS sending failed for incident 2025-001"
  ]
}

# Workflow Steps

# 1. Fetch Unprocessed Incidents

SELECT * FROM fire_incidents 
WHERE processed = false 
ORDER BY incident_date DESC 
LIMIT 10;

# 2. Get Building Owner (per incident)

Calls get-building-owner-actovia function:

{
  "address": "123 Main St",
  "city": "New York",
  "state": "NY"
}

Returns:

{
  "name": "John Owner",
  "email": "owner@example.com",
  "phone": "+1234567890",
  "company": "ABC Properties LLC"
}

# 3. Send Notifications

Calls send-fire-alert-notification function:

{
  "incident": {
    "address": "123 Main St",
    "city": "New York",
    "incident_date": "2025-01-22",
    "incident_type": "FIRE"
  },
  "owner": {
    "name": "John Owner",
    "email": "owner@example.com",
    "phone": "+1234567890"
  }
}

# 4. Update Incident Status

UPDATE fire_incidents 
SET processed = true, 
    processed_at = NOW() 
WHERE id = 123;

# 5. Log Results

INSERT INTO alert_logs (
  fire_incident_id,
  alert_type,
  recipient,
  status,
  sent_at,
  building_owner_info
) VALUES (
  123,
  'email',
  'owner@example.com',
  'sent',
  NOW(),
  '{"name": "John Owner", ...}'
);

# Environment Variables

Required:

  • SUPABASE_URL - Supabase project URL
  • SUPABASE_SERVICE_ROLE_KEY - Service role key
  • All environment variables for sub-functions

# Cron Schedule

Configured in Supabase dashboard:

SELECT cron.schedule(
  'process-fire-incidents',
  '*/5 * * * *', -- Every 5 minutes
  $$
  SELECT net.http_post(
    'https://edknwrcztqwhskjpoxta.supabase.co/functions/v1/process-fire-incidents',
    headers => jsonb_build_object(
      'Authorization', 'Bearer ' || current_setting('app.settings.service_role_key')
    )
  );
  $$
);

# Error Handling

The function:

  • Continues processing even if individual incidents fail
  • Logs all errors to alert_logs table
  • Returns summary of successes and failures
  • Implements retry logic for transient failures

# Rate Limiting

  • Processes maximum 10 incidents per run
  • 5-minute interval between runs
  • Prevents overwhelming external APIs
  • Ensures timely notification delivery

# Monitoring

Check processing status:

-- Unprocessed incidents
SELECT COUNT(*) FROM fire_incidents WHERE processed = false;

-- Recent alerts
SELECT * FROM alert_logs 
ORDER BY created_at DESC 
LIMIT 20;

-- Failed notifications
SELECT * FROM alert_logs 
WHERE status = 'failed' 
ORDER BY created_at DESC;