enflow-api / API_DOCUMENTATION.md
dhruv575
Fixed oai initialization
73e99ed

Enflow API Documentation

This document provides details about all the endpoints available in the Enflow API.

Base URL

For local development: http://localhost:5000 For production: Your HuggingFace space URL

Authentication

Most endpoints require authentication via JWT token. To authenticate:

  1. Get a token by calling the login endpoint
  2. Include the token in your requests in the Authorization header:
    Authorization: Bearer <your_jwt_token>
    

Authentication Endpoints

Login

POST /api/auth/login

Request Body:

{
  "email": "user@example.com",
  "password": "your_password"
}

Response:

{
  "message": "Login successful",
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...",
  "user": {
    "_id": "60d21b4967d0d8992e610c85",
    "email": "user@example.com",
    "name": "John Doe",
    "permissions": "Admin",
    "position": "Officer",
    "department_id": "60d21b4967d0d8992e610c85",
    "logs": [],
    "incidents": []
  }
}

Get Current User

GET /api/auth/me

Response:

{
  "user": {
    "_id": "60d21b4967d0d8992e610c85",
    "email": "user@example.com",
    "name": "John Doe",
    "permissions": "Admin",
    "position": "Officer",
    "department_id": "60d21b4967d0d8992e610c85",
    "logs": [],
    "incidents": []
  }
}

Update Password

PUT /api/auth/password

Request Body:

{
  "current_password": "your_current_password",
  "new_password": "your_new_password"
}

Response:

{
  "message": "Password updated successfully"
}

Update Profile

PUT /api/auth/profile

Request Body:

{
  "name": "New Name",
  "position": "New Position"
}

Response:

{
  "message": "Profile updated successfully",
  "user": {
    // Updated user object
  }
}

Reset Password (Admin Only)

POST /api/auth/reset-password

Request Body:

{
  "user_id": "60d21b4967d0d8992e610c85",
  "new_password": "new_password" // Optional, a random password will be generated if not provided
}

Response:

{
  "message": "Password reset successfully",
  "user": {
    // User object
  },
  "new_password": "random_password" // Included only if a random password was generated
}

Department Endpoints

Create Department

POST /api/departments

Request Body:

{
  "name": "Police Department",
  "address": "123 Main St, City, State, ZIP",
  "website": "https://example.com",
  "admin_email": "admin@example.com",
  "admin_name": "Admin User",
  "admin_password": "admin_password",
  "admin_position": "Administrator"
}

Response:

{
  "message": "Department and admin user created successfully",
  "department": {
    "_id": "60d21b4967d0d8992e610c85",
    "name": "Police Department",
    "address": "123 Main St, City, State, ZIP",
    "website": "https://example.com",
    "members": ["60d21b4967d0d8992e610c86"],
    "workflows": []
  },
  "admin_user": {
    "_id": "60d21b4967d0d8992e610c86",
    "email": "admin@example.com",
    "name": "Admin User",
    "permissions": "Admin",
    "position": "Administrator",
    "department_id": "60d21b4967d0d8992e610c85"
  }
}

Get All Departments

GET /api/departments

Response:

{
  "departments": [
    {
      "_id": "60d21b4967d0d8992e610c85",
      "name": "Police Department",
      "address": "123 Main St, City, State, ZIP",
      "website": "https://example.com",
      "members": ["60d21b4967d0d8992e610c86"],
      "workflows": []
    }
  ]
}

Get Department by ID

GET /api/departments/:department_id

Response:

{
  "department": {
    "_id": "60d21b4967d0d8992e610c85",
    "name": "Police Department",
    "address": "123 Main St, City, State, ZIP",
    "website": "https://example.com",
    "members": ["60d21b4967d0d8992e610c86"],
    "workflows": []
  }
}

Update Department (Admin Only)

PUT /api/departments/:department_id

Request Body:

{
  "name": "Updated Department Name",
  "address": "New Address",
  "website": "https://new-website.com"
}

Response:

{
  "message": "Department updated successfully",
  "department": {
    // Updated department object
  }
}

Delete Department (Admin Only)

DELETE /api/departments/:department_id

Response:

{
  "message": "Department and all its users deleted successfully"
}

Get Department Members

GET /api/departments/:department_id/members

Response:

{
  "members": [
    {
      "_id": "60d21b4967d0d8992e610c86",
      "email": "admin@example.com",
      "name": "Admin User",
      "permissions": "Admin",
      "position": "Administrator"
    }
  ]
}

Add Member (Admin Only)

POST /api/departments/:department_id/members

Request Body:

