# mfts/papermark architecture

> Architecture diagram of the GitHub repository mfts/papermark, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-03-28). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mfts/papermark
- Repository: https://github.com/mfts/papermark

## Overview

##### Main Components
- Backend/API – There are many API endpoints defined under both “app/api” and “pages/api”. These endpoints manage authentication (NextAuth.js), file uploads (S3, Tus), datarooms, document analytics, teams, and webhooks.
- Business Logic & Libraries – The “lib” directory contains helper functions, integrations (for AWS S3, Notion, Posthog, and Tinybird), authentication utilities, webhook handlers, and analytics. The “ee” folder provides enterprise edition specific code (e.g., limits and Stripe integration).
- Analytics & External Services – The integration with Tinybird (in the “lib/tinybird” folder), Resend for emails, and Stripe for payments. These integrations can be shown as external services that interact with the backend.
- Authentication & Security – The “app/(auth)” directory and NextAuth.js configuration in “pages/api/auth” establish the authentication flows, while additional logic for passkeys and token generation exists in “lib/api/auth”.
- File Management – Code for uploading, processing, and streaming document files is distributed across “lib/files” and relevant API endpoints in “pages/api/file”. This handles S3 or Vercel Blob integrations.
- Cron Jobs & Scheduling – There’s a dedicated “app/api/cron” folder and “lib/cron” directory that execute periodic tasks (like verifying queues with Qstash and processing documents).

##### And Represent Relationships
- API ↔ Business Logic: The API endpoints invoke services in “lib” (authentication, document handling, notifications, analytics, etc.) so illustrate these service modules as an intermediate layer.
- API Layer ↔ Database: Prisma ORM serves as the abstraction to PostgreSQL through schema-defined models and migrations.
- Cron & Scheduled Jobs: Show a module or a background worker element that periodically executes tasks via endpoints under “cron”.

##### Architectural Patterns and Design Principles
- Separation of Concerns: The project strictly separates UI concerns (in “components”, “pages”, “app”) from business logic (in “lib”) and integration modules.
- Modular Architecture: With distinct directories for authentication, datarooms, teams, and documents, identify each as independent modules communicating through clear API boundaries.
- Event and Webhook Handling: Illustrate event flows (e.g., document events, link creation, webhooks, etc.) coming from both user actions and external services.
- Scalability Aspects: Highlight the use of serverless functions (via Next.js API routes) and cron jobs for asynchronous processing.

##### Detailed Elements
- Labels: Clearly identify each module (e.g., “UI Components”, “Auth API”, “Document Management”, “Analytics & Reporting”, “Team & Permissions”, “Webhook Handlers”, etc.).
- User initiates document upload → UI → API endpoint → lib/files → AWS S3/Vercel Blob.
- Authentication flow: UI login → NextAuth API → lib/api/auth → Database.
- Analytics events: UI activity → API → lib/tracking or tinybird integration.
- Interaction Points: Indicate that API endpoints under “pages/api” and “app/api” act as the connectors between frontend requests and backend responses.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User Layer
    subgraph "User Layer"
        U("User/Client"):::frontend
    end

    %% Frontend Layer
    subgraph "Frontend Layer"
        FE1("Pages (pages)"):::frontend
        FE2("App (app)"):::frontend
        FE3("UI Components (components)"):::frontend
        FEAuth("Auth UI (app/(auth))"):::frontend
    end

    %% Backend/API Layer
    subgraph "Backend/API Layer"
        API1("API Routes (pages/api)"):::backend
        API2("API Routes (app/api)"):::backend
        APIAuth("NextAuth Config (pages/api/auth/[...nextauth].ts)"):::backend
        APIFile("File API (pages/api/file)"):::backend
        APICron("Cron API (app/api/cron)"):::backend
    end

    %% Business Logic & Libraries
    subgraph "Business Logic & Libraries"
        BL1("Business Logic (lib)"):::business
        BL2("Enterprise Logic (ee)"):::business
        BLAuth("Custom Auth (lib/api/auth)"):::business
        BLFiles("File Handling (lib/files)"):::business
    end

    %% Data Persistence
    subgraph "Data Persistence"
        DB("Prisma & Migrations (prisma)"):::database
    end

    %% External Services
    subgraph "External Services"
        ES1("Tinybird Integration (lib/tinybird)"):::external
        ES2("Stripe Integration (ee/stripe)"):::external
        ES3("Resend Emails (lib/resend.ts)"):::external
        ES4("File Storage (AWS S3/Vercel Blob)"):::external
    end

    %% Cron Jobs & Scheduling
    subgraph "Cron Jobs & Scheduling"
        Cron("Cron Jobs (lib/cron)"):::cron
    end

    %% Connections from User to Frontend
    U --> FE1
    U --> FE2
    U --> FE3
    U --> FEAuth

    %% Frontend calls API Layer
    FE1 -->|"calls"| API1
    FE2 -->|"calls"| API2
    FE3 -->|"triggers"| API1
    FEAuth -->|"login requests"| APIAuth

    %% API Layer calls Business Logic & Libraries
    API1 -->|"invokes"| BL1
    API2 -->|"invokes"| BL1
    APIAuth -->|"auth flow"| BLAuth
    APIFile -->|"file upload"| BLFiles
    APICron -->|"starts"| Cron

    %% Business Logic interacts with Database
    BL1 -->|"db operations"| DB
    BL2 -->|"db operations"| DB
    BLAuth -->|"user auth ops"| DB

    %% Business Logic calls External Services
    BL1 -->|"analytics"| ES1
    BL1 -->|"email notifications"| ES3
    BL2 -->|"payments"| ES2
    BLFiles -->|"stores files"| ES4

    %% Click Events
    click FE1 "https://github.com/mfts/papermark/tree/main/pages"
    click FE2 "https://github.com/mfts/papermark/tree/main/app"
    click FE3 "https://github.com/mfts/papermark/tree/main/components"
    click FEAuth "https://github.com/mfts/papermark/tree/main/app/(auth)"
    click API1 "https://github.com/mfts/papermark/tree/main/pages/api"
    click API2 "https://github.com/mfts/papermark/tree/main/app/api"
    click APIAuth "https://github.com/mfts/papermark/blob/main/pages/api/auth/[...nextauth].ts"
    click APICron "https://github.com/mfts/papermark/tree/main/app/api/cron"
    click APIFile "https://github.com/mfts/papermark/tree/main/pages/api/file"
    click BL1 "https://github.com/mfts/papermark/tree/main/lib"
    click BL2 "https://github.com/mfts/papermark/tree/main/ee"
    click BLAuth "https://github.com/mfts/papermark/tree/main/lib/api/auth"
    click BLFiles "https://github.com/mfts/papermark/tree/main/lib/files"
    click DB "https://github.com/mfts/papermark/tree/main/prisma"
    click ES1 "https://github.com/mfts/papermark/tree/main/lib/tinybird"
    click ES2 "https://github.com/mfts/papermark/tree/main/ee/stripe"
    click ES3 "https://github.com/mfts/papermark/blob/main/lib/resend.ts"
    click Cron "https://github.com/mfts/papermark/tree/main/lib/cron"

    %% Styles
    classDef frontend fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef backend fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef business fill:#e2e3e5,stroke:#6c757d,stroke-width:2px;
    classDef database fill:#fff3cd,stroke:#856404,stroke-width:2px;
    classDef external fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    classDef cron fill:#d1ecf1,stroke:#0c5460,stroke-width:2px;
```
