# stevesdiary/crm architecture

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

- Interactive diagram: https://gitdiagram.com/stevesdiary/crm
- Repository: https://github.com/stevesdiary/crm

## Overview

#### Purpose and executable surface
This repository is the backend of a multi-tenant CRM SaaS, implemented in NestJS/TypeScript. It exposes versioned REST APIs, JWT-based access control, health endpoints, and WebSocket updates. The runtime bootstraps from `src/main.ts`, composes features in `src/app.module.ts`, and persists CRM data through Prisma.

#### Application composition
Nest feature modules follow a controller → service → Prisma pattern. Controllers own HTTP boundaries; services implement domain operations and database access. Core CRM domains include accounts, contacts, leads, opportunities, activities, tasks, tickets, products, notifications, health metrics, analytics, and files. Representative paths: `src/contacts/contacts.module.ts`, `src/leads/leads.service.ts`, `src/opportunities/opportunities.controller.ts`.

#### Request security and tenancy
Authentication is centered on JWT issuance/validation and route guards. Tenant context and role enforcement form the main isolation boundary before domain services execute. Shared middleware, exception translation, validation pipes, and DTO/schema validation standardize inbound request handling. Paths: `src/auth/auth.module.ts`, `src/auth/jwt-auth.guard.ts`, `src/auth/tenant.guard.ts`, `src/common/guards/roles.guard.ts`, `src/common/middleware/security.middleware.ts`.

#### Persistence and data model
PostgreSQL is the system of record, accessed solely through the application Prisma provider. The schema and ordered migrations define tenant-aware CRM entities, status evolution, and file metadata; domain services should depend on `PrismaService` rather than instantiate database clients. Paths: `prisma/schema.prisma`, `prisma/migrations/20251115152753_init/migration.sql`, `src/prisma/prisma.service.ts`.

#### Files and external integration boundary
File API/service logic records and serves document metadata while shared storage abstraction separates the CRM from the storage provider. Environment configuration indicates Backblaze B2 storage, plus SendGrid, Twilio, Redis, and encryption credentials; concrete integration implementations are not visible in the tree. Paths: `src/files/files.controller.ts`, `src/files/files.service.ts`, `src/common/services/storage.service.ts`, `.env.example`.

#### Real-time, observability, and operations
A dedicated Nest WebSocket gateway provides the real-time transport boundary, likely for notifications and live CRM changes. Health and metrics modules expose operational state. Prometheus scrapes metrics, Grafana visualizes them, and Loki/Promtail aggregate logs. Paths: `src/websocket/websocket.gateway.ts`, `src/health/health.controller.ts`, `src/health-metrics/health-metrics.service.ts`, `monitoring/prometheus.yml`, `monitoring/docker-compose.monitoring.yml`.

#### Deployment infrastructure
The service is containerized with Docker and deployable through GitHub Actions. Terraform provisions AWS-oriented platform dependencies: network, RDS/PostgreSQL, Redis, S3, encryption, and monitoring. CI/CD and deployment scripts are the release boundary, with database migrations expected during deployment. Paths: `Dockerfile`, `docker-compose.yml`, `.github/workflows/ci.yml`, `terraform/main.tf`, `terraform/modules/rds/main.tf`.

