How stevesdiary/crm works
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: app.module.ts Application Module · opportunities.controller.ts Versioned REST API · prisma.service.ts Prisma Provider · leads.service.ts CRM Domain Services
Components 20
NestJS Runtime
- Application Bootstrap · Starts the HTTP application and global request pipeline.
src/main.ts - Application Module · Composes feature and infrastructure modules.
src/app.module.ts - Versioned REST API · CRM controllers expose the public request boundary.
src/opportunities/opportunities.controller.ts - Auth, Tenant & Roles · JWT validation, tenant isolation, and role authorization run before domain work.
src/auth/jwt-auth.guard.ts - Request Standardization · Security middleware, validation, and exception translation normalize requests.
src/common/middleware/security.middleware.ts - Authentication · Issues and validates JWT-backed identities.
src/auth/auth.module.ts - CRM Domain Services · Accounts, contacts, leads, opportunities, activities, tasks, tickets, products, analytics, and notifications.
src/leads/leads.service.ts - Contacts Module · Representative controller-service Prisma feature composition.
src/contacts/contacts.module.ts - Files API & Service · Records and serves document metadata.
src/files/files.service.ts - WebSocket Gateway · Push boundary for notifications and live CRM changes.
src/websocket/websocket.gateway.ts - Health & Metrics · Exposes health state and application metrics.
src/health-metrics/health-metrics.service.ts
Data & Integrations
- Prisma Provider · The sole application provider for database clients.
src/prisma/prisma.service.ts - Tenant-aware CRM Schema · Defines CRM entities, statuses, and file metadata.
prisma/schema.prisma - PostgreSQL
- Ordered Migrations · Evolves the PostgreSQL schema during deployment.
prisma/migrations/20251115152753_init/migration.sql - Storage Abstraction · Separates file workflows from the storage provider.
src/common/services/storage.service.ts - Configured External Services · Storage, email, SMS, Redis, and encryption credentials are environment-configured.
.env.example
Delivery & Operations
- Container & CI Release · Docker packaging and GitHub Actions form the release boundary.
.github/workflows/ci.yml - AWS Platform Provisioning · Provisions network, RDS, Redis, storage, encryption, and monitoring dependencies.
terraform/main.tf - Monitoring Stack · Scrapes metrics, visualizes dashboards, and aggregates logs.
monitoring/docker-compose.monitoring.yml
Connections 25
- 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