{
  "email": "new_member@example.com",
  "name": "New Member",
  "position": "Officer",
  "permissions": "User"
}

Response:

{
  "message": "Member added successfully",
  "user": {
    // New user object
  },
  "raw_password": "random_password" // Password for the new user
}

Add Members via CSV (Admin Only)

POST /api/departments/:department_id/members/csv

Request: Multipart form data with a CSV file containing member information.

Response:

{
  "message": "Processed 5 new users with 0 errors",
  "new_users": [
    // Array of new user objects, each containing a raw_password
  ],
  "errors": [
    // Array of error messages, if any
  ]
}

Remove Member (Admin Only)

DELETE /api/departments/:department_id/members/:user_id

Response:

{
  "message": "Member removed successfully"
}

Update Member Permissions (Admin Only)

PUT /api/departments/:department_id/members/:user_id/permissions

Request Body:

{
  "permissions": "Admin" // Or "User"
}

Response:

{
  "message": "Member permissions updated successfully",
  "user": {
    // Updated user object
  }
}

Workflow Endpoints

Get All Department Workflows

GET /api/workflows

Response:

{
  "workflows": [
    {
      "_id": "60d21b4967d0d8992e610c87",
      "title": "Traffic Violation Workflow",
      "description": "Process for handling traffic violations",
      "department_id": "60d21b4967d0d8992e610c85",
      "data_requirements": [
        {
          "field": "license_plate",
          "description": "Vehicle license plate number"
        }
      ],
      "form_fields": [],
      "raw_forms": []
    }
  ]
}

Create Workflow (Admin Only)

POST /api/workflows

Request Body:

{
  "title": "Traffic Violation Workflow",
  "description": "Process for handling traffic violations",
  "data_requirements": [
    {
      "field": "license_plate",
      "description": "Vehicle license plate number"
    }
  ],
  "form_fields": []
}

Response:

{
  "message": "Workflow created successfully",
  "workflow": {
    // New workflow object
  }
}

Get Workflow by ID

GET /api/workflows/:workflow_id

Response:

{
  "workflow": {
    "_id": "60d21b4967d0d8992e610c87",
    "title": "Traffic Violation Workflow",
    "description": "Process for handling traffic violations",
    "department_id": "60d21b4967d0d8992e610c85",
    "data_requirements": [
      {
        "field": "license_plate",
        "description": "Vehicle license plate number"
      }
    ],
    "form_fields": [],
    "raw_forms": []
  }
}

Update Workflow (Admin Only)

PUT /api/workflows/:workflow_id

Request Body:

{
  "title": "Updated Workflow Title",
  "description": "Updated description",
  "data_requirements": [
    // Updated data requirements
  ],
  "form_fields": [
    // Updated form fields
  ]
}

Response:

{
  "message": "Workflow updated successfully",
  "workflow": {
    // Updated workflow object
  }
}

Delete Workflow (Admin Only)

DELETE /api/workflows/:workflow_id

Response:

{
  "message": "Workflow deleted successfully"
}

Add Data Requirement (Admin Only)

POST /api/workflows/:workflow_id/data-requirements

Request Body:

{
  "field": "driver_license",
  "description": "Driver's license number"
}

Response:

{
  "message": "Data requirement added successfully",
  "workflow": {
    // Updated workflow object
  }
}

Remove Data Requirement (Admin Only)

DELETE /api/workflows/:workflow_id/data-requirements

Request Body:

{
  "index": 0
}

Response:

{
  "message": "Data requirement removed successfully",
  "workflow": {
    // Updated workflow object
  }
}

Upload Form (Admin Only)

POST /api/workflows/:workflow_id/forms

Request: Multipart form data with a markdown template file.

Response:

{
  "message": "Markdown template uploaded successfully",
  "workflow": {
    "_id": "60d21b4967d0d8992e610c87",
    "title": "Traffic Violation Workflow",
    "description": "Process for handling traffic violations",
    "department_id": "60d21b4967d0d8992e610c85",
    "data_requirements": [
      {
        "field": "license_plate",
        "description": "Vehicle license plate number"
      }
    ],
    "markdown_template": "# Traffic Violation Report\n\nDate: {{date}}\nLicense Plate: {{license_plate}}\n\n## Officer Notes\n\n{{notes}}",
    "template_name": "traffic_violation_template.md"
  }
}

Remove Form (Admin Only)

DELETE /api/workflows/:workflow_id/forms

Response:

{
  "message": "Template removed successfully",
  "workflow": {
    // Updated workflow object with markdown_template set to null
  }
}