Main entry points: [`src/app.module.ts`](https://github.com/stevesdiary/crm/blob/main/src/app.module.ts) (Application Module), [`src/opportunities/opportunities.controller.ts`](https://github.com/stevesdiary/crm/blob/main/src/opportunities/opportunities.controller.ts) (Versioned REST API), [`src/prisma/prisma.service.ts`](https://github.com/stevesdiary/crm/blob/main/src/prisma/prisma.service.ts) (Prisma Provider), [`src/leads/leads.service.ts`](https://github.com/stevesdiary/crm/blob/main/src/leads/leads.service.ts) (CRM Domain Services)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["NestJS Runtime"]
  node_bootstrap["Application Bootstrap<br/>NestJS entrypoint<br/>[main.ts]"]
  node_app_module["Application Module<br/>NestJS composition<br/>[app.module.ts]"]
  node_http_api["Versioned REST API<br/>HTTP controllers"]
  node_request_security{{"Auth, Tenant &amp; Roles<br/>guards<br/>[jwt-auth.guard.ts]"}}
  node_request_standardization["Request Standardization<br/>middleware &amp; validation"]
  node_auth["Authentication<br/>auth module<br/>[auth.module.ts]"]
  node_crm_domains["CRM Domain Services<br/>NestJS services<br/>[leads.service.ts]"]
  node_contacts_module["Contacts Module<br/>feature module<br/>[contacts.module.ts]"]
  node_files_api["Files API &amp; Service<br/>file feature<br/>[files.service.ts]"]
  node_websocket["WebSocket Gateway<br/>real-time transport"]
  node_health_metrics["Health &amp; Metrics<br/>operations endpoints"]
end

subgraph group_data["Data &amp; Integrations"]
  node_prisma["Prisma Provider<br/>database access<br/>[prisma.service.ts]"]
  node_crm_schema["Tenant-aware CRM Schema<br/>Prisma schema<br/>[schema.prisma]"]
  node_postgres[("PostgreSQL<br/>system of record")]
  node_migrations["Ordered Migrations<br/>database migrations<br/>[migration.sql]"]
  node_storage["Storage Abstraction<br/>shared service<br/>[storage.service.ts]"]
  node_external_services{{"Configured External Services<br/>provider boundary<br/>[.env.example]"}}
end

subgraph group_operations["Delivery &amp; Operations"]
  node_container_release["Container &amp; CI Release<br/>build and deployment<br/>[ci.yml]"]
  node_terraform["AWS Platform Provisioning<br/>Terraform<br/>[main.tf]"]
  node_monitoring["Monitoring Stack<br/>Prometheus Grafana Loki"]
end

node_bootstrap -->|"boots"| node_app_module
node_app_module -->|"registers"| node_http_api
node_app_module -->|"registers"| node_auth
node_app_module -->|"registers"| node_contacts_module
node_app_module -->|"registers"| node_websocket
node_app_module -->|"registers"| node_health_metrics
node_request_standardization -->|"normalizes requests"| node_http_api
node_http_api -->|"enforces before handling"| node_request_security
node_request_security -->|"validates JWTs"| node_auth
node_http_api -->|"invokes"| node_crm_domains
node_contacts_module -->|"contains domain flow"| node_crm_domains
node_http_api -->|"routes file requests"| node_files_api
node_crm_domains -->|"persists through"| node_prisma
node_files_api -->|"stores metadata through"| node_prisma
node_files_api -->|"uses"| node_storage
node_storage -.->|"provider boundary"| node_external_services
node_prisma -->|"maps"| node_crm_schema
node_prisma -->|"accesses"| node_postgres
node_migrations -->|"evolves"| node_postgres
node_websocket -.->|"publishes live changes from"| node_crm_domains
node_health_metrics -->|"exposes metrics to"| node_monitoring
node_container_release -->|"deploys runtime"| node_bootstrap
node_container_release -.->|"runs during deployment"| node_migrations
node_terraform -->|"provisions RDS"| node_postgres
node_terraform -.->|"provisions platform dependencies"| node_external_services

click node_bootstrap "https://github.com/stevesdiary/crm/blob/main/src/main.ts"
click node_app_module "https://github.com/stevesdiary/crm/blob/main/src/app.module.ts"
click node_http_api "https://github.com/stevesdiary/crm/blob/main/src/opportunities/opportunities.controller.ts"
click node_request_security "https://github.com/stevesdiary/crm/blob/main/src/auth/jwt-auth.guard.ts"
click node_request_standardization "https://github.com/stevesdiary/crm/blob/main/src/common/middleware/security.middleware.ts"
click node_auth "https://github.com/stevesdiary/crm/blob/main/src/auth/auth.module.ts"
click node_crm_domains "https://github.com/stevesdiary/crm/blob/main/src/leads/leads.service.ts"
click node_contacts_module "https://github.com/stevesdiary/crm/blob/main/src/contacts/contacts.module.ts"
click node_files_api "https://github.com/stevesdiary/crm/blob/main/src/files/files.service.ts"
click node_websocket "https://github.com/stevesdiary/crm/blob/main/src/websocket/websocket.gateway.ts"
click node_health_metrics "https://github.com/stevesdiary/crm/blob/main/src/health-metrics/health-metrics.service.ts"
click node_prisma "https://github.com/stevesdiary/crm/blob/main/src/prisma/prisma.service.ts"
click node_crm_schema "https://github.com/stevesdiary/crm/blob/main/prisma/schema.prisma"
click node_migrations "https://github.com/stevesdiary/crm/blob/main/prisma/migrations/20251115152753_init/migration.sql"
click node_storage "https://github.com/stevesdiary/crm/blob/main/src/common/services/storage.service.ts"
click node_external_services "https://github.com/stevesdiary/crm/blob/main/.env.example"
click node_container_release "https://github.com/stevesdiary/crm/blob/main/.github/workflows/ci.yml"
click node_terraform "https://github.com/stevesdiary/crm/blob/main/terraform/main.tf"
click node_monitoring "https://github.com/stevesdiary/crm/blob/main/monitoring/docker-compose.monitoring.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_bootstrap,node_app_module,node_http_api,node_request_security,node_request_standardization,node_auth,node_crm_domains,node_contacts_module,node_files_api,node_websocket,node_health_metrics toneBlue
class node_prisma,node_crm_schema,node_postgres,node_migrations,node_storage,node_external_services toneAmber
class node_container_release,node_terraform,node_monitoring toneMint
```

## Components

### NestJS Runtime

- [Application Bootstrap](https://github.com/stevesdiary/crm/blob/main/src/main.ts): NestJS entrypoint · Starts the HTTP application and global request pipeline. · `src/main.ts`
- [Application Module](https://github.com/stevesdiary/crm/blob/main/src/app.module.ts): NestJS composition · Composes feature and infrastructure modules. · `src/app.module.ts`
- [Versioned REST API](https://github.com/stevesdiary/crm/blob/main/src/opportunities/opportunities.controller.ts): HTTP controllers · CRM controllers expose the public request boundary. · `src/opportunities/opportunities.controller.ts`
- [Auth, Tenant & Roles](https://github.com/stevesdiary/crm/blob/main/src/auth/jwt-auth.guard.ts): guards · JWT validation, tenant isolation, and role authorization run before domain work. · `src/auth/jwt-auth.guard.ts`
- [Request Standardization](https://github.com/stevesdiary/crm/blob/main/src/common/middleware/security.middleware.ts): middleware & validation · Security middleware, validation, and exception translation normalize requests. · `src/common/middleware/security.middleware.ts`
- [Authentication](https://github.com/stevesdiary/crm/blob/main/src/auth/auth.module.ts): auth module · Issues and validates JWT-backed identities. · `src/auth/auth.module.ts`
- [CRM Domain Services](https://github.com/stevesdiary/crm/blob/main/src/leads/leads.service.ts): NestJS services · Accounts, contacts, leads, opportunities, activities, tasks, tickets, products, analytics, and notifications. · `src/leads/leads.service.ts`
- [Contacts Module](https://github.com/stevesdiary/crm/blob/main/src/contacts/contacts.module.ts): feature module · Representative controller-service Prisma feature composition. · `src/contacts/contacts.module.ts`
- [Files API & Service](https://github.com/stevesdiary/crm/blob/main/src/files/files.service.ts): file feature · Records and serves document metadata. · `src/files/files.service.ts`
- [WebSocket Gateway](https://github.com/stevesdiary/crm/blob/main/src/websocket/websocket.gateway.ts): real-time transport · Push boundary for notifications and live CRM changes. · `src/websocket/websocket.gateway.ts`
- [Health & Metrics](https://github.com/stevesdiary/crm/blob/main/src/health-metrics/health-metrics.service.ts): operations endpoints · Exposes health state and application metrics. · `src/health-metrics/health-metrics.service.ts`

### Data & Integrations

- [Prisma Provider](https://github.com/stevesdiary/crm/blob/main/src/prisma/prisma.service.ts): database access · The sole application provider for database clients. · `src/prisma/prisma.service.ts`
- [Tenant-aware CRM Schema](https://github.com/stevesdiary/crm/blob/main/prisma/schema.prisma): Prisma schema · Defines CRM entities, statuses, and file metadata. · `prisma/schema.prisma`
- **PostgreSQL**: system of record
- [Ordered Migrations](https://github.com/stevesdiary/crm/blob/main/prisma/migrations/20251115152753_init/migration.sql): database migrations · Evolves the PostgreSQL schema during deployment. · `prisma/migrations/20251115152753_init/migration.sql`
- [Storage Abstraction](https://github.com/stevesdiary/crm/blob/main/src/common/services/storage.service.ts): shared service · Separates file workflows from the storage provider. · `src/common/services/storage.service.ts`
- [Configured External Services](https://github.com/stevesdiary/crm/blob/main/.env.example): provider boundary · Storage, email, SMS, Redis, and encryption credentials are environment-configured. · `.env.example`

### Delivery & Operations

- [Container & CI Release](https://github.com/stevesdiary/crm/blob/main/.github/workflows/ci.yml): build and deployment · Docker packaging and GitHub Actions form the release boundary. · `.github/workflows/ci.yml`
- [AWS Platform Provisioning](https://github.com/stevesdiary/crm/blob/main/terraform/main.tf): Terraform · Provisions network, RDS, Redis, storage, encryption, and monitoring dependencies. · `terraform/main.tf`
- [Monitoring Stack](https://github.com/stevesdiary/crm/blob/main/monitoring/docker-compose.monitoring.yml): Prometheus Grafana Loki · Scrapes metrics, visualizes dashboards, and aggregates logs. · `monitoring/docker-compose.monitoring.yml`

## Connections

- Application Bootstrap → Application Module: boots
- Application Module → Versioned REST API: registers
- Application Module → Authentication: registers
- Application Module → Contacts Module: registers
- Application Module → WebSocket Gateway: registers
- Application Module → Health & Metrics: registers
- Request Standardization → Versioned REST API: normalizes requests
- Versioned REST API → Auth, Tenant & Roles: enforces before handling
- Auth, Tenant & Roles → Authentication: validates JWTs
- Versioned REST API → CRM Domain Services: invokes
- Contacts Module → CRM Domain Services: contains domain flow
- Versioned REST API → Files API & Service: routes file requests
- CRM Domain Services → Prisma Provider: persists through
- Files API & Service → Prisma Provider: stores metadata through
- Files API & Service → Storage Abstraction: uses
- Storage Abstraction → Configured External Services: provider boundary
- Prisma Provider → Tenant-aware CRM Schema: maps
- Prisma Provider → PostgreSQL: accesses
- Ordered Migrations → PostgreSQL: evolves
- WebSocket Gateway → CRM Domain Services: publishes live changes from
- Health & Metrics → Monitoring Stack: exposes metrics to
- Container & CI Release → Application Bootstrap: deploys runtime
- Container & CI Release → Ordered Migrations: runs during deployment
- AWS Platform Provisioning → PostgreSQL: provisions RDS
- AWS Platform Provisioning → Configured External Services: provisions platform dependencies
