# phukon/clack architecture

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

- Interactive diagram: https://gitdiagram.com/phukon/clack
- Repository: https://github.com/phukon/clack

## Overview

##### Main System Components

a) Client Layer:
- Next.js Frontend (src/app directory)
- Authentication UI (src/app/auth)
- Dashboard Interface (src/app/dash)
- Rich Text Editor (src/app/note)

b) API Layer:
- Authentication Endpoints (src/app/api/auth)
- Integration Endpoints (Google, Notion)
- Webhook Handlers (Stripe)
- Cron Job Endpoints

c) Service Layer:
- Authentication Service (NextAuth.js)
- Document Processing
- Encryption Service (AES-256-CBC)
- Payment Processing (Stripe)
- Email Service (Resend)

d) Data Layer:
- Neon PostgreSQL Database
- Vercel KV (Redis)
- Cloudflare Workers KV

##### Key Relationships to Highlight

a) Authentication Flow:
**User → Auth UI → NextAuth.js → Database**
- ↘ Email Service

b) Document Processing Flow:
**User → Editor UI → API Layer → Encryption Service → Database**
- ↘ Integration Services (Notion/Google)

c) Data Synchronization:
**Cron Jobs → API Layer → KV Stores**
- ↘ Database

##### Performance Optimization Points

Highlight:
- Cloudflare Workers KV for low-latency data
- Vercel KV for caching
- Redundant cron jobs (Vercel + EC2)

##### Architectural Patterns to Emphasize

- Server-side rendering with Next.js
- API route handlers
- Database schema relationships
- Middleware implementation
- Service layer abstraction

Remember to:
- Version information
- Document all external service dependencies
- Error handling flows
- Data encryption points
- Public vs. protected routes

Keep the abstraction level high enough to be understandable but detailed enough to show the crucial architectural decisions, especially around data encryption, integration points, and the cron job system for word count tracking.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph Client["Client Layer"]
        Frontend["Next.js Frontend"]:::frontend
        AuthUI["Authentication UI"]:::frontend
        DashUI["Dashboard Interface"]:::frontend
        Editor["Rich Text Editor"]:::frontend
    end

    subgraph API["API Layer"]
        AuthAPI["Authentication Endpoints"]:::api
        NotionAPI["Notion Integration API"]:::api
        GoogleAPI["Google Integration API"]:::api
        WebhookAPI["Webhook Handlers"]:::api
        CronAPI["Cron Job Endpoints"]:::api
    end

    subgraph Services["Service Layer"]
        AuthService["Authentication Service"]:::service
        DocProcess["Document Processing"]:::service
        Encryption["Encryption Service"]:::service
        PaymentService["Payment Processing"]:::service
        EmailService["Email Service"]:::service
    end

    subgraph Data["Data Layer"]
        DB[(PostgreSQL)]:::database
        VercelKV[(Vercel KV)]:::database
        CloudflareKV[(Cloudflare KV)]:::database
    end

    subgraph External["External Services"]
        Notion["Notion API"]:::external
        Google["Google Docs"]:::external
        Stripe["Stripe"]:::external
        Resend["Resend Email"]:::external
    end

    %% Client Layer Relationships
    Frontend --> AuthUI
    Frontend --> DashUI
    Frontend --> Editor

    %% Authentication Flow
    AuthUI --> AuthAPI
    AuthAPI --> AuthService
    AuthService --> DB
    AuthService --> EmailService
    EmailService --> Resend

    %% Document Flow
    Editor --> DocProcess
    DocProcess --> Encryption
    Encryption --> DB
    DocProcess --> NotionAPI
    DocProcess --> GoogleAPI
    NotionAPI --> Notion
    GoogleAPI --> Google

    %% Payment Flow
    WebhookAPI --> PaymentService
    PaymentService --> Stripe
    PaymentService --> DB

    %% Cron Jobs
    CronAPI --> CloudflareKV
    CronAPI --> VercelKV
    CronAPI --> DB

    %% Click Events
    click Frontend "https://github.com/phukon/clack/tree/main/src/app"
    click AuthUI "https://github.com/phukon/clack/tree/main/src/app/auth"
    click DashUI "https://github.com/phukon/clack/tree/main/src/app/dash"
    click Editor "https://github.com/phukon/clack/blob/main/src/app/note/[id]/novelEditor.tsx"
    click AuthAPI "https://github.com/phukon/clack/tree/main/src/app/api/auth"
    click NotionAPI "https://github.com/phukon/clack/tree/main/src/app/api/notion"
    click GoogleAPI "https://github.com/phukon/clack/tree/main/src/app/api/google"
    click WebhookAPI "https://github.com/phukon/clack/tree/main/src/app/api/webhooks"
    click CronAPI "https://github.com/phukon/clack/tree/main/src/app/api/cron"
    click AuthService "https://github.com/phukon/clack/blob/main/src/auth.ts"
    click DocProcess "https://github.com/phukon/clack/tree/main/src/actions/document"
    click Encryption "https://github.com/phukon/clack/blob/main/src/lib/encryptData.ts"
    click PaymentService "https://github.com/phukon/clack/tree/main/src/actions/payments"
    click EmailService "https://github.com/phukon/clack/blob/main/src/lib/mail.tsx"
    click DB "https://github.com/phukon/clack/blob/main/prisma/schema.prisma"
    click CloudflareKV "https://github.com/phukon/clack/blob/main/clack-cloudfare-worker/src/index.ts"

    %% Styling
    classDef frontend fill:#2563eb,color:white
    classDef api fill:#22c55e,color:white
    classDef service fill:#a855f7,color:white
    classDef database fill:#6366f1,color:white
    classDef external fill:#eab308,color:white

    %% Legend
    subgraph Legend
        Frontend_L[Frontend Components]:::frontend
        API_L[API Endpoints]:::api
        Service_L[Services]:::service
        DB_L[(Databases)]:::database
        External_L[External Services]:::external
    end
```
