# pdovhomilja/nextcrm-app architecture

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

- Interactive diagram: https://gitdiagram.com/pdovhomilja/nextcrm-app
- Repository: https://github.com/pdovhomilja/nextcrm-app

## Overview

#### Purpose and Entry Points
NextCRM is a self-hostable, full-stack CRM platform covering sales records, campaigns, projects, documents, email, invoicing, reporting, AI enrichment/search, and AI-agent access. It is a Next.js 16/React 19 App Router application with locale-prefixed UI routes. Primary entry points are `app/[locale]/layout.tsx`, `app/[locale]/(routes)/page.tsx`, API route handlers under `app/api`, and the background-function endpoint `app/api/inngest/route.ts`.

#### Application and Domain Boundary
The UI is organized by feature-oriented route groups; pages invoke server-side action modules rather than a separate internal REST layer for most mutations. `actions/` is the principal application-service boundary, pairing domain reads/writes with authorization and validation patterns. The largest domain is CRM: accounts, contacts, leads, opportunities, contracts, products, targets, activities, and audit history.
- `app/[locale]/(routes)/crm/page.tsx`
- `actions/crm/accounts/create-account.ts`
- `actions/crm/activities/create-activity.ts`

#### Persistence, Authorization, and Auditability
PostgreSQL is accessed through Prisma, with schema migrations defining the durable model. pgvector supports embeddings and semantic similarity. Better Auth provides sessions and identity; centralized authorization scopes/roles gate actions and routes. CRM records use soft deletion and maintain a structured audit trail, separating operational data access from authorization/audit cross-cutting concerns.
- `prisma/schema.prisma`
- `lib/prisma.ts`
- `lib/authz/index.ts`
- `lib/audit-log.ts`

#### CRM Workflow and Semantic Search
CRM entities are edited through actions, rendered in entity/detail pages, and can be linked through activities, tasks, documents, products, or sales line items. Record changes can trigger embedding jobs; unified search combines conventional full-text results with pgvector semantic results, while similarity actions query related entities.
- `actions/fulltext/unified-search.ts`
- `actions/crm/similarity/get-similar-accounts.ts`
- `inngest/functions/embed-account.ts`

#### Campaigns and AI Enrichment
Campaigns model templates, recipients/target lists, scheduling, send-now, follow-up, delivery webhooks, and unsubscribe handling. Enrichment for contacts and targets is initiated through API routes/actions, dispatched to Inngest, then executed by an E2B sandboxed, browser-capable agent architecture; results are filtered/applied back to CRM records. It integrates Anthropic/OpenAI-style model access and supports encrypted, tiered API keys.
- `inngest/functions/campaigns/send-step.ts`
- `app/api/campaigns/webhooks/resend/route.ts`
- `inngest/functions/enrich-target.ts`
- `lib/enrichment/agent-architecture/orchestrator.ts`

#### Operational Modules
Documents use S3-compatible object storage with presigned uploads and asynchronous enrichment/thumbnail processing. The email module supports account synchronization and CRM linking through background jobs. Invoices implement lifecycle transitions, taxes, line items, payments, PDF rendering, and Resend-based delivery. Reports aggregate domain data and can export or run on schedules.
- `app/api/upload/presigned-url/route.ts`
- `inngest/functions/emails/sync-account.ts`
- `actions/invoices/issue-invoice.ts`
- `lib/invoices/pdf/render.tsx`
- `actions/reports/schedule.ts`

#### External API and Deployment Surface
The public API surface includes auth, integrations/webhooks, PDF/export/upload routes, and an MCP server exposing CRM, campaigns, projects, documents, and reports to AI clients using bearer API tokens. Runtime infrastructure comprises Next.js, PostgreSQL 17+ with pgvector, Inngest, MinIO/S3-compatible storage, and optional Resend, IMAP/SMTP, OpenAI, Anthropic, E2B, and Google OAuth. Docker Compose packages the app and core local services; the container entrypoint applies migrations.
- `app/api/mcp/[transport]/route.ts`
- `lib/mcp/tools/index.ts`
- `docker-compose.yml`
- `docker-entrypoint.sh`

