# hackclub/theseus architecture

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

- Interactive diagram: https://gitdiagram.com/hackclub/theseus
- Repository: https://github.com/hackclub/theseus

## Overview

##### Project type and purpose
- This is a monolithic, full-stack Ruby on Rails application that powers “hack club’s shiny new mail system.”
- It provides multiple interfaces:
- An authenticated admin UI (via Rails views/Administrate)
- A public web UI (including a PWA)
- REST JSON APIs for first-party clients (`/api/v1/...`)
- A public-facing API under `/public/api/v1/...`
- It also runs background ETL and integration jobs (Airtable, USPS, Zenventory, geocoding), generates PDFs via `snail_mail`, handles email delivery (ActionMailer), and supports real-time features (ActionCable).

##### File structure
- Top–level directories:
- `app/controllers/...` (namespaces: admin, api/v1, public, letter, warehouse, usps webhooks)
- `app/models` + `app/services` + `app/jobs`: classic MVC + service-layer + background jobs
- `app/frontend` (Vite/webpack‐style entrypoints, JS components, SCSS styles) & `app/javascript` (Stimulus controllers)
- `config/` (Rails, database.yml, cable.yml, queue.yml, credentials, routes)
- `bin/`, `Dockerfile`, `docker-compose-*.yml`, `.kamal/` (deployment hooks via Kamal), GoodJob initializer
- `lib/snail_mail` (PDF template engine)
- This reflects a layered architecture:
- Presentation layer (Rails views, JS)
- Application layer (controllers, services)
- Domain layer (models, policies, concerns)
- Infrastructure layer (database, job queue, mailer, external APIs)

##### Insights from README
- The README confirms the purpose: an open-source mail system with many moving parts and integrations.
- There’s a note that wiring up seeds, authentication, and Slack integration is pending, so flexible extensibility is key.

##### Main components & interactions (
a. Clients (browser, PWA service worker, third-party app)
b. Rails Application Server
- HTTP(S) endpoints → controllers (admin, public, API, webhooks)
- Background job producer → GoodJob (in-db queue)
- Real-time channel → ActionCable (websocket)
- Asset server → Vite build outputs, static `public/`
c. Database (PostgreSQL via ActiveRecord)
d. Background Workers (GoodJob threads/processes)
- Jobs call services:
- Airtable ETL, table_sync
- USPS API (iv_mtr, pricing, indicia), EasyPost
- GeocodingService (USPS facilities)
- Zenventory integration
- AIService (OpenAI)
- Jobs write results back to DB, enqueue further jobs, trigger mailers
e. Mailer Service (ActionMailer + SMTP or Sendgrid, configured in `config/environments/*.rb`)
f. SnailMail PDF Generator (lib/snail_mail)
g. External services (USPS, Airtable, Zenventory, Geocoding, OpenAI)
h. Storage (ActiveStorage: local or S3 per `config/storage.yml`)

##### Architectural patterns & principles
- MVC in Rails (controllers → models → views / JSON builders)
- Service-oriented helpers (`app/services/…`) for external API encapsulation
- Namespacing for versioned APIs (`api/v1`, `public/api/v1`)
- Background processing via GoodJob (job queue + worker pool)
- Concern-based code reuse (`app/controllers/concerns`, `app/models/concerns`)
- Frontend asset bundling with Vite + Stimulus for interactivity

