# slynderly/wibc architecture

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

- Interactive diagram: https://gitdiagram.com/slynderly/wibc
- Repository: https://github.com/slynderly/wibc

## Overview

1. **System shape**
   - This is a single Node.js/Express SaaS backend that serves both the web dashboard and the API.
   - It is not split into separate services; most behavior lives in `server.js` plus modules under `src/`.
   - Persistence is file-based in `data/` rather than a database.

2. **Main runtime boundary**
   - `server.js` is the bootstrapping entry point.
   - On startup it configures Express, applies security middleware, migrates base64 avatars to files, and restores persisted WhatsApp sessions from `data/auth_*/`.
   - The server listens on `PORT` and exposes both user-facing routes and admin routes.

3. **Frontend boundary**
   - `public/` contains static HTML/CSS/Vanilla JS pages: login/register, dashboard, and admin panel.
   - The frontend is thin and talks to the backend through REST endpoints.
   - There is no SPA framework; the UI is mostly server-served static assets plus API-driven interactions.

4. **Core API boundary**
   - `src/routes.js` is the main application API surface.
   - It covers authentication, user profile and bot configuration, WhatsApp device/session control, chat history, manual message sending, order management, Shopify/Hotmart integrations, and prompt generation.
   - The route design is user-scoped: most resources are addressed by `:userId`.

5. **WhatsApp session subsystem**
   - `src/whatsapp.js` manages Baileys connections and persists device sessions per user/device in `data/auth_*/`.
   - It supports QR-based linking and pairing-code-based linking.
   - Incoming WhatsApp messages are the primary event stream for the application and feed both chat history storage and bot automation.

6. **AI and sales automation subsystem**
   - `src/ai.js` is the bot brain: it builds prompts, calls Groq API models, and handles AI-driven replies.
   - The bot has a silent order-detection path that scans conversation history and creates orders without interrupting the chat.
   - The AI context is intentionally selective: product catalog data is only injected when purchase-related intent is detected.

7. **Business data model and persistence**
   - User accounts live in `data/users.json`.
   - Per-user configuration, products, and integration settings live under `data/user_data/<userId>.json`.
   - Chat logs are stored under `data/chats/`, and detected orders under `data/orders/`.
   - This makes the architecture strongly file-backed, with each user’s operational state separated by directory structure.

8. **Commerce integrations boundary**
   - `src/shopify.js` handles store connection, product sync, and CSV import from Shopify.
   - `src/hotmart.js` handles Hotmart account connection and product sync.
   - These integrations feed the same internal product/catalog model used by the WhatsApp bot and dashboard.

9. **Security and abuse protection**
   - `src/security.js` centralizes defensive middleware.
   - It includes Helmet, global and endpoint-specific rate limiting, payload inspection, anti-flood checks, user-agent blocking, request timeouts, and IP handling behind proxies.
   - The admin panel uses a random session token generated on each server start, so admin access is transient across restarts.

10. **Admin subsystem**
   - `src/admin-routes.js` exposes `/admin` functionality for privileged operations.
   - Admin capabilities include user management, per-user product limits, live file editing, backup download, and restore.
   - This is a separate trust boundary from the normal SaaS dashboard, protected by password plus session token.

11. **Operational flow**
   - Typical flow: user logs in via the web UI, saves bot and catalog settings, connects WhatsApp, and the backend stores everything in `data/`.
   - Incoming WhatsApp messages trigger the bot pipeline: anti-spam/security checks, conversation persistence, optional AI response generation, and order detection.
   - Manual dashboard actions can also modify chats, orders, bot mode, and integrations, all through the same file-backed state.

12. **Important architectural consequence**
   - Because the system relies on local JSON files and session folders, the deployment model is stateful on disk.
   - Architecture diagrams should show a single Express application with three major external dependencies: WhatsApp via Baileys, Groq for AI, and Shopify/Hotmart for catalog imports, all mediated by local file storage.

