# saashqdev/saashq architecture

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

- Interactive diagram: https://gitdiagram.com/saashqdev/saashq
- Repository: https://github.com/saashqdev/saashq

## Overview

#### Purpose and Runtime
SaasHQ is a Next.js 14/TypeScript CRM/ERP starter: a localized, authenticated business application covering CRM, projects, documents, invoices, reporting, AI assistance, and workflow automation. It runs as a single Next App Router service, deployable to Vercel or Docker.
- `app/[locale]/layout.tsx`
- `app/[locale]/(routes)/layout.tsx`
- `middleware.tsx`

#### Entry Points, Identity, and Tenancy Boundaries
Locale-prefixed App Router pages are the browser entry points; route groups separate authentication screens from protected application modules. Auth.js is exposed through a catch-all API handler. User activation, roles/admin features, enabled modules, and profile settings are managed through user/admin endpoints and UI.
- `app/[locale]/(auth)/sign-in/page.tsx`
- `app/api/auth/[...nextauth]/route.ts`
- `lib/auth.ts`

#### Persistence and Domain Model
Prisma is the central persistence boundary over PostgreSQL (documented as Tembo-hosted in the README). Its schema defines cross-module business entities and relations; seeded reference data initializes CRM types, GPT models, and enabled modules. Server-side `actions/` primarily read domain data for rendered views, while `app/api/` owns mutation/integration endpoints.
- `prisma/schema.prisma`
- `lib/prisma.ts`
- `prisma/seeds/seed.ts`

#### CRM and Operational Work Management
CRM manages accounts, contacts, leads, opportunities, and account tasks, with detail views connecting related records. Projects provide boards, sections, Kanban tasks, assignments, comments, documents, watches, and task completion. These are separate UI/API domains but both expose task-oriented operations and notifications.
- `app/[locale]/(routes)/crm/page.tsx`
- `app/api/crm/opportunity/[opportunityId]/route.ts`
- `app/[locale]/(routes)/projects/boards/[boardId]/components/Kanban.tsx`

#### Workflow Definition and Execution Engine
The workflow module supplies graph-oriented create/edit/detail screens with React Flow and Monaco-based task configuration. Definitions and runtime records are accessed through workflow APIs. A server-side engine abstracts transport, task dispatch, and processors for function, guard, and wait stages; dedicated engine routes start executions and receive/listen for events.
- `app/[locale]/(routes)/workflows/components/ReactFlow.tsx`
- `lib/engine/engine.service.ts`
- `app/api/engine/start/route.ts`

#### Documents, Invoices, and External Storage/Extraction
Documents and uploads cross from app APIs into UploadThing/S3-oriented storage integrations. Invoice workflows add Rossum token, embedded cockpit, and annotation endpoints for AI-assisted invoice parsing; XML/export and email-send endpoints support downstream invoice handling. File storage and invoice extraction are material external-service boundaries.
- `app/api/uploadthing/core.ts`
- `lib/digital-ocean-s3.ts`
- `app/api/invoice/rossum/get-annotation/[annotationId]/route.ts`

#### AI, Messaging, and Background-style Reporting
OpenAI endpoints power chat/completions and project/board AI reports; admin settings select GPT models. Resend plus React Email templates deliver invites, task comments, CRM/project task notifications, password resets, and AI reports. SMTP/IMAP support also exists for email features. Cron-oriented actions retrieve AI task work, implying scheduled invocation is external to this repo.
- `app/api/openai/create-chat-completion/route.ts`
- `actions/ai/projects/boards/getAiReport.tsx`
- `lib/resend.ts`

#### Delivery and Client Data Flow
UI pages compose server actions and client-side fetching/state mechanisms (SWR, Axios, React Query per README), calling same-origin route handlers for mutations. Shared providers establish theme/toast behavior; dictionaries and locale layouts support i18n. Docker, Vercel, and CI definitions are the deployment/automation boundaries.
- `app/providers/ThemeProvider.tsx`
- `dictionaries.ts`
- `docker-compose.yml`