- Client devices & PWA
- Rails Web/API Server
- Asset pipeline (Vite)
- ActiveRecord → PostgreSQL
- GoodJob queue & worker pool
- SnailMail PDF service
- ActionMailer SMTP path
- ActionCable websocket channel
- External integrations (USPS, Airtable, Zenventory, Geocoding, OpenAI)
- ActiveStorage (local/S3)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% UI Layer
    subgraph "Clients & Frontend" 
        direction TB
        FE1["Entrypoints"]:::ui
        FE2["JS Components"]:::ui
        FE3["Styles"]:::ui
        FE4["PWA Manifest"]:::ui
        FE5["Service Worker"]:::ui
        SC1["Stimulus Controllers"]:::ui
        SC2["Stimulus Entrypoint"]:::ui
    end

    %% Application Layer
    subgraph "Rails Application Server" 
        direction TB
        RA["Rails App (Web + API Controllers)"]:::app
        AC["ActionCable (Websocket)"]:::app
        AS["Asset Pipeline (Vite)"]:::app
        AM["ActionMailer"]:::app
        LB["ActiveStorage"]:::app
    end

    %% Background Processing
    subgraph "Background Processing" 
        direction TB
        GJ["GoodJob Queue"]:::background
        JB["Background Workers"]:::background
    end

    %% Services Layer
    subgraph "Integration Services" 
        direction TB
        S1["Airtable ETL"]:::external
        S2["Table Sync"]:::external
        S3["EasyPost Service"]:::external
        S4["Geocoding Service"]:::external
        S5["Zenventory Service"]:::external
        S6["AI/OpenAI Service"]:::external
        S7["USPS API Engines"]:::external
    end

    %% Utility & Helpers
    SM["SnailMail PDF Generator"]:::app

    %% Persistence Layer
    subgraph "Persistence & Storage"
        direction TB
        DB["PostgreSQL Database"]:::persistence
        FS["Object Storage (Local/S3)"]:::persistence
    end

    %% External Services
    subgraph "External APIs & Mail Delivery"
        direction TB
        EXT1["USPS API"]:::external
        EXT2["Airtable API"]:::external
        EXT3["Zenventory API"]:::external
        EXT4["Geocoding API"]:::external
        EXT5["OpenAI API"]:::external
        SMTP["SMTP/SendGrid"]:::external
    end

    %% Connections
    FE1 -->|loads assets| RA
    FE2 -->|loads assets| RA
    FE3 -->|loads styles| RA
    FE4 -->|PWA manifest| RA
    FE5 -->|service worker| RA
    SC1 -->|JS interaction| RA
    SC2 -->|JS boot| RA

    RA -->|HTTP/JSON| DB
    RA -->|enqueue jobs| GJ
    RA -->|publish| AC
    RA -->|send email| AM
    RA -->|store files| LB
    RA -->|bundle assets| AS

    GJ -->|run| JB
    JB -->|call| S1
    JB -->|call| S2
    JB -->|call| S3
    JB -->|call| S4
    JB -->|call| S5
    JB -->|call| S6
    JB -->|call| S7
    JB -->|write results| DB
    JB -->|trigger mail| AM

    S1 -->|API| EXT2
    S2 -->|sync| DB
    S3 -->|API| EXT3
    S4 -->|API| EXT4
    S5 -->|API| EXT3
    S6 -->|API| EXT5
    S7 -->|API| EXT1

    AM -->|SMTP| SMTP
    LB -->|S3 or local| FS
    SM -->|generate PDF| RA

    AC -->|websocket| FE1

    %% Click Events
    click FE1 "https://github.com/hackclub/theseus/tree/main/app/frontend/entrypoints/"
    click FE2 "https://github.com/hackclub/theseus/tree/main/app/frontend/js/"
    click FE3 "https://github.com/hackclub/theseus/tree/main/app/frontend/styles/"
    click FE4 "https://github.com/hackclub/theseus/blob/main/app/views/pwa/manifest.json.erb"
    click FE5 "https://github.com/hackclub/theseus/blob/main/app/views/pwa/service-worker.js"
    click SC1 "https://github.com/hackclub/theseus/tree/main/app/javascript/controllers/"
    click SC2 "https://github.com/hackclub/theseus/blob/main/app/javascript/entrypoints/application.js"
    click RA "https://github.com/hackclub/theseus/blob/main/config/routes.rb"
    click RA "https://github.com/hackclub/theseus/tree/main/app/controllers/admin/"
    click RA "https://github.com/hackclub/theseus/tree/main/app/controllers/public/"
    click RA "https://github.com/hackclub/theseus/tree/main/app/controllers/api/v1/"
    click RA "https://github.com/hackclub/theseus/tree/main/app/controllers/public/api/v1/"
    click RA "https://github.com/hackclub/theseus/blob/main/app/controllers/usps/iv_mtr/webhook_controller.rb"
    click SM "https://github.com/hackclub/theseus/tree/main/lib/snail_mail/"
    click GJ "https://github.com/hackclub/theseus/blob/main/config/initializers/good_job.rb"
    click JB "https://github.com/hackclub/theseus/tree/main/app/jobs/"
    click S1 "https://github.com/hackclub/theseus/tree/main/app/services/airtable_etl"
    click S2 "https://github.com/hackclub/theseus/tree/main/app/services/table_sync"
    click S3 "https://github.com/hackclub/theseus/blob/main/app/services/easypost_service.rb"
    click S4 "https://github.com/hackclub/theseus/tree/main/app/services/geocoding_service/"
    click S5 "https://github.com/hackclub/theseus/blob/main/app/services/zenventory.rb"
    click S6 "https://github.com/hackclub/theseus/blob/main/app/services/ai_service.rb"
    click S7 "https://github.com/hackclub/theseus/blob/main/app/services/usps/api_service.rb"
    click DB "https://github.com/hackclub/theseus/blob/main/config/database.yml"
    click FS "https://github.com/hackclub/theseus/blob/main/config/storage.yml"
    click AS "https://github.com/hackclub/theseus/blob/main/config/vite.json"
    click AS "https://github.com/hackclub/theseus/blob/main/public/.vite/manifest.json"
    click AS "https://github.com/hackclub/theseus/tree/main/bin/vite"
    click SM "https://github.com/hackclub/theseus/tree/main/lib/snail_mail/components/"
    click SM "https://github.com/hackclub/theseus/tree/main/lib/snail_mail/assets/"

    %% Styles
    classDef ui fill:#E3F2FD,stroke:#90CAF9,color:#0D47A1
    classDef app fill:#E8F5E9,stroke:#66BB6A,color:#1B5E20
    classDef background fill:#FFF3E0,stroke:#FFB74D,color:#E65100
    classDef persistence fill:#ECEFF1,stroke:#B0BEC5,color:#37474F
    classDef external fill:#FFEBEE,stroke:#EF5350,color:#B71C1C
```
