# theleadio/lead_crm architecture

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

- Interactive diagram: https://gitdiagram.com/theleadio/lead_crm
- Repository: https://github.com/theleadio/lead_crm

## Overview

This is a Next.js CRM for managing people and companies, sales deals, courses and classes, enrolments, and payments. The main operational flow is staff working in CRM screens, with API routes delegating to domain services that enforce permissions and persist changes in Postgres. Separate capabilities include public lead/schedule handling, duplicate-person review, class notices and seat management, and payment proof handling. The README is a stock Next.js template, so product capabilities are inferred from the source tree; unsampled UI and route wiring is not asserted.

Main entry points: [`lib/sql.ts`](https://github.com/theleadio/lead_crm/blob/main/lib/sql.ts) (Postgres), [`lib/people/service.ts`](https://github.com/theleadio/lead_crm/blob/main/lib/people/service.ts) (People records), [`app/(crm)`](https://github.com/theleadio/lead_crm/tree/main/app/(crm)) (CRM screens), [`lib/auth/permissions.ts`](https://github.com/theleadio/lead_crm/blob/main/lib/auth/permissions.ts) (Role permissions)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_access["Access and API"]
  node_crm_ui["CRM screens"]
  node_auth["Session and identity<br/>[current-user.ts]"]
  node_api_people["People API"]
  node_api_companies["Companies API"]
  node_api_deals["Deals API"]
  node_api_delivery["Delivery APIs"]
  node_api_enrolments["Enrolments API"]
  node_public_handlers["Public lead and schedule handlers<br/>[handlers.ts]"]
end

subgraph group_people["People and Companies"]
  node_people_service["People records<br/>[service.ts]"]
  node_person_detail["Person timeline<br/>[detail-service.ts]"]
  node_duplicate_review["Duplicate review"]
  node_company_service["Company records<br/>[service.ts]"]
end

subgraph group_sales["Sales Pipeline"]
  node_deal_service["Deals and stages<br/>[service.ts]"]
  node_deal_detail["Deal detail<br/>[detail-service.ts]"]
  node_hrdc_tasks["HRDC deadline tasks<br/>[hrdc-task.ts]"]
end

subgraph group_delivery["Courses and Delivery"]
  node_course_service["Courses<br/>[service.ts]"]
  node_class_service["Classes and notices<br/>[service.ts]"]
  node_seats["Seat capacity<br/>[seats.ts]"]
  node_enrolment_service["Enrolments<br/>[service.ts]"]
  node_payment_service["Payments<br/>[service.ts]"]
end

subgraph group_platform["Shared Services"]
  node_database[("Postgres<br/>[sql.ts]")]
  node_audit["Audit log<br/>[audit.ts]"]
  node_permissions["Role permissions<br/>[permissions.ts]"]
end

node_staff(("CRM staff"))

node_staff -.->|"uses"| node_crm_ui
node_crm_ui -.->|"requests"| node_api_people
node_crm_ui -.->|"requests"| node_api_companies
node_crm_ui -.->|"requests"| node_api_deals
node_crm_ui -.->|"requests"| node_api_delivery
node_crm_ui -.->|"requests"| node_api_enrolments
node_api_people -->|"delegates"| node_people_service
node_api_companies -->|"delegates"| node_company_service
node_api_deals -->|"delegates"| node_deal_service
node_api_deals -->|"delegates"| node_deal_detail
node_api_delivery -->|"delegates"| node_class_service
node_api_enrolments -->|"delegates"| node_enrolment_service
node_auth -->|"reads user"| node_database
node_auth -->|"uses roles"| node_permissions
node_people_service -->|"raises review"| node_duplicate_review
node_people_service -->|"reads and writes"| node_database
node_person_detail -->|"uses records"| node_people_service
node_company_service -->|"uses people"| node_people_service
node_deal_detail -->|"syncs deadlines"| node_hrdc_tasks
node_deal_detail -->|"uses deal rules"| node_deal_service
node_class_service -->|"counts seats"| node_seats
node_class_service -->|"reads and writes"| node_database
node_enrolment_service -->|"checks capacity"| node_seats
node_enrolment_service -->|"reads and writes"| node_database
node_payment_service -->|"reads and writes"| node_database
node_deal_service -->|"reads and writes"| node_database
node_course_service -->|"reads and writes"| node_database
node_public_handlers -->|"handles requests"| node_database
node_audit -->|"writes entries"| node_database
node_people_service -->|"records changes"| node_audit
node_company_service -->|"records changes"| node_audit
node_class_service -->|"records changes"| node_audit
node_enrolment_service -->|"records changes"| node_audit
node_people_service -->|"checks access"| node_permissions
node_company_service -->|"checks access"| node_permissions
node_deal_service -->|"checks access"| node_permissions
node_deal_detail -->|"checks access"| node_permissions
node_class_service -->|"checks access"| node_permissions
node_enrolment_service -->|"checks access"| node_permissions

click node_crm_ui "https://github.com/theleadio/lead_crm/tree/main/app/(crm)"
click node_auth "https://github.com/theleadio/lead_crm/blob/main/lib/auth/current-user.ts"
click node_api_people "https://github.com/theleadio/lead_crm/tree/main/app/api/people"
click node_api_companies "https://github.com/theleadio/lead_crm/tree/main/app/api/companies"
click node_api_deals "https://github.com/theleadio/lead_crm/tree/main/app/api/deals"
click node_api_delivery "https://github.com/theleadio/lead_crm/tree/main/app/api/classes"
click node_api_enrolments "https://github.com/theleadio/lead_crm/tree/main/app/api/enrolments"
click node_people_service "https://github.com/theleadio/lead_crm/blob/main/lib/people/service.ts"
click node_person_detail "https://github.com/theleadio/lead_crm/blob/main/lib/people/detail-service.ts"
click node_duplicate_review "https://github.com/theleadio/lead_crm/blob/main/lib/people/duplicate-review.ts"
click node_company_service "https://github.com/theleadio/lead_crm/blob/main/lib/companies/service.ts"
click node_deal_service "https://github.com/theleadio/lead_crm/blob/main/lib/deals/service.ts"
click node_deal_detail "https://github.com/theleadio/lead_crm/blob/main/lib/deals/detail-service.ts"
click node_hrdc_tasks "https://github.com/theleadio/lead_crm/blob/main/lib/deals/hrdc-task.ts"
click node_course_service "https://github.com/theleadio/lead_crm/blob/main/lib/courses/service.ts"
click node_class_service "https://github.com/theleadio/lead_crm/blob/main/lib/classes/service.ts"
click node_seats "https://github.com/theleadio/lead_crm/blob/main/lib/classes/seats.ts"
click node_enrolment_service "https://github.com/theleadio/lead_crm/blob/main/lib/enrolments/service.ts"
click node_payment_service "https://github.com/theleadio/lead_crm/blob/main/lib/payments/service.ts"
click node_public_handlers "https://github.com/theleadio/lead_crm/blob/main/lib/public/handlers.ts"
click node_database "https://github.com/theleadio/lead_crm/blob/main/lib/sql.ts"
click node_audit "https://github.com/theleadio/lead_crm/blob/main/lib/audit.ts"
click node_permissions "https://github.com/theleadio/lead_crm/blob/main/lib/auth/permissions.ts"

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_crm_ui,node_auth,node_api_people,node_api_companies,node_api_deals,node_api_delivery,node_api_enrolments,node_public_handlers toneBlue
class node_people_service,node_person_detail,node_duplicate_review,node_company_service toneAmber
class node_deal_service,node_deal_detail,node_hrdc_tasks toneMint
class node_course_service,node_class_service,node_seats,node_enrolment_service,node_payment_service toneRose
class node_database,node_audit,node_permissions,node_staff toneIndigo
```

## Components

### Access and API

- [CRM screens](https://github.com/theleadio/lead_crm/tree/main/app/(crm)): `app/(crm)`
- [Session and identity](https://github.com/theleadio/lead_crm/blob/main/lib/auth/current-user.ts): `lib/auth/current-user.ts`
- [People API](https://github.com/theleadio/lead_crm/tree/main/app/api/people): `app/api/people`
- [Companies API](https://github.com/theleadio/lead_crm/tree/main/app/api/companies): `app/api/companies`
- [Deals API](https://github.com/theleadio/lead_crm/tree/main/app/api/deals): `app/api/deals`
- [Delivery APIs](https://github.com/theleadio/lead_crm/tree/main/app/api/classes): `app/api/classes`
- [Enrolments API](https://github.com/theleadio/lead_crm/tree/main/app/api/enrolments): `app/api/enrolments`
- [Public lead and schedule handlers](https://github.com/theleadio/lead_crm/blob/main/lib/public/handlers.ts): `lib/public/handlers.ts`

### People and Companies

- [People records](https://github.com/theleadio/lead_crm/blob/main/lib/people/service.ts): `lib/people/service.ts`
- [Person timeline](https://github.com/theleadio/lead_crm/blob/main/lib/people/detail-service.ts): `lib/people/detail-service.ts`
- [Duplicate review](https://github.com/theleadio/lead_crm/blob/main/lib/people/duplicate-review.ts): `lib/people/duplicate-review.ts`
- [Company records](https://github.com/theleadio/lead_crm/blob/main/lib/companies/service.ts): `lib/companies/service.ts`

### Sales Pipeline

- [Deals and stages](https://github.com/theleadio/lead_crm/blob/main/lib/deals/service.ts): `lib/deals/service.ts`
- [Deal detail](https://github.com/theleadio/lead_crm/blob/main/lib/deals/detail-service.ts): `lib/deals/detail-service.ts`
- [HRDC deadline tasks](https://github.com/theleadio/lead_crm/blob/main/lib/deals/hrdc-task.ts): `lib/deals/hrdc-task.ts`

### Courses and Delivery

- [Courses](https://github.com/theleadio/lead_crm/blob/main/lib/courses/service.ts): `lib/courses/service.ts`
- [Classes and notices](https://github.com/theleadio/lead_crm/blob/main/lib/classes/service.ts): `lib/classes/service.ts`
- [Seat capacity](https://github.com/theleadio/lead_crm/blob/main/lib/classes/seats.ts): `lib/classes/seats.ts`
- [Enrolments](https://github.com/theleadio/lead_crm/blob/main/lib/enrolments/service.ts): `lib/enrolments/service.ts`
- [Payments](https://github.com/theleadio/lead_crm/blob/main/lib/payments/service.ts): `lib/payments/service.ts`

### Shared Services

- [Postgres](https://github.com/theleadio/lead_crm/blob/main/lib/sql.ts): `lib/sql.ts`
- [Audit log](https://github.com/theleadio/lead_crm/blob/main/lib/audit.ts): `lib/audit.ts`
- [Role permissions](https://github.com/theleadio/lead_crm/blob/main/lib/auth/permissions.ts): `lib/auth/permissions.ts`

### Other components

- **CRM staff**

## Connections

- CRM staff → CRM screens: uses
- CRM screens → People API: requests
- CRM screens → Companies API: requests
- CRM screens → Deals API: requests
- CRM screens → Delivery APIs: requests
- CRM screens → Enrolments API: requests
- People API → People records: delegates (evidence: `app/api/people/route.ts`)
- Companies API → Company records: delegates (evidence: `app/api/companies/route.ts`)
- Deals API → Deals and stages: delegates (evidence: `app/api/deals/route.ts`)
- Deals API → Deal detail: delegates (evidence: `app/api/deals/route.ts`)
- Delivery APIs → Classes and notices: delegates (evidence: `app/api/classes/route.ts`)
- Enrolments API → Enrolments: delegates (evidence: `app/api/enrolments/route.ts`)
- Session and identity → Postgres: reads user (evidence: `lib/auth/current-user.ts`)
- Session and identity → Role permissions: uses roles (evidence: `lib/auth/current-user.ts`)
- People records → Duplicate review: raises review (evidence: `lib/people/service.ts`)
- People records → Postgres: reads and writes (evidence: `lib/people/service.ts`)
- Person timeline → People records: uses records (evidence: `lib/people/detail-service.ts`)
- Company records → People records: uses people (evidence: `lib/companies/service.ts`)
- Deal detail → HRDC deadline tasks: syncs deadlines (evidence: `lib/deals/detail-service.ts`)
- Deal detail → Deals and stages: uses deal rules (evidence: `lib/deals/detail-service.ts`)
- Classes and notices → Seat capacity: counts seats (evidence: `lib/classes/service.ts`)
- Classes and notices → Postgres: reads and writes (evidence: `lib/classes/service.ts`)
- Enrolments → Seat capacity: checks capacity (evidence: `lib/enrolments/service.ts`)
- Enrolments → Postgres: reads and writes (evidence: `lib/enrolments/service.ts`)
- Payments → Postgres: reads and writes (evidence: `lib/payments/service.ts`)
- Deals and stages → Postgres: reads and writes (evidence: `lib/deals/service.ts`)
- Courses → Postgres: reads and writes (evidence: `lib/courses/service.ts`)
- Public lead and schedule handlers → Postgres: handles requests (evidence: `lib/public/handlers.ts`)
- Audit log → Postgres: writes entries (evidence: `lib/audit.ts`)
- People records → Audit log: records changes (evidence: `lib/people/service.ts`)
- Company records → Audit log: records changes (evidence: `lib/companies/service.ts`)
- Classes and notices → Audit log: records changes (evidence: `lib/classes/service.ts`)
- Enrolments → Audit log: records changes (evidence: `lib/enrolments/service.ts`)
- People records → Role permissions: checks access (evidence: `lib/people/service.ts`)
- Company records → Role permissions: checks access (evidence: `lib/companies/service.ts`)
- Deals and stages → Role permissions: checks access (evidence: `lib/deals/service.ts`)
- Deal detail → Role permissions: checks access (evidence: `lib/deals/detail-service.ts`)
- Classes and notices → Role permissions: checks access (evidence: `lib/classes/service.ts`)
- Enrolments → Role permissions: checks access (evidence: `lib/enrolments/service.ts`)