Main entry points: [`README.md`](https://github.com/slynderly/wibc/blob/main/README.md) (README)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_backend["Backend"]
  node_server["Server<br/>bootstrap"]
  node_routes["API<br/>http routes"]
  node_wa["WhatsApp<br/>session manager"]
  node_ai["Bot AI<br/>prompt+reply"]
  node_security["Security<br/>middleware"]
  node_admin["Admin<br/>privileged routes"]
end

subgraph group_frontend["Frontend"]
  node_publicui["Web UI<br/>static frontend"]
end

subgraph group_data["Data"]
  node_users[("Users<br/>account store")]
  node_userdata[("User state")]
  node_chats[("Chats<br/>conversation store")]
  node_orders[("Orders<br/>order store")]
  node_authdir[("WA sessions<br/>session files")]
end

subgraph group_integrations["Integrations"]
  node_shopify["Shopify<br/>catalog import"]
  node_hotmart["Hotmart<br/>catalog sync"]
  node_groq{{"Groq<br/>ai model"}}
  node_waext{{"WhatsApp net<br/>external service"}}
end

node_readme["README<br/>docs<br/>[README.md]"]

node_readme -->|"describes"| node_server
node_server -->|"applies"| node_security
node_server -->|"mounts"| node_routes
node_server -->|"mounts"| node_admin
node_server -->|"serves"| node_publicui
node_server -->|"initializes"| node_wa
node_server -->|"restores"| node_authdir
node_routes -->|"reads/writes"| node_users
node_routes -->|"reads/writes"| node_userdata
node_routes -->|"reads/writes"| node_chats
node_routes -->|"reads/writes"| node_orders
node_routes -->|"controls"| node_wa
node_routes -->|"invokes"| node_ai
node_routes -->|"uses"| node_shopify
node_routes -->|"uses"| node_hotmart
node_admin -->|"administers"| node_users
node_admin -->|"edits"| node_userdata
node_wa -->|"connects"| node_waext
node_wa -->|"emits"| node_chats
node_wa -->|"feeds"| node_ai
node_ai -->|"calls"| node_groq
node_ai -->|"detects"| node_orders
node_shopify -->|"updates"| node_userdata
node_hotmart -->|"updates"| node_userdata

click node_readme "https://github.com/slynderly/wibc/blob/main/README.md"

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_server,node_routes,node_wa,node_ai,node_security,node_admin toneBlue
class node_publicui toneAmber
class node_users,node_userdata,node_chats,node_orders,node_authdir toneMint
class node_shopify,node_hotmart,node_groq,node_waext toneRose
class node_readme toneNeutral
```

## Components

### Backend

Express app, core API, automation, and admin boundary

- **Server**: bootstrap · Express startup, middleware wiring, session restore, and route mounting
- **API**: http routes · Main SaaS API for auth, profile, bot config, chats, orders, and integrations
- **WhatsApp**: session manager · Baileys connections, QR/pairing linking, and incoming message stream
- **Bot AI**: prompt+reply · Prompt building, Groq calls, replies, and silent order detection
- **Security**: middleware · Helmet, rate limits, flood checks, blocking, timeouts, and proxy-aware IP handling
- **Admin**: privileged routes · Admin operations for user management, limits, file editing, backup, and restore

### Frontend

Static dashboard and admin UI

- **Web UI**: static frontend · Login, registration, dashboard, and admin pages served as static assets

### Data

Local file-backed persistence and session state

- **Users**: account store · User account records in the file-backed data layer
- **User state**: per-user state · Per-user bot settings, products, and integration configuration
- **Chats**: conversation store · Persisted WhatsApp conversation history
- **Orders**: order store · Detected and manually managed order records
- **WA sessions**: session files · Per-user/device WhatsApp session state restored on startup

### Integrations

External services for messaging, AI, and commerce

- **Shopify**: catalog import · Store connection, product sync, and CSV import
- **Hotmart**: catalog sync · Hotmart account connection and product synchronization
- **Groq**: ai model · External model provider used for bot reasoning and replies
- **WhatsApp net**: external service · WhatsApp platform accessed through Baileys connections

### Other components

- [README](https://github.com/slynderly/wibc/blob/main/README.md): docs · Repository overview and operational notes · `README.md`

## Connections

- README → Server: describes · Documents the server-driven architecture and startup flow
- Server → Security: applies · Installs protective middleware before handling requests
- Server → API: mounts · Exposes the main SaaS API surface
- Server → Admin: mounts · Exposes privileged admin functionality
- Server → Web UI: serves · Hosts the static dashboard and admin pages
- Server → WhatsApp: initializes · Restores sessions and starts device connections
- Server → WA sessions: restores · Loads persisted WhatsApp sessions from disk
- API → Users: reads/writes · Manages accounts and user-scoped state
- API → User state: reads/writes · Stores bot settings, products, and integrations
- API → Chats: reads/writes · Persists conversation history
- API → Orders: reads/writes · Manages detected and manual orders
- API → WhatsApp: controls · Links devices, sends messages, and manages sessions
- API → Bot AI: invokes · Generates prompts and bot responses
- API → Shopify: uses · Connects stores and imports products
- API → Hotmart: uses · Connects accounts and syncs catalog data
- Admin → Users: administers · Manages users and per-user limits
- Admin → User state: edits · Allows live file editing of persisted settings
- WhatsApp → WhatsApp net: connects · Maintains Baileys connections to WhatsApp
- WhatsApp → Chats: emits · Stores incoming conversation events
- WhatsApp → Bot AI: feeds · Supplies message context for automation
- Bot AI → Groq: calls · Uses Groq models for reasoning and replies
- Bot AI → Orders: detects · Creates silent orders from conversation history
- Shopify → User state: updates · Writes imported catalog data into user state
- Hotmart → User state: updates · Writes synced catalog data into user state