Main entry points: [`actions/crm/accounts/create-account.ts`](https://github.com/pdovhomilja/nextcrm-app/blob/main/actions/crm/accounts/create-account.ts) (Application actions), [`inngest/functions/campaigns/send-step.ts`](https://github.com/pdovhomilja/nextcrm-app/blob/main/inngest/functions/campaigns/send-step.ts) (Background workflows), [`prisma/schema.prisma`](https://github.com/pdovhomilja/nextcrm-app/blob/main/prisma/schema.prisma) (PostgreSQL + pgvector), [`lib/prisma.ts`](https://github.com/pdovhomilja/nextcrm-app/blob/main/lib/prisma.ts) (Prisma data access)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_app["NextCRM application"]
  node_ui["Locale UI routes<br/>Next.js App Router<br/>[page.tsx]"]
  node_layout["Locale layout<br/>Next.js layout<br/>[layout.tsx]"]
  node_actions["Application actions<br/>server actions<br/>[create-account.ts]"]
  node_activity_actions["CRM activities<br/>CRM workflow actions<br/>[create-activity.ts]"]
  node_public_api["Public API routes<br/>route handlers<br/>[route.ts]"]
  node_mcp_api["MCP server<br/>AI client API<br/>[route.ts]"]
  node_mcp_tools["MCP tools<br/>tool registry<br/>[index.ts]"]
  node_search["Unified search<br/>search actions<br/>[unified-search.ts]"]
  node_similarity["Entity similarity<br/>semantic CRM actions"]
  node_campaign_webhook["Campaign delivery webhook<br/>Resend webhook<br/>[route.ts]"]
  node_invoice_reporting["Invoices and reports<br/>operational actions<br/>[issue-invoice.ts]"]
end

subgraph group_data["Data and governance"]
  node_authz{{"Authorization scopes<br/>access control<br/>[index.ts]"}}
  node_audit["Audit trail<br/>audit logging<br/>[audit-log.ts]"]
  node_prisma["Prisma data access<br/>ORM<br/>[prisma.ts]"]
  node_postgres[("PostgreSQL + pgvector<br/>database<br/>[schema.prisma]")]
end

subgraph group_async["Background workflows"]
  node_inngest_endpoint["Inngest endpoint<br/>background endpoint<br/>[route.ts]"]
  node_workflows["Background workflows<br/>Inngest functions<br/>[send-step.ts]"]
  node_enrichment["Enrichment agent<br/>sandboxed browser agent<br/>[orchestrator.ts]"]
end

subgraph group_external["External services"]
  node_object_storage[("S3-compatible storage<br/>object storage")]
  node_delivery_ai{{"Delivery and AI providers<br/>external providers"}}
  node_deployment["Container deployment<br/>Docker Compose<br/>[docker-compose.yml]"]
end

node_layout -->|"wraps locale pages"| node_ui
node_ui -->|"invokes"| node_actions
node_actions -->|"coordinates CRM work"| node_activity_actions
node_actions -->|"authorizes"| node_authz
node_actions -->|"reads and writes"| node_prisma
node_actions -->|"records changes"| node_audit
node_prisma -->|"persists"| node_postgres
node_search -->|"full-text + vector query"| node_postgres
node_similarity -->|"vector similarity"| node_postgres
node_actions -.->|"dispatches events"| node_inngest_endpoint
node_inngest_endpoint -->|"runs"| node_workflows
node_workflows -->|"executes research"| node_enrichment
node_enrichment -->|"uses models and E2B"| node_delivery_ai
node_workflows -->|"applies results and embeddings"| node_prisma
node_public_api -->|"presigned uploads"| node_object_storage
node_campaign_webhook -->|"delivery events"| node_workflows
node_workflows -->|"campaign and email delivery"| node_delivery_ai
node_mcp_api -->|"exposes"| node_mcp_tools
node_mcp_tools -->|"invokes domain operations"| node_actions
node_invoice_reporting -->|"sends invoice delivery"| node_delivery_ai
node_deployment -->|"runs core database"| node_postgres
node_deployment -->|"hosts application"| node_inngest_endpoint

click node_ui "https://github.com/pdovhomilja/nextcrm-app/blob/main/app/%5Blocale%5D/(routes)/crm/page.tsx"
click node_layout "https://github.com/pdovhomilja/nextcrm-app/blob/main/app/%5Blocale%5D/layout.tsx"
click node_actions "https://github.com/pdovhomilja/nextcrm-app/blob/main/actions/crm/accounts/create-account.ts"
click node_activity_actions "https://github.com/pdovhomilja/nextcrm-app/blob/main/actions/crm/activities/create-activity.ts"
click node_public_api "https://github.com/pdovhomilja/nextcrm-app/blob/main/app/api/upload/presigned-url/route.ts"
click node_mcp_api "https://github.com/pdovhomilja/nextcrm-app/blob/main/app/api/mcp/%5Btransport%5D/route.ts"
click node_mcp_tools "https://github.com/pdovhomilja/nextcrm-app/blob/main/lib/mcp/tools/index.ts"
click node_authz "https://github.com/pdovhomilja/nextcrm-app/blob/main/lib/authz/index.ts"
click node_audit "https://github.com/pdovhomilja/nextcrm-app/blob/main/lib/audit-log.ts"
click node_prisma "https://github.com/pdovhomilja/nextcrm-app/blob/main/lib/prisma.ts"
click node_postgres "https://github.com/pdovhomilja/nextcrm-app/blob/main/prisma/schema.prisma"
click node_search "https://github.com/pdovhomilja/nextcrm-app/blob/main/actions/fulltext/unified-search.ts"
click node_similarity "https://github.com/pdovhomilja/nextcrm-app/blob/main/actions/crm/similarity/get-similar-accounts.ts"
click node_inngest_endpoint "https://github.com/pdovhomilja/nextcrm-app/blob/main/app/api/inngest/route.ts"
click node_workflows "https://github.com/pdovhomilja/nextcrm-app/blob/main/inngest/functions/campaigns/send-step.ts"
click node_enrichment "https://github.com/pdovhomilja/nextcrm-app/blob/main/lib/enrichment/agent-architecture/orchestrator.ts"
click node_campaign_webhook "https://github.com/pdovhomilja/nextcrm-app/blob/main/app/api/campaigns/webhooks/resend/route.ts"
click node_invoice_reporting "https://github.com/pdovhomilja/nextcrm-app/blob/main/actions/invoices/issue-invoice.ts"
click node_deployment "https://github.com/pdovhomilja/nextcrm-app/blob/main/docker-compose.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_ui,node_layout,node_actions,node_activity_actions,node_public_api,node_mcp_api,node_mcp_tools,node_search,node_similarity,node_campaign_webhook,node_invoice_reporting toneBlue
class node_authz,node_audit,node_prisma,node_postgres toneAmber
class node_inngest_endpoint,node_workflows,node_enrichment toneMint
class node_object_storage,node_delivery_ai,node_deployment toneRose
```

## Components

### NextCRM application

Next.js UI, application services, and public endpoints.

- [Locale UI routes](https://github.com/pdovhomilja/nextcrm-app/blob/main/app/%5Blocale%5D/(routes)/crm/page.tsx): Next.js App Router · Feature pages render CRM and operational modules. · `app/[locale]/(routes)/crm/page.tsx`
- [Locale layout](https://github.com/pdovhomilja/nextcrm-app/blob/main/app/%5Blocale%5D/layout.tsx): Next.js layout · `app/[locale]/layout.tsx`
- [Application actions](https://github.com/pdovhomilja/nextcrm-app/blob/main/actions/crm/accounts/create-account.ts): server actions · Primary mutation and domain-service boundary. · `actions/crm/accounts/create-account.ts`
- [CRM activities](https://github.com/pdovhomilja/nextcrm-app/blob/main/actions/crm/activities/create-activity.ts): CRM workflow actions · Links records, tasks, documents, and sales work. · `actions/crm/activities/create-activity.ts`
- [Public API routes](https://github.com/pdovhomilja/nextcrm-app/blob/main/app/api/upload/presigned-url/route.ts): route handlers · Serves integrations, uploads, exports, and webhooks. · `app/api/upload/presigned-url/route.ts`
- [MCP server](https://github.com/pdovhomilja/nextcrm-app/blob/main/app/api/mcp/%5Btransport%5D/route.ts): AI client API · Bearer-token access to CRM and operational tools. · `app/api/mcp/[transport]/route.ts`
- [MCP tools](https://github.com/pdovhomilja/nextcrm-app/blob/main/lib/mcp/tools/index.ts): tool registry · `lib/mcp/tools/index.ts`
- [Unified search](https://github.com/pdovhomilja/nextcrm-app/blob/main/actions/fulltext/unified-search.ts): search actions · Combines full-text and semantic results. · `actions/fulltext/unified-search.ts`
- [Entity similarity](https://github.com/pdovhomilja/nextcrm-app/blob/main/actions/crm/similarity/get-similar-accounts.ts): semantic CRM actions · `actions/crm/similarity/get-similar-accounts.ts`
- [Campaign delivery webhook](https://github.com/pdovhomilja/nextcrm-app/blob/main/app/api/campaigns/webhooks/resend/route.ts): Resend webhook · Captures delivery events and unsubscribe handling. · `app/api/campaigns/webhooks/resend/route.ts`
- [Invoices and reports](https://github.com/pdovhomilja/nextcrm-app/blob/main/actions/invoices/issue-invoice.ts): operational actions · Issues invoices, renders PDFs, and schedules reports. · `actions/invoices/issue-invoice.ts`

### Data and governance

Durable CRM data, access control, and auditability.

- [Authorization scopes](https://github.com/pdovhomilja/nextcrm-app/blob/main/lib/authz/index.ts): access control · Roles and scopes gate routes and actions. · `lib/authz/index.ts`
- [Audit trail](https://github.com/pdovhomilja/nextcrm-app/blob/main/lib/audit-log.ts): audit logging · Tracks structured CRM record changes. · `lib/audit-log.ts`
- [Prisma data access](https://github.com/pdovhomilja/nextcrm-app/blob/main/lib/prisma.ts): ORM · Persists soft-deleted CRM and operational records. · `lib/prisma.ts`
- [PostgreSQL + pgvector](https://github.com/pdovhomilja/nextcrm-app/blob/main/prisma/schema.prisma): database · Durable schema, full-text data, and embeddings. · `prisma/schema.prisma`

### Background workflows

Inngest-driven automation and AI processing.

- [Inngest endpoint](https://github.com/pdovhomilja/nextcrm-app/blob/main/app/api/inngest/route.ts): background endpoint · Registers and receives workflow events. · `app/api/inngest/route.ts`
- [Background workflows](https://github.com/pdovhomilja/nextcrm-app/blob/main/inngest/functions/campaigns/send-step.ts): Inngest functions · Runs campaign sends, email sync, embedding, and enrichment. · `inngest/functions/campaigns/send-step.ts`
- [Enrichment agent](https://github.com/pdovhomilja/nextcrm-app/blob/main/lib/enrichment/agent-architecture/orchestrator.ts): sandboxed browser agent · Researches targets and applies filtered CRM updates. · `lib/enrichment/agent-architecture/orchestrator.ts`

### External services

Storage, delivery, AI, and client integrations.

- **S3-compatible storage**: object storage · Stores documents and generated artifacts.
- **Delivery and AI providers**: external providers · Resend, mail servers, model APIs, and E2B support workflows.
- [Container deployment](https://github.com/pdovhomilja/nextcrm-app/blob/main/docker-compose.yml): Docker Compose · Starts core services; entrypoint applies migrations. · `docker-compose.yml`

## Connections

- Locale layout → Locale UI routes: wraps locale pages
- Locale UI routes → Application actions: invokes
- Application actions → CRM activities: coordinates CRM work
- Application actions → Authorization scopes: authorizes
- Application actions → Prisma data access: reads and writes
- Application actions → Audit trail: records changes
- Prisma data access → PostgreSQL + pgvector: persists
- Unified search → PostgreSQL + pgvector: full-text + vector query
- Entity similarity → PostgreSQL + pgvector: vector similarity
- Application actions → Inngest endpoint: dispatches events
- Inngest endpoint → Background workflows: runs
- Background workflows → Enrichment agent: executes research
- Enrichment agent → Delivery and AI providers: uses models and E2B
- Background workflows → Prisma data access: applies results and embeddings
- Public API routes → S3-compatible storage: presigned uploads
- Campaign delivery webhook → Background workflows: delivery events
- Background workflows → Delivery and AI providers: campaign and email delivery
- MCP server → MCP tools: exposes
- MCP tools → Application actions: invokes domain operations
- Invoices and reports → Delivery and AI providers: sends invoice delivery
- Container deployment → PostgreSQL + pgvector: runs core database
- Container deployment → Inngest endpoint: hosts application
