# codex-mine/sales_pilot architecture

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

- Interactive diagram: https://gitdiagram.com/codex-mine/sales_pilot
- Repository: https://github.com/codex-mine/sales_pilot

## Overview

#### Purpose and topology
SalesPilot is a Phase-1 foundation for a multi-tenant AI SDR SaaS: it currently establishes identity, organization tenancy, API conventions, and a dashboard shell rather than AI or sales automation. It is a monorepo with a Next.js web client and FastAPI backend, containerized for local deployment. Key deployment paths: `docker-compose.yml`, `apps/api/Dockerfile`, `apps/web/Dockerfile`.

#### Runtime entry points
The API starts from `apps/api/app/main.py`, mounting versioned routes through `apps/api/app/api/v1/router.py`; health and auth endpoints live under `/api/v1`. The web runtime is Next.js App Router, rooted at `apps/web/src/app/layout.tsx`, with landing, login, dashboard, and settings pages. API health is the operational liveness entry point: `apps/api/app/api/v1/health.py`.

#### HTTP/API boundary
FastAPI route modules should remain thin: request validation and response schemas are at the edge, while services own application behavior. Auth and organization endpoints are defined in `apps/api/app/api/v1/auth.py` and `apps/api/app/api/v1/organizations.py`; shared API schema/envelope serialization is represented by `apps/api/app/schemas/common.py` and `apps/api/app/schemas/serializers.py`. All externally supplied API data is Pydantic-validated.

#### Identity, sessions, and authorization
Authentication is the central implemented workflow. Routes invoke service-layer orchestration in `apps/api/app/services/auth_service.py` and `apps/api/app/services/session_service.py`, backed by repositories such as `apps/api/app/repositories/user_repository.py` and `apps/api/app/repositories/refresh_token_repository.py`. Short-lived JWT access tokens and hashed opaque refresh tokens form the session boundary; cookie, CSRF, password, device, rate-limit, permission, and token concerns are isolated in `apps/api/app/auth/cookies.py`, `apps/api/app/security/tokens.py`, and `apps/api/app/security/permissions.py`.

#### Tenancy and persistence
PostgreSQL/SQLAlchemy is the system of record. Database initialization/session management is isolated in `apps/api/app/database/base.py` and `apps/api/app/database/session.py`. Domain models establish identity, organizations, CRM, campaign, communication, and AI-adjacent data boundaries under `apps/api/app/models/identity/models.py`, `apps/api/app/models/crm/models.py`, and `apps/api/app/models/campaigns/models.py`. Future data access is intended to be organization-scoped through repositories and dependency injection; organization behavior is concentrated in `apps/api/app/services/organization_service.py`.

#### Schema evolution and asynchronous infrastructure
Alembic owns relational schema changes, with runtime migration setup at `apps/api/alembic/env.py` and versioned migrations in `apps/api/alembic/versions/`. Redis is a material infrastructure dependency configured via `apps/api/app/core/redis.py`; Celery worker initialization in `apps/api/app/workers/celery_app.py` provides the future async-work boundary.

#### Web client flow
The browser application uses a typed HTTP layer (`apps/web/src/api/client.ts`, `apps/web/src/api/interceptors.ts`) and feature-local auth API calls (`apps/web/src/features/auth/api/login.ts`). Authentication state is held in `apps/web/src/store/auth-store.ts`; middleware at `apps/web/src/middleware.ts` is the route-access boundary. User flow is landing → login → dashboard/settings via `apps/web/src/app/page.tsx`, `apps/web/src/app/login/page.tsx`, and `apps/web/src/app/dashboard/page.tsx`. The present dashboard is a UI shell, not a sales-data workflow.

#### Cross-cutting contracts and operations
Configuration is environment-driven at `apps/api/app/core/config.py`, with example environment files at `.env.example` and `apps/web/.env.example`. Shared package directories are reserved extension points rather than active core dependencies: `packages/types`, `packages/ui`, and `packages/config`. CI is defined in `.github/workflows/ci.yml`.

