# aadirajput-dev26/ai-legal-system architecture

> Architecture diagram of the GitHub repository aadirajput-dev26/ai-legal-system, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aadirajput-dev26/ai-legal-system
- Repository: https://github.com/aadirajput-dev26/ai-legal-system

## Overview

This is a Fastify legal-practice backend: authenticated users and organisations manage cases, case materials, collaboration, schedules, and billing through an HTTP API. Case routes expose the principal workflow, including case access and transcript processing; case context assembles related records for AI-assisted work. Other branches cover drafting, chat, documents, tools, notifications, legal updates, and hearing reminders. The graph follows explicit route registrations, sampled calls, and SOURCE INDEX references; unsampled subsystems are included where the tree establishes their role, but their internal links are not inferred.

Main entry points: [`src/repositories/case.repository.ts`](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/repositories/case.repository.ts) (Case Records), [`src/app.ts`](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/app.ts) (Fastify API), [`src/services/case-context.service.ts`](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/services/case-context.service.ts) (Case Context), [`src/controllers/billing.controller.ts`](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/controllers/billing.controller.ts) (Billing Controller)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_api["API and Access"]
  node_app["Fastify API<br/>[app.ts]"]
  node_authroutes["Authentication<br/>[auth.routes.ts]"]
  node_auth["Auth Controller<br/>[auth.controller.ts]"]
  node_authenticate["JWT Authentication<br/>[authenticate.ts]"]
  node_caseroutes["Case Routes<br/>[case.routes.ts]"]
  node_billingroutes["Billing Routes<br/>[billing.routes.ts]"]
end

subgraph group_casework["Casework"]
  node_casecontroller["Case Controller<br/>[case.controller.ts]"]
  node_caserepo["Case Records<br/>[case.repository.ts]"]
  node_context["Case Context"]
  node_db[("PostgreSQL Pool<br/>[db.ts]")]
  node_documents["Documents"]
  node_tools["Case Tools<br/>[tool.controller.ts]"]
end

subgraph group_ai["AI and Drafting"]
  node_drafts["Drafting"]
  node_draftservice["Draft Service<br/>[draft.service.ts]"]
  node_chat["Case Chat<br/>[chat.controller.ts]"]
  node_gateway["AI Gateway<br/>[gtwy.service.ts]"]
  node_hippocampus["Document Retrieval<br/>[hippocampus.ts]"]
end

subgraph group_operations["Operations"]
  node_hearingworker["Hearing Reminders<br/>[hearing.worker.ts]"]
  node_hearingrepo["Hearing Records"]
  node_webhook["Notification Webhooks<br/>[webhook.service.ts]"]
  node_legalupdates["Legal Updates"]
end

subgraph group_billing["Billing"]
  node_billingcontroller["Billing Controller"]
  node_credits["Credit Ledger<br/>[credit.service.ts]"]
  node_billingrepo["Billing Records"]
  node_razorpay["Razorpay Payments"]
  node_usage["Usage Metering<br/>[usage.service.ts]"]
end

node_user(("User"))

node_user -->|"sends requests"| node_app
node_app -->|"registers routes"| node_authroutes
node_app -->|"registers routes"| node_caseroutes
node_app -->|"registers routes"| node_billingroutes
node_authroutes -->|"guards requests"| node_authenticate
node_authroutes -->|"dispatches to"| node_auth
node_caseroutes -->|"guards requests"| node_authenticate
node_caseroutes -->|"dispatches to"| node_casecontroller
node_casecontroller -->|"reads cases"| node_caserepo
node_casecontroller -->|"builds context"| node_context
node_context -->|"loads case"| node_caserepo
node_caserepo -->|"queries"| node_db
node_context -->|"loads hearings"| node_hearingrepo
node_context -->|"uses AI gateway"| node_gateway
node_chat -->|"calls"| node_gateway
node_chat -->|"records usage"| node_usage
node_documents -->|"uses"| node_gateway
node_documents -->|"loads case"| node_caserepo
node_tools -->|"loads case"| node_caserepo
node_drafts -->|"delegates to"| node_draftservice
node_drafts -->|"records usage"| node_usage
node_hearingworker -->|"selects and updates"| node_hearingrepo
node_hearingworker -->|"loads case"| node_caserepo
node_hearingworker -->|"sends reminders"| node_webhook
node_billingroutes -->|"dispatches to"| node_billingcontroller
node_billingcontroller -->|"manages credits"| node_credits
node_billingcontroller -->|"creates payments"| node_razorpay
node_billingcontroller -->|"stores billing data"| node_billingrepo
node_usage -->|"checks ledger"| node_credits
node_credits -->|"updates ledger"| node_db