Add Form Field (Admin Only)

POST /api/workflows/:workflow_id/form-fields

Request Body:

{
  "position": {"x": 100, "y": 100},
  "field_name": "license_plate"
}

Response:

{
  "message": "Form field added successfully",
  "workflow": {
    // Updated workflow object
  }
}

Remove Form Field (Admin Only)

DELETE /api/workflows/:workflow_id/form-fields

Request Body:

{
  "index": 0
}

Response:

{
  "message": "Form field removed successfully",
  "workflow": {
    // Updated workflow object
  }
}

Log Endpoints

Upload Log

POST /api/logs

Request: Multipart form data with:

  • file: PDF file of the log
  • log_date: Date in YYYY-MM-DD format

Response:

{
  "message": "Log uploaded and processed successfully",
  "log": {
    "_id": "60d21b4967d0d8992e610c88",
    "user_id": "60d21b4967d0d8992e610c86",
    "department_id": "60d21b4967d0d8992e610c85",
    "log_date": "2023-05-15",
    "log_text": "Extracted text content from the PDF",
    "incidents": []
  },
  "incidents_created": 2
}

Classify Log Activities

POST /api/logs/classify

Request: Multipart form data with:

  • file: PDF file of the log

Response:

{
  "message": "Log activities extracted and classified",
  "activities": [
    {
      "activity": "Traffic stop on Main Street",
      "text": "Conducted traffic stop at 100 Main St. Driver had expired license.",
      "time": "14:30",
      "location": "100 Main St"
    }
  ],
  "classified_activities": [
    {
      "activity": {
        "activity": "Traffic stop on Main Street",
        "text": "Conducted traffic stop at 100 Main St. Driver had expired license.",
        "time": "14:30",
        "location": "100 Main St"
      },
      "workflow_id": "60d21b4967d0d8992e610c87",
      "workflow_title": "Traffic Violation Workflow",
      "classified": true
    }
  ],
  "workflows": [
    {
      "id": "60d21b4967d0d8992e610c87",
      "title": "Traffic Violation Workflow",
      "description": "Process for handling traffic violations"
    }
  ],
  "extracted_text": "Extracted text content from the PDF"
}

Get User Logs

GET /api/logs/user

Response:

{
  "logs": [
    {
      "_id": "60d21b4967d0d8992e610c88",
      "user_id": "60d21b4967d0d8992e610c86",
      "department_id": "60d21b4967d0d8992e610c85",
      "log_date": "2023-05-15",
      "log_file": "https://res.cloudinary.com/...",
      "incidents": []
    }
  ]
}

Get Department Logs (Admin Only)

GET /api/logs/department

Response:

{
  "logs": [
    // Array of all logs in the department
  ]
}

Get Logs by Date Range

POST /api/logs/date-range

Request Body:

{
  "start_date": "2023-05-01",
  "end_date": "2023-05-31"
}

Response:

{
  "logs": [
    // Array of logs within the date range
  ]
}

Get Log by ID

GET /api/logs/:log_id

Response:

{
  "log": {
    "_id": "60d21b4967d0d8992e610c88",
    "user_id": "60d21b4967d0d8992e610c86",
    "department_id": "60d21b4967d0d8992e610c85",
    "log_date": "2023-05-15",
    "log_file": "https://res.cloudinary.com/...",
    "incidents": []
  }
}

Delete Log

DELETE /api/logs/:log_id

Response:

{
  "message": "Log and associated incidents deleted successfully"
}

Incident Endpoints

Get User Incidents

GET /api/incidents/user

Response:

{
  "incidents": [
    {
      "_id": "60d21b4967d0d8992e610c89",
      "department_id": "60d21b4967d0d8992e610c85",
      "user_id": "60d21b4967d0d8992e610c86",
      "workflow_id": "60d21b4967d0d8992e610c87",
      "description": "Traffic violation at Main St",
      "date": "2023-05-15",
      "log_id": "60d21b4967d0d8992e610c88",
      "activity_text": "Observed vehicle speeding...",
      "status": "completed",
      "filled_forms": [
        {
          "url": "/api/logs/files/60d21b4967d0d8992e610c8a",
          "filename": "Traffic_Violation_Workflow_incident_60d21b4967d0d8992e610c89",
          "original_template": "traffic_violation_template.md"
        }
      ],
      "extracted_data": {
        "license_plate": "ABC123",
        "driver_name": "John Doe",
        "violation": "Speeding"
      }
    }
  ]
}

Process Incident (Admin Only)

POST /api/incidents/:incident_id/process

Response:

