# salahadin-hassen/lead-intelligence-automation architecture

> Architecture diagram of the GitHub repository salahadin-hassen/lead-intelligence-automation, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/salahadin-hassen/lead-intelligence-automation
- Repository: https://github.com/salahadin-hassen/lead-intelligence-automation

## Overview

Lead Intelligence Automation accepts and validates B2B leads, stores them in PostgreSQL, scores them with a configurable LLM or local heuristic, and exposes retrieval, status updates, CSV export, deletion, and database-backed health checks. Its documented n8n workflow routes qualification outcomes and records operational events; a demo form submits through that workflow. The graph follows the API-to-database/scoring ownership shown in sampled source. The n8n workflow, demo, and migration details are included from the README and tree; their source was not sampled.

Main entry points: [`app/routes/leads.py`](https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/app/routes/leads.py) (Lead endpoints), [`app/db.py`](https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/app/db.py) (Database access), [`app/main.py`](https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/app/main.py) (App and health), [`app/scoring.py`](https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/app/scoring.py) (Score dispatcher)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API"]
  node_application["App and health<br/>[main.py]"]
  node_lead_routes["Lead endpoints<br/>[leads.py]"]
  node_lead_models["Lead models<br/>[models.py]"]
  node_settings["Settings<br/>[config.py]"]
  node_duplicate_error["Duplicate error<br/>[errors.py]"]
end

subgraph group_scoring["Scoring"]
  node_score_dispatch["Score dispatcher<br/>[scoring.py]"]
  node_llm_scoring["LLM scoring<br/>[scoring.py]"]
  node_heuristic_scoring["Local heuristic<br/>[scoring.py]"]
end

subgraph group_storage["Persistence"]
  node_database_access["Database access<br/>[db.py]"]
  node_postgres[("Lead database")]
  node_migrations["Schema migrations"]
end

subgraph group_automation["Automation"]
  node_demo_form["Demo lead form<br/>[lead-form.html]"]
  node_n8n_workflow["Intake workflow<br/>[lead-intake.json]"]
  node_workflow_events["Decision events<br/>[lead-intake.json]"]
end

node_caller(("API client"))
node_llm_provider{{"LLM provider"}}

node_caller -->|"calls API"| node_application
node_application -->|"registers routes"| node_lead_routes
node_application -->|"loads settings"| node_settings
node_application -->|"starts pool; probes"| node_database_access
node_lead_routes -->|"uses schemas"| node_lead_models
node_lead_routes -->|"reads and writes"| node_database_access
node_lead_routes -->|"maps duplicate"| node_duplicate_error
node_lead_routes -->|"requests score"| node_score_dispatch
node_lead_routes -->|"saves score"| node_database_access
node_database_access -->|"queries"| node_postgres
node_database_access -->|"applies migrations"| node_migrations
node_score_dispatch -->|"reads provider config"| node_settings
node_score_dispatch -->|"tries LLM"| node_llm_scoring
node_score_dispatch -->|"falls back"| node_heuristic_scoring
node_llm_scoring -->|"requests completion"| node_llm_provider
node_demo_form -->|"submits lead"| node_n8n_workflow
node_n8n_workflow -->|"posts lead"| node_lead_routes
node_n8n_workflow -->|"records decisions"| node_workflow_events

click node_application "https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/app/main.py"
click node_lead_routes "https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/app/routes/leads.py"
click node_lead_models "https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/app/models.py"
click node_settings "https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/app/config.py"
click node_duplicate_error "https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/app/errors.py"
click node_database_access "https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/app/db.py"
click node_migrations "https://github.com/salahadin-hassen/lead-intelligence-automation/tree/main/sql"
click node_score_dispatch "https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/app/scoring.py"
click node_llm_scoring "https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/app/scoring.py"
click node_heuristic_scoring "https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/app/scoring.py"
click node_demo_form "https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/demo/lead-form.html"
click node_n8n_workflow "https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/n8n/lead-intake.json"
click node_workflow_events "https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/n8n/lead-intake.json"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_application,node_lead_routes,node_lead_models,node_settings,node_duplicate_error,node_caller toneBlue
class node_score_dispatch,node_llm_scoring,node_heuristic_scoring toneAmber
class node_database_access,node_postgres,node_migrations toneMint
class node_demo_form,node_n8n_workflow,node_workflow_events toneRose
class node_llm_provider toneIndigo
```

## Components

### API

- [App and health](https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/app/main.py): `app/main.py`
- [Lead endpoints](https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/app/routes/leads.py): `app/routes/leads.py`
- [Lead models](https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/app/models.py): `app/models.py`
- [Settings](https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/app/config.py): `app/config.py`
- [Duplicate error](https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/app/errors.py): `app/errors.py`

### Scoring

- [Score dispatcher](https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/app/scoring.py): `app/scoring.py`
- [LLM scoring](https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/app/scoring.py): `app/scoring.py`
- [Local heuristic](https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/app/scoring.py): `app/scoring.py`

### Persistence

- [Database access](https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/app/db.py): `app/db.py`
- **Lead database**
- [Schema migrations](https://github.com/salahadin-hassen/lead-intelligence-automation/tree/main/sql): `sql`

### Automation

- [Demo lead form](https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/demo/lead-form.html): `demo/lead-form.html`
- [Intake workflow](https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/n8n/lead-intake.json): `n8n/lead-intake.json`
- [Decision events](https://github.com/salahadin-hassen/lead-intelligence-automation/blob/main/n8n/lead-intake.json): `n8n/lead-intake.json`

### Other components

- **API client**
- **LLM provider**

## Connections

- API client → App and health: calls API (evidence: `README.md`)
- App and health → Lead endpoints: registers routes (evidence: `app/main.py`)
- App and health → Settings: loads settings (evidence: `app/main.py`)
- App and health → Database access: starts pool; probes (evidence: `app/main.py`)
- Lead endpoints → Lead models: uses schemas (evidence: `app/routes/leads.py`)
- Lead endpoints → Database access: reads and writes (evidence: `app/routes/leads.py`)
- Lead endpoints → Duplicate error: maps duplicate (evidence: `app/routes/leads.py`)
- Lead endpoints → Score dispatcher: requests score (evidence: `app/routes/leads.py`)
- Lead endpoints → Database access: saves score (evidence: `app/routes/leads.py`)
- Database access → Lead database: queries (evidence: `app/db.py`)
- Database access → Schema migrations: applies migrations (evidence: `app/db.py`)
- Score dispatcher → Settings: reads provider config (evidence: `app/scoring.py`)
- Score dispatcher → LLM scoring: tries LLM (evidence: `app/scoring.py`)
- Score dispatcher → Local heuristic: falls back (evidence: `app/scoring.py`)
- LLM scoring → LLM provider: requests completion (evidence: `app/scoring.py`)
- Demo lead form → Intake workflow: submits lead (evidence: `README.md`)
- Intake workflow → Lead endpoints: posts lead (evidence: `README.md`)
- Intake workflow → Decision events: records decisions (evidence: `README.md`)