click node_app "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/app.ts"
click node_authroutes "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/routes/auth.routes.ts"
click node_auth "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/controllers/auth.controller.ts"
click node_authenticate "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/middlewares/authenticate.ts"
click node_caseroutes "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/routes/case.routes.ts"
click node_casecontroller "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/controllers/case.controller.ts"
click node_caserepo "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/repositories/case.repository.ts"
click node_context "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/services/case-context.service.ts"
click node_db "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/lib/db.ts"
click node_drafts "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/controllers/draft.controller.ts"
click node_draftservice "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/services/draft.service.ts"
click node_chat "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/controllers/chat.controller.ts"
click node_gateway "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/services/gtwy.service.ts"
click node_hippocampus "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/lib/hippocampus.ts"
click node_documents "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/controllers/document.controller.ts"
click node_tools "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/controllers/tool.controller.ts"
click node_hearingworker "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/workers/hearing.worker.ts"
click node_hearingrepo "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/repositories/hearing.repository.ts"
click node_webhook "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/services/webhook.service.ts"
click node_legalupdates "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/services/legal-update.service.ts"
click node_billingroutes "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/routes/billing.routes.ts"
click node_billingcontroller "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/controllers/billing.controller.ts"
click node_credits "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/services/credit.service.ts"
click node_billingrepo "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/repositories/billing.repository.ts"
click node_razorpay "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/services/razorpay.service.ts"
click node_usage "https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/services/usage.service.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_app,node_authroutes,node_auth,node_authenticate,node_caseroutes,node_billingroutes,node_user toneBlue
class node_casecontroller,node_caserepo,node_context,node_db,node_documents,node_tools toneAmber
class node_drafts,node_draftservice,node_chat,node_gateway,node_hippocampus toneMint
class node_hearingworker,node_hearingrepo,node_webhook,node_legalupdates toneRose
class node_billingcontroller,node_credits,node_billingrepo,node_razorpay,node_usage toneIndigo
```

## Components

### API and Access

- [Fastify API](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/app.ts): `src/app.ts`
- [Authentication](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/routes/auth.routes.ts): `src/routes/auth.routes.ts`
- [Auth Controller](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/controllers/auth.controller.ts): `src/controllers/auth.controller.ts`
- [JWT Authentication](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/middlewares/authenticate.ts): `src/middlewares/authenticate.ts`
- [Case Routes](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/routes/case.routes.ts): `src/routes/case.routes.ts`
- [Billing Routes](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/routes/billing.routes.ts): `src/routes/billing.routes.ts`

### Casework

- [Case Controller](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/controllers/case.controller.ts): `src/controllers/case.controller.ts`
- [Case Records](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/repositories/case.repository.ts): `src/repositories/case.repository.ts`
- [Case Context](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/services/case-context.service.ts): `src/services/case-context.service.ts`
- [PostgreSQL Pool](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/lib/db.ts): `src/lib/db.ts`
- [Documents](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/controllers/document.controller.ts): `src/controllers/document.controller.ts`
- [Case Tools](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/controllers/tool.controller.ts): `src/controllers/tool.controller.ts`

### AI and Drafting

- [Drafting](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/controllers/draft.controller.ts): `src/controllers/draft.controller.ts`
- [Draft Service](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/services/draft.service.ts): `src/services/draft.service.ts`
- [Case Chat](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/controllers/chat.controller.ts): `src/controllers/chat.controller.ts`
- [AI Gateway](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/services/gtwy.service.ts): `src/services/gtwy.service.ts`
- [Document Retrieval](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/lib/hippocampus.ts): `src/lib/hippocampus.ts`

### Operations

- [Hearing Reminders](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/workers/hearing.worker.ts): `src/workers/hearing.worker.ts`
- [Hearing Records](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/repositories/hearing.repository.ts): `src/repositories/hearing.repository.ts`
- [Notification Webhooks](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/services/webhook.service.ts): `src/services/webhook.service.ts`
- [Legal Updates](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/services/legal-update.service.ts): `src/services/legal-update.service.ts`

### Billing

- [Billing Controller](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/controllers/billing.controller.ts): `src/controllers/billing.controller.ts`
- [Credit Ledger](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/services/credit.service.ts): `src/services/credit.service.ts`
- [Billing Records](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/repositories/billing.repository.ts): `src/repositories/billing.repository.ts`
- [Razorpay Payments](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/services/razorpay.service.ts): `src/services/razorpay.service.ts`
- [Usage Metering](https://github.com/aadirajput-dev26/ai-legal-system/blob/main/src/services/usage.service.ts): `src/services/usage.service.ts`

### Other components

- **User**

## Connections

- User → Fastify API: sends requests (evidence: `src/app.ts`)
- Fastify API → Authentication: registers routes (evidence: `src/app.ts`)
- Fastify API → Case Routes: registers routes (evidence: `src/app.ts`)
- Fastify API → Billing Routes: registers routes (evidence: `src/app.ts`)
- Authentication → JWT Authentication: guards requests (evidence: `src/routes/auth.routes.ts`)
- Authentication → Auth Controller: dispatches to (evidence: `src/routes/auth.routes.ts`)
- Case Routes → JWT Authentication: guards requests (evidence: `src/routes/case.routes.ts`)
- Case Routes → Case Controller: dispatches to (evidence: `src/routes/case.routes.ts`)
- Case Controller → Case Records: reads cases (evidence: `src/controllers/case.controller.ts`)
- Case Controller → Case Context: builds context (evidence: `src/controllers/case.controller.ts`)
- Case Context → Case Records: loads case (evidence: `src/services/case-context.service.ts`)
- Case Records → PostgreSQL Pool: queries (evidence: `src/repositories/case.repository.ts`)
- Case Context → Hearing Records: loads hearings (evidence: `src/services/case-context.service.ts`)
- Case Context → AI Gateway: uses AI gateway (evidence: `src/services/case-context.service.ts`)
- Case Chat → AI Gateway: calls (evidence: `src/controllers/chat.controller.ts`)
- Case Chat → Usage Metering: records usage (evidence: `src/controllers/chat.controller.ts`)
- Documents → AI Gateway: uses (evidence: `src/controllers/document.controller.ts`)
- Documents → Case Records: loads case (evidence: `src/controllers/document.controller.ts`)
- Case Tools → Case Records: loads case (evidence: `src/controllers/tool.controller.ts`)
- Drafting → Draft Service: delegates to (evidence: `src/controllers/draft.controller.ts`)
- Drafting → Usage Metering: records usage (evidence: `src/controllers/draft.controller.ts`)
- Hearing Reminders → Hearing Records: selects and updates (evidence: `src/workers/hearing.worker.ts`)
- Hearing Reminders → Case Records: loads case (evidence: `src/workers/hearing.worker.ts`)
- Hearing Reminders → Notification Webhooks: sends reminders (evidence: `src/workers/hearing.worker.ts`)
- Billing Routes → Billing Controller: dispatches to (evidence: `src/routes/billing.routes.ts`)
- Billing Controller → Credit Ledger: manages credits (evidence: `src/controllers/billing.controller.ts`)
- Billing Controller → Razorpay Payments: creates payments (evidence: `src/controllers/billing.controller.ts`)
- Billing Controller → Billing Records: stores billing data (evidence: `src/controllers/billing.controller.ts`)
- Usage Metering → Credit Ledger: checks ledger (evidence: `src/services/usage.service.ts`)
- Credit Ledger → PostgreSQL Pool: updates ledger (evidence: `src/services/credit.service.ts`)