Main entry points: [`apps/api/app/api/v1/router.py`](https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/api/v1/router.py) (V1 route boundary), [`apps/web/src/app/page.tsx`](https://github.com/codex-mine/sales_pilot/blob/main/apps/web/src/app/page.tsx) (Browser user flow), [`apps/api/app/main.py`](https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/main.py) (FastAPI application), [`apps/api/app/services/auth_service.py`](https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/services/auth_service.py) (Auth and session services)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_web["Web client"]
  node_browser["Browser user flow<br/>Next.js pages<br/>[page.tsx]"]
  node_middleware["Route access middleware<br/>Next.js middleware<br/>[middleware.ts]"]
  node_auth_store["Auth state<br/>client store<br/>[auth-store.ts]"]
  node_web_api["Typed API client<br/>HTTP client<br/>[client.ts]"]
  node_login_api["Login API feature<br/>feature client<br/>[login.ts]"]
end

subgraph group_api["API service"]
  node_api_app["FastAPI application<br/>API runtime<br/>[main.py]"]
  node_v1_router["V1 route boundary<br/>FastAPI router<br/>[router.py]"]
  node_auth_routes["Auth endpoints<br/>route module<br/>[auth.py]"]
  node_org_routes["Organization endpoints<br/>route module<br/>[organizations.py]"]
  node_api_contracts["API contracts<br/>Pydantic schemas<br/>[common.py]"]
  node_auth_services["Auth and session services<br/>service layer<br/>[auth_service.py]"]
  node_org_service["Organization service<br/>service layer"]
  node_security{{"Session security<br/>security boundary<br/>[tokens.py]"}}
  node_repositories["Repositories<br/>persistence layer<br/>[user_repository.py]"]
end

subgraph group_data["Data and async"]
  node_database[("PostgreSQL / SQLAlchemy<br/>system of record<br/>[session.py]")]
  node_migrations["Alembic migrations<br/>schema evolution<br/>[env.py]"]
  node_redis[("Redis<br/>infrastructure dependency<br/>[redis.py]")]
  node_celery["Celery worker boundary<br/>async worker<br/>[celery_app.py]"]
end

subgraph group_ops["Delivery"]
  node_containers["Local containers<br/>Docker Compose<br/>[docker-compose.yml]"]
  node_ci["CI pipeline<br/>GitHub Actions<br/>[ci.yml]"]
end

node_browser -->|"route access"| node_middleware
node_browser -->|"reads session state"| node_auth_store
node_browser -->|"submits login"| node_login_api
node_login_api -->|"uses"| node_web_api
node_web_api -->|"HTTPS /api/v1"| node_api_app
node_api_app -->|"mounts"| node_v1_router
node_v1_router -->|"auth routes"| node_auth_routes
node_v1_router -->|"organization routes"| node_org_routes
node_v1_router -->|"validates and serializes"| node_api_contracts
node_auth_routes -->|"delegates"| node_auth_services
node_org_routes -->|"delegates"| node_org_service
node_auth_services -->|"issues and verifies sessions"| node_security
node_auth_services -->|"persists identity"| node_repositories
node_org_service -->|"organization-scoped access"| node_repositories
node_repositories -->|"SQLAlchemy"| node_database
node_migrations -->|"evolves schema"| node_database
node_api_app -->|"uses"| node_redis
node_celery -->|"broker/backend boundary"| node_redis
node_containers -->|"runs API image"| node_api_app
node_containers -->|"runs web image"| node_browser
node_ci -.->|"validates delivery"| node_containers

click node_browser "https://github.com/codex-mine/sales_pilot/blob/main/apps/web/src/app/page.tsx"
click node_middleware "https://github.com/codex-mine/sales_pilot/blob/main/apps/web/src/middleware.ts"
click node_auth_store "https://github.com/codex-mine/sales_pilot/blob/main/apps/web/src/store/auth-store.ts"
click node_web_api "https://github.com/codex-mine/sales_pilot/blob/main/apps/web/src/api/client.ts"
click node_login_api "https://github.com/codex-mine/sales_pilot/blob/main/apps/web/src/features/auth/api/login.ts"
click node_api_app "https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/main.py"
click node_v1_router "https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/api/v1/router.py"
click node_auth_routes "https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/api/v1/auth.py"
click node_org_routes "https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/api/v1/organizations.py"
click node_api_contracts "https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/schemas/common.py"
click node_auth_services "https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/services/auth_service.py"
click node_org_service "https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/services/organization_service.py"
click node_security "https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/security/tokens.py"
click node_repositories "https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/repositories/user_repository.py"
click node_database "https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/database/session.py"
click node_migrations "https://github.com/codex-mine/sales_pilot/blob/main/apps/api/alembic/env.py"
click node_redis "https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/core/redis.py"
click node_celery "https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/workers/celery_app.py"
click node_containers "https://github.com/codex-mine/sales_pilot/blob/main/docker-compose.yml"
click node_ci "https://github.com/codex-mine/sales_pilot/blob/main/.github/workflows/ci.yml"

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_browser,node_middleware,node_auth_store,node_web_api,node_login_api toneBlue
class node_api_app,node_v1_router,node_auth_routes,node_org_routes,node_api_contracts,node_auth_services,node_org_service,node_security,node_repositories toneAmber
class node_database,node_migrations,node_redis,node_celery toneMint
class node_containers,node_ci toneRose
```

## Components

### Web client

- [Browser user flow](https://github.com/codex-mine/sales_pilot/blob/main/apps/web/src/app/page.tsx): Next.js pages · Landing, login, dashboard, and settings shell. · `apps/web/src/app/page.tsx`
- [Route access middleware](https://github.com/codex-mine/sales_pilot/blob/main/apps/web/src/middleware.ts): Next.js middleware · `apps/web/src/middleware.ts`
- [Auth state](https://github.com/codex-mine/sales_pilot/blob/main/apps/web/src/store/auth-store.ts): client store · `apps/web/src/store/auth-store.ts`
- [Typed API client](https://github.com/codex-mine/sales_pilot/blob/main/apps/web/src/api/client.ts): HTTP client · Interceptors centralize browser API behavior. · `apps/web/src/api/client.ts`
- [Login API feature](https://github.com/codex-mine/sales_pilot/blob/main/apps/web/src/features/auth/api/login.ts): feature client · `apps/web/src/features/auth/api/login.ts`

### API service

- [FastAPI application](https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/main.py): API runtime · Mounts the versioned API. · `apps/api/app/main.py`
- [V1 route boundary](https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/api/v1/router.py): FastAPI router · Thin validated HTTP endpoint layer. · `apps/api/app/api/v1/router.py`
- [Auth endpoints](https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/api/v1/auth.py): route module · `apps/api/app/api/v1/auth.py`
- [Organization endpoints](https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/api/v1/organizations.py): route module · `apps/api/app/api/v1/organizations.py`
- [API contracts](https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/schemas/common.py): Pydantic schemas · Validates external data and serializes envelopes. · `apps/api/app/schemas/common.py`
- [Auth and session services](https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/services/auth_service.py): service layer · Orchestrates login, sessions, and token refresh. · `apps/api/app/services/auth_service.py`
- [Organization service](https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/services/organization_service.py): service layer · Tenancy behavior and future organization scoping. · `apps/api/app/services/organization_service.py`
- [Session security](https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/security/tokens.py): security boundary · JWT access tokens, hashed refresh tokens, cookies, CSRF, and permissions. · `apps/api/app/security/tokens.py`
- [Repositories](https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/repositories/user_repository.py): persistence layer · User and refresh-token access boundary. · `apps/api/app/repositories/user_repository.py`

### Data and async

- [PostgreSQL / SQLAlchemy](https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/database/session.py): system of record · Identity, organization, CRM, campaign, communication, and AI-adjacent models. · `apps/api/app/database/session.py`
- [Alembic migrations](https://github.com/codex-mine/sales_pilot/blob/main/apps/api/alembic/env.py): schema evolution · `apps/api/alembic/env.py`
- [Redis](https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/core/redis.py): infrastructure dependency · `apps/api/app/core/redis.py`
- [Celery worker boundary](https://github.com/codex-mine/sales_pilot/blob/main/apps/api/app/workers/celery_app.py): async worker · Reserved boundary for future asynchronous work. · `apps/api/app/workers/celery_app.py`

### Delivery

- [Local containers](https://github.com/codex-mine/sales_pilot/blob/main/docker-compose.yml): Docker Compose · Runs web, API, and supporting services locally. · `docker-compose.yml`
- [CI pipeline](https://github.com/codex-mine/sales_pilot/blob/main/.github/workflows/ci.yml): GitHub Actions · `.github/workflows/ci.yml`

## Connections

- Browser user flow → Route access middleware: route access
- Browser user flow → Auth state: reads session state
- Browser user flow → Login API feature: submits login
- Login API feature → Typed API client: uses
- Typed API client → FastAPI application: HTTPS /api/v1
- FastAPI application → V1 route boundary: mounts
- V1 route boundary → Auth endpoints: auth routes
- V1 route boundary → Organization endpoints: organization routes
- V1 route boundary → API contracts: validates and serializes
- Auth endpoints → Auth and session services: delegates
- Organization endpoints → Organization service: delegates
- Auth and session services → Session security: issues and verifies sessions
- Auth and session services → Repositories: persists identity
- Organization service → Repositories: organization-scoped access
- Repositories → PostgreSQL / SQLAlchemy: SQLAlchemy
- Alembic migrations → PostgreSQL / SQLAlchemy: evolves schema
- FastAPI application → Redis: uses
- Celery worker boundary → Redis: broker/backend boundary
- Local containers → FastAPI application: runs API image
- Local containers → Browser user flow: runs web image
- CI pipeline → Local containers: validates delivery