Main entry points: [`lib/prisma.ts`](https://github.com/saashqdev/saashq/blob/main/lib/prisma.ts) (Prisma data layer), [`app/[locale]/layout.tsx`](https://github.com/saashqdev/saashq/blob/main/app/%5Blocale%5D/layout.tsx) (Locale route layouts), [`app/api/crm/opportunity/[opportunityId]/route.ts`](https://github.com/saashqdev/saashq/blob/main/app/api/crm/opportunity/%5BopportunityId%5D/route.ts) (Business APIs), [`lib/auth.ts`](https://github.com/saashqdev/saashq/blob/main/lib/auth.ts) (Auth service)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Browser UI"]
  node_auth_ui["Sign-in UI<br/>auth page<br/>[page.tsx]"]
  node_providers["UI providers<br/>client providers<br/>[ThemeProvider.tsx]"]
  node_domain_ui["CRM &amp; projects UI<br/>business modules<br/>[Kanban.tsx]"]
  node_workflow_designer["Workflow designer<br/>React Flow UI<br/>[ReactFlow.tsx]"]
end

subgraph group_app["Next.js Application"]
  node_locale_routes["Locale route layouts<br/>App Router<br/>[layout.tsx]"]
  node_protected_routes["Protected app routes<br/>route layout<br/>[layout.tsx]"]
  node_middleware{{"Request middleware<br/>[middleware.tsx]"}}
  node_i18n["Dictionaries<br/>i18n<br/>[dictionaries.ts]"]
  node_auth_api["Auth.js handler<br/>API route<br/>[route.ts]"]
  node_auth["Auth service<br/>identity<br/>[auth.ts]"]
  node_domain_api["Business APIs<br/>API routes<br/>[route.ts]"]
  node_engine_api["Engine start API<br/>API route<br/>[route.ts]"]
  node_workflow_engine["Workflow engine<br/>execution service<br/>[engine.service.ts]"]
  node_ai_api["OpenAI API<br/>API route<br/>[route.ts]"]
  node_ai_reporting["AI board reports<br/>scheduled action<br/>[getAiReport.tsx]"]
  node_upload_api["Upload API<br/>storage API<br/>[core.ts]"]
  node_invoice_extraction["Invoice extraction API<br/>integration API<br/>[route.ts]"]
end

subgraph group_data["Data &amp; Integrations"]
  node_prisma["Prisma data layer<br/>ORM<br/>[prisma.ts]"]
  node_schema["Business schema<br/>Prisma schema<br/>[schema.prisma]"]
  node_postgres[("PostgreSQL<br/>database")]
  node_seed_data["Reference-data seed<br/>database seed<br/>[seed.ts]"]
  node_object_storage[("Object storage<br/>external storage")]
  node_email["Email delivery<br/>Resend integration<br/>[resend.ts]"]
  node_external_ai{{"OpenAI<br/>external AI"}}
  node_rossum{{"Rossum<br/>external extraction"}}
end

subgraph group_delivery["Delivery"]
  node_deployment["Docker deployment<br/>container delivery<br/>[docker-compose.yml]"]
end

node_middleware -->|"routes requests"| node_locale_routes
node_locale_routes -->|"loads locale text"| node_i18n
node_locale_routes -->|"renders with"| node_providers
node_auth_ui -->|"signs in through"| node_auth_api
node_auth_api -->|"uses"| node_auth
node_auth -->|"reads users and roles"| node_prisma
node_protected_routes -->|"enforces access"| node_auth
node_domain_ui -->|"mutations"| node_domain_api
node_domain_api -->|"persists domain data"| node_prisma
node_workflow_designer -->|"manages definitions"| node_domain_api
node_engine_api -->|"starts execution"| node_workflow_engine
node_workflow_engine -->|"reads definitions and runtime"| node_prisma
node_ai_api -->|"completions"| node_external_ai
node_ai_reporting -->|"generates reports"| node_external_ai
node_ai_reporting -->|"sends reports"| node_email
node_upload_api -->|"stores files"| node_object_storage
node_invoice_extraction -->|"gets annotations"| node_rossum
node_prisma -->|"maps models"| node_schema
node_prisma -->|"queries"| node_postgres
node_seed_data -->|"initializes"| node_postgres
node_domain_api -->|"sends notifications"| node_email
node_deployment -.->|"runs application"| node_locale_routes

click node_locale_routes "https://github.com/saashqdev/saashq/blob/main/app/%5Blocale%5D/layout.tsx"
click node_auth_ui "https://github.com/saashqdev/saashq/blob/main/app/%5Blocale%5D/(auth)/sign-in/page.tsx"
click node_protected_routes "https://github.com/saashqdev/saashq/blob/main/app/%5Blocale%5D/(routes)/layout.tsx"
click node_middleware "https://github.com/saashqdev/saashq/blob/main/middleware.tsx"
click node_providers "https://github.com/saashqdev/saashq/blob/main/app/providers/ThemeProvider.tsx"
click node_i18n "https://github.com/saashqdev/saashq/blob/main/dictionaries.ts"
click node_auth_api "https://github.com/saashqdev/saashq/blob/main/app/api/auth/%5B...nextauth%5D/route.ts"
click node_auth "https://github.com/saashqdev/saashq/blob/main/lib/auth.ts"
click node_domain_ui "https://github.com/saashqdev/saashq/blob/main/app/%5Blocale%5D/(routes)/projects/boards/%5BboardId%5D/components/Kanban.tsx"
click node_domain_api "https://github.com/saashqdev/saashq/blob/main/app/api/crm/opportunity/%5BopportunityId%5D/route.ts"
click node_workflow_designer "https://github.com/saashqdev/saashq/blob/main/app/%5Blocale%5D/(routes)/workflows/components/ReactFlow.tsx"
click node_engine_api "https://github.com/saashqdev/saashq/blob/main/app/api/engine/start/route.ts"
click node_workflow_engine "https://github.com/saashqdev/saashq/blob/main/lib/engine/engine.service.ts"
click node_ai_api "https://github.com/saashqdev/saashq/blob/main/app/api/openai/create-chat-completion/route.ts"
click node_ai_reporting "https://github.com/saashqdev/saashq/blob/main/actions/ai/projects/boards/getAiReport.tsx"
click node_upload_api "https://github.com/saashqdev/saashq/blob/main/app/api/uploadthing/core.ts"
click node_invoice_extraction "https://github.com/saashqdev/saashq/blob/main/app/api/invoice/rossum/get-annotation/%5BannotationId%5D/route.ts"
click node_prisma "https://github.com/saashqdev/saashq/blob/main/lib/prisma.ts"
click node_schema "https://github.com/saashqdev/saashq/blob/main/prisma/schema.prisma"
click node_seed_data "https://github.com/saashqdev/saashq/blob/main/prisma/seeds/seed.ts"
click node_object_storage "https://github.com/saashqdev/saashq/blob/main/lib/digital-ocean-s3.ts"
click node_email "https://github.com/saashqdev/saashq/blob/main/lib/resend.ts"
click node_deployment "https://github.com/saashqdev/saashq/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_auth_ui,node_providers,node_domain_ui,node_workflow_designer toneBlue
class node_locale_routes,node_protected_routes,node_middleware,node_i18n,node_auth_api,node_auth,node_domain_api,node_engine_api,node_workflow_engine,node_ai_api,node_ai_reporting,node_upload_api,node_invoice_extraction toneAmber
class node_prisma,node_schema,node_postgres,node_seed_data,node_object_storage,node_email,node_external_ai,node_rossum toneMint
class node_deployment toneRose
```

## Components

### Browser UI

- [Sign-in UI](https://github.com/saashqdev/saashq/blob/main/app/%5Blocale%5D/(auth)/sign-in/page.tsx): auth page · `app/[locale]/(auth)/sign-in/page.tsx`
- [UI providers](https://github.com/saashqdev/saashq/blob/main/app/providers/ThemeProvider.tsx): client providers · Theme and toast behavior. · `app/providers/ThemeProvider.tsx`
- [CRM & projects UI](https://github.com/saashqdev/saashq/blob/main/app/%5Blocale%5D/(routes)/projects/boards/%5BboardId%5D/components/Kanban.tsx): business modules · CRM records and project Kanban work management. · `app/[locale]/(routes)/projects/boards/[boardId]/components/Kanban.tsx`
- [Workflow designer](https://github.com/saashqdev/saashq/blob/main/app/%5Blocale%5D/(routes)/workflows/components/ReactFlow.tsx): React Flow UI · Graph definition and task configuration. · `app/[locale]/(routes)/workflows/components/ReactFlow.tsx`

### Next.js Application

- [Locale route layouts](https://github.com/saashqdev/saashq/blob/main/app/%5Blocale%5D/layout.tsx): App Router · Locale-prefixed public and protected page boundary. · `app/[locale]/layout.tsx`
- [Protected app routes](https://github.com/saashqdev/saashq/blob/main/app/%5Blocale%5D/(routes)/layout.tsx): route layout · Hosts authenticated business modules. · `app/[locale]/(routes)/layout.tsx`
- [Request middleware](https://github.com/saashqdev/saashq/blob/main/middleware.tsx): middleware · `middleware.tsx`
- [Dictionaries](https://github.com/saashqdev/saashq/blob/main/dictionaries.ts): i18n · `dictionaries.ts`
- [Auth.js handler](https://github.com/saashqdev/saashq/blob/main/app/api/auth/%5B...nextauth%5D/route.ts): API route · `app/api/auth/[...nextauth]/route.ts`
- [Auth service](https://github.com/saashqdev/saashq/blob/main/lib/auth.ts): identity · Authentication, activation, roles, and user access. · `lib/auth.ts`
- [Business APIs](https://github.com/saashqdev/saashq/blob/main/app/api/crm/opportunity/%5BopportunityId%5D/route.ts): API routes · Mutation and integration endpoints for domain modules. · `app/api/crm/opportunity/[opportunityId]/route.ts`
- [Engine start API](https://github.com/saashqdev/saashq/blob/main/app/api/engine/start/route.ts): API route · Starts executions and receives engine events. · `app/api/engine/start/route.ts`
- [Workflow engine](https://github.com/saashqdev/saashq/blob/main/lib/engine/engine.service.ts): execution service · Dispatches function, guard, and wait processors. · `lib/engine/engine.service.ts`
- [OpenAI API](https://github.com/saashqdev/saashq/blob/main/app/api/openai/create-chat-completion/route.ts): API route · Chat and completion requests. · `app/api/openai/create-chat-completion/route.ts`
- [AI board reports](https://github.com/saashqdev/saashq/blob/main/actions/ai/projects/boards/getAiReport.tsx): scheduled action · Retrieves AI task work for report generation. · `actions/ai/projects/boards/getAiReport.tsx`
- [Upload API](https://github.com/saashqdev/saashq/blob/main/app/api/uploadthing/core.ts): storage API · UploadThing-oriented document ingress. · `app/api/uploadthing/core.ts`
- [Invoice extraction API](https://github.com/saashqdev/saashq/blob/main/app/api/invoice/rossum/get-annotation/%5BannotationId%5D/route.ts): integration API · Retrieves Rossum annotations for invoice parsing. · `app/api/invoice/rossum/get-annotation/[annotationId]/route.ts`

### Data & Integrations

- [Prisma data layer](https://github.com/saashqdev/saashq/blob/main/lib/prisma.ts): ORM · Central domain persistence access. · `lib/prisma.ts`
- [Business schema](https://github.com/saashqdev/saashq/blob/main/prisma/schema.prisma): Prisma schema · Cross-module entities and relations. · `prisma/schema.prisma`
- **PostgreSQL**: database · Tembo-hosted persistence target.
- [Reference-data seed](https://github.com/saashqdev/saashq/blob/main/prisma/seeds/seed.ts): database seed · Initializes CRM types, GPT models, and enabled modules. · `prisma/seeds/seed.ts`
- [Object storage](https://github.com/saashqdev/saashq/blob/main/lib/digital-ocean-s3.ts): external storage · S3-compatible uploaded file storage. · `lib/digital-ocean-s3.ts`
- [Email delivery](https://github.com/saashqdev/saashq/blob/main/lib/resend.ts): Resend integration · Sends notifications, invites, resets, and AI reports. · `lib/resend.ts`
- **OpenAI**: external AI
- **Rossum**: external extraction · Invoice cockpit and annotation service.

### Delivery

- [Docker deployment](https://github.com/saashqdev/saashq/blob/main/docker-compose.yml): container delivery · `docker-compose.yml`

## Connections

- Request middleware → Locale route layouts: routes requests
- Locale route layouts → Dictionaries: loads locale text
- Locale route layouts → UI providers: renders with
- Sign-in UI → Auth.js handler: signs in through
- Auth.js handler → Auth service: uses
- Auth service → Prisma data layer: reads users and roles
- Protected app routes → Auth service: enforces access
- CRM & projects UI → Business APIs: mutations
- Business APIs → Prisma data layer: persists domain data
- Workflow designer → Business APIs: manages definitions
- Engine start API → Workflow engine: starts execution
- Workflow engine → Prisma data layer: reads definitions and runtime
- OpenAI API → OpenAI: completions
- AI board reports → OpenAI: generates reports
- AI board reports → Email delivery: sends reports
- Upload API → Object storage: stores files
- Invoice extraction API → Rossum: gets annotations
- Prisma data layer → Business schema: maps models
- Prisma data layer → PostgreSQL: queries
- Reference-data seed → PostgreSQL: initializes
- Business APIs → Email delivery: sends notifications
- Docker deployment → Locale route layouts: runs application