{
  "message": "Incident processed successfully",
  "incident": {
    // Updated incident object with filled_markdown and other details
  },
  "filled_markdown": "# Traffic Violation Report\n\nDate: 2023-05-15\nLicense Plate: ABC123\n\n## Officer Notes\n\nObserved vehicle speeding on Main St."
}

Get Department Incidents (Admin Only)

GET /api/incidents/department

Response:

{
  "incidents": [
    // Array of all incidents in the department
  ]
}

Get Incidents by Date Range

POST /api/incidents/date-range

Request Body:

{
  "start_date": "2023-05-01",
  "end_date": "2023-05-31"
}

Response:

{
  "incidents": [
    // Array of incidents within the date range
  ]
}

Get Workflow Incidents

GET /api/incidents/workflow/:workflow_id

Response:

{
  "incidents": [
    // Array of incidents for the specified workflow
  ]
}

Get Incident by ID

GET /api/incidents/:incident_id

Response:

{
  "incident": {
    "_id": "60d21b4967d0d8992e610c89",
    "department_id": "60d21b4967d0d8992e610c85",
    "user_id": "60d21b4967d0d8992e610c86",
    "workflow_id": "60d21b4967d0d8992e610c87",
    "description": "Traffic violation at Main St",
    "date": "2023-05-15",
    "log_id": "60d21b4967d0d8992e610c88",
    "activity_text": "Observed vehicle speeding...",
    "status": "completed",
    "filled_forms": [],
    "extracted_data": {
      "license_plate": "ABC123"
    }
  }
}

Delete Incident

DELETE /api/incidents/:incident_id

Response:

{
  "message": "Incident deleted successfully"
}

User Endpoints

Create User (Admin Only)

POST /api/users

Request Body:

{
  "email": "new_user@example.com",
  "name": "New User",
  "password": "password123",
  "position": "Officer",
  "permissions": "User",
  "department_id": "60d21b4967d0d8992e610c85"
}

Response:

{
  "message": "User created successfully",
  "user": {
    "_id": "60d21b4967d0d8992e610c89",
    "email": "new_user@example.com",
    "name": "New User",
    "permissions": "User",
    "position": "Officer",
    "department_id": "60d21b4967d0d8992e610c85",
    "logs": [],
    "incidents": []
  }
}

Create Multiple Users (Admin Only)

POST /api/users/bulk

Request Body:

[
  {
    "email": "user1@example.com",
    "name": "User One",
    "password": "password123",
    "position": "Officer",
    "permissions": "User",
    "department_id": "60d21b4967d0d8992e610c85"
  },
  {
    "email": "user2@example.com",
    "name": "User Two",
    "password": "password456",
    "position": "Detective",
    "permissions": "Admin",
    "department_id": "60d21b4967d0d8992e610c85"
  }
]

Response:

{
  "message": "Created 2 users with 0 errors",
  "users": [
    // Array of created user objects
  ],
  "errors": []
}

Get All Users (Admin Only)

GET /api/users

Response:

{
  "users": [
    {
      "_id": "60d21b4967d0d8992e610c89",
      "email": "new_user@example.com",
      "name": "New User",
      "permissions": "User",
      "position": "Officer",
      "department_id": "60d21b4967d0d8992e610c85",
      "logs": [],
      "incidents": []
    }
    // Other users...
  ]
}

Get User by ID

GET /api/users/:user_id

Response:

{
  "user": {
    "_id": "60d21b4967d0d8992e610c89",
    "email": "new_user@example.com",
    "name": "New User",
    "permissions": "User",
    "position": "Officer",
    "department_id": "60d21b4967d0d8992e610c85",
    "logs": [],
    "incidents": []
  }
}

Update User (Admin Only)

PUT /api/users/:user_id

Request Body:

{
  "name": "Updated Name",
  "position": "Detective",
  "permissions": "Admin",
  "department_id": "60d21b4967d0d8992e610c86" // Only include if transferring to new department
}

Response:

{
  "message": "User updated successfully",
  "user": {
    // Updated user object
  }
}

Delete User (Admin Only)

DELETE /api/users/:user_id

Response:

{
  "message": "User deleted successfully"
}

Utility Endpoints

Health Check

GET /health

Response:

{
  "status": "healthy",
  "mongo": "connected",
  "env_vars": "ok",
  "upload_dir": "/tmp/uploads",
  "log_dir": "/tmp/logs"
}

API Test

GET /api/test

Response:

{
  "message": "API test successful",
  "timestamp": "2023-05-15T12:00:00.000Z",
  "environment": "development"
}