# stavomidnite661/sovrfincmdctr architecture

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

- Interactive diagram: https://gitdiagram.com/stavomidnite661/sovrfincmdctr
- Repository: https://github.com/stavomidnite661/sovrfincmdctr

## Overview

1. **Overall shape**
   - This repo is a monorepo-style financial operations app centered on a **Next.js 16** web application, backed by **Prisma + SQLite**, plus a separate **Bun-based message service** for real-time queue activity.
   - The main architectural boundary is between the **dashboard/API app** on port 3000 and the **message-processing service** on port 3030.

2. **User-facing application**
   - `src/app/page.tsx` and the App Router layout form the primary dashboard UI.
   - The app is built as a financial command center: it aggregates ledger state, message queues, settlement info, and operational KPIs into one control plane.

3. **API layer inside Next.js**
   - Business logic is exposed through route handlers under `src/app/api/*`.
   - The major API domains are:
     - `ledger/*` for master reserve, internal accounts, transactions, settlements
     - `messages/*` for payment message CRUD and stats
     - `transactions/*` for transaction workflows
     - `dashboard/stats` for top-level KPIs
     - `ai/insights` for an AI-assisted analysis endpoint
   - These endpoints are the main synchronous data access path for the UI.

4. **Persistence layer**
   - `src/lib/db.ts` is the Prisma client entrypoint.
   - The schema lives in `prisma/schema.prisma`, and the repo includes SQLite database files under `db/`, notably `internal-ledger.db` and `custom.db`.
   - The persistence model centers on ledger entities such as master reserves, internal accounts, transactions, and hash-chained ledger entries.

5. **Internal ledger subsystem**
   - The ledger is the core domain model: a **master reserve account** backs multiple **internal accounts** and their balances.
   - Transaction records and immutable ledger entries provide an audit trail; the README emphasizes hash chaining and sequential entry integrity.
   - This subsystem is likely the source of truth for balances, allocations, settlements, and internal “SCreds” accounting.

6. **Message processing subsystem**
   - `mini-services/message-service/index.ts` is a separate service that handles payment message queueing and processing.
   - It exposes both **HTTP REST** and **Socket.IO** interfaces, and the README describes events like `message:create`, `message:queued`, `message:settled`, and `queue:stats`.
   - This service is the real-time boundary for message lifecycle events, separate from the main Next.js app.

7. **Real-time client flow**
   - `src/hooks/useMessageSocket.ts` is the client-side bridge to the message service.
   - The dashboard can subscribe to live queue and settlement updates via Socket.IO rather than polling the main API alone.
   - That makes the message service the real-time event source, while the Next.js APIs provide the persisted view.

8. **Gateway and routing**
   - `Caddyfile` acts as a reverse proxy gateway.
   - The intended routing splits traffic between `/api/*` to the Next.js app and `/socket.io/*` to the message service.
   - Architecturally, this creates a clean separation between browser-facing HTTP routes and the socket-based event channel.

9. **UI and component boundaries**
   - `src/components/ui/*` is a large shadcn/ui-style component library used to assemble the dashboard.
   - `src/hooks/use-mobile.ts` and `src/hooks/use-toast.ts` are supporting client hooks for responsiveness and notifications.
   - These are presentation-layer concerns and should sit above the API/service boundaries in a diagram.

10. **Auxiliary and demo code**
   - `examples/websocket/*` contains a standalone websocket demo, useful as a reference for the real-time pattern.
   - `download/` and `upload/` appear to be supporting folders, likely for file-based workflows or assets, but they are not central to the main architecture.
   - `public/` holds static assets such as branding and robots.txt.

11. **Operational tooling**
   - `.zscripts/*` contains build/start/install helpers for local and mini-service workflows.
   - `package.json`, `bun.lock`, `eslint.config.mjs`, `tailwind.config.ts`, and `next.config.ts` define the app toolchain: TypeScript, Tailwind, ESLint, and Bun-compatible scripting.
   - These are supporting infrastructure rather than runtime subsystems.

12. **Primary data flows to diagram**
   - **UI → Next.js API → Prisma → SQLite** for ledger, dashboard, and message persistence.
   - **UI → Socket.IO → message service** for live queue and settlement updates.
   - **Caddy → route splitting → Next.js / message service** as the entry gateway.
   - The repository is therefore best understood as a **dashboard + API monolith** with one **event-driven sidecar service** for real-time message processing.

Main entry points: [`src/app/page.tsx`](https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/app/page.tsx) (Dashboard), [`src/lib/db.ts`](https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/lib/db.ts) (Prisma client), [`src/app/api/ledger/transactions/route.ts`](https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/app/api/ledger/transactions/route.ts) (Ledger API), [`prisma/schema.prisma`](https://github.com/stavomidnite661/sovrfincmdctr/blob/main/prisma/schema.prisma) (Schema)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_gateway["Gateway"]
  node_caddy["Caddyfile<br/>reverse proxy"]
end

subgraph group_webapp["Next.js App"]
  node_page["Dashboard<br/>Next.js page<br/>[page.tsx]"]
  node_layout["App shell<br/>Next.js layout<br/>[layout.tsx]"]
  node_api_root["API root<br/>route entry<br/>[route.ts]"]
  node_api_dashboard["Stats API<br/>route handler<br/>[route.ts]"]
  node_api_ledger["Ledger API<br/>route handler<br/>[route.ts]"]
  node_api_messages["Messages API<br/>route handler<br/>[route.ts]"]
  node_api_tx["Tx API<br/>route handler<br/>[route.ts]"]
  node_api_ai["AI insights<br/>route handler<br/>[route.ts]"]
end

subgraph group_ledger["Ledger Domain"]
  node_db_client[("Prisma client<br/>db access<br/>[db.ts]")]
  node_schema["Schema<br/>[schema.prisma]"]
  node_sqlite[("SQLite DB<br/>database files<br/>[internal-ledger.db]")]
  node_master(("Master reserve<br/>ledger entity<br/>[schema.prisma]"))
  node_accounts(("Internal accounts<br/>ledger entity<br/>[schema.prisma]"))
  node_entries{{"Ledger entries<br/>audit trail<br/>[schema.prisma]"}}
end

subgraph group_realtime["Realtime Service"]
  node_messages_service["Message service<br/>Bun service<br/>[index.ts]"]
  node_socket(("Socket.IO<br/>realtime transport<br/>[index.ts]"))
  node_http_rest["REST API<br/>service API<br/>[index.ts]"]
end

subgraph group_ui["Presentation"]
  node_message_hook["Socket hook<br/>client hook"]
  node_ui_lib["UI library<br/>components<br/>[button.tsx]"]
  node_toast["Toasts<br/>client hooks<br/>[use-toast.ts]"]
end

node_caddy -->|"routes /"| node_page
node_caddy -->|"routes socket"| node_messages_service
node_page -->|"uses shell"| node_layout
node_page -->|"loads KPIs"| node_api_dashboard
node_page -->|"reads ledger"| node_api_ledger
node_page -->|"reads messages"| node_api_messages
node_page -->|"subscribes live"| node_message_hook
node_page -->|"composes UI"| node_ui_lib
node_page -->|"notifies"| node_toast
node_api_dashboard -->|"queries"| node_db_client
node_api_ledger -->|"queries"| node_db_client
node_api_messages -->|"queries"| node_db_client
node_api_tx -->|"mutates"| node_db_client
node_api_ai -->|"reads"| node_db_client
node_db_client -->|"uses model"| node_schema
node_schema -->|"defines"| node_master
node_schema -->|"defines"| node_accounts
node_schema -->|"defines"| node_entries
node_schema -->|"stores in"| node_sqlite
node_messages_service -->|"emits events"| node_socket
node_messages_service -->|"serves"| node_http_rest
node_message_hook -->|"connects to"| node_socket
node_api_messages -.->|"syncs view"| node_messages_service
node_api_ledger -->|"operates on"| node_master
node_api_ledger -->|"operates on"| node_accounts
node_api_ledger -->|"records"| node_entries
node_api_tx -->|"books"| node_entries

click node_caddy "https://github.com/stavomidnite661/sovrfincmdctr/tree/main/Caddyfile"
click node_page "https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/app/page.tsx"
click node_layout "https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/app/layout.tsx"
click node_api_root "https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/app/api/route.ts"
click node_api_dashboard "https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/app/api/dashboard/stats/route.ts"
click node_api_ledger "https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/app/api/ledger/transactions/route.ts"
click node_api_messages "https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/app/api/messages/route.ts"
click node_api_tx "https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/app/api/transactions/route.ts"
click node_api_ai "https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/app/api/ai/insights/route.ts"
click node_db_client "https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/lib/db.ts"
click node_schema "https://github.com/stavomidnite661/sovrfincmdctr/blob/main/prisma/schema.prisma"
click node_sqlite "https://github.com/stavomidnite661/sovrfincmdctr/blob/main/db/internal-ledger.db"
click node_master "https://github.com/stavomidnite661/sovrfincmdctr/blob/main/prisma/schema.prisma"
click node_accounts "https://github.com/stavomidnite661/sovrfincmdctr/blob/main/prisma/schema.prisma"
click node_entries "https://github.com/stavomidnite661/sovrfincmdctr/blob/main/prisma/schema.prisma"
click node_messages_service "https://github.com/stavomidnite661/sovrfincmdctr/blob/main/mini-services/message-service/index.ts"
click node_socket "https://github.com/stavomidnite661/sovrfincmdctr/blob/main/mini-services/message-service/index.ts"
click node_http_rest "https://github.com/stavomidnite661/sovrfincmdctr/blob/main/mini-services/message-service/index.ts"
click node_message_hook "https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/hooks/useMessageSocket.ts"
click node_ui_lib "https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/components/ui/button.tsx"
click node_toast "https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/hooks/use-toast.ts"

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_caddy toneBlue
class node_page,node_layout,node_api_root,node_api_dashboard,node_api_ledger,node_api_messages,node_api_tx,node_api_ai toneAmber
class node_db_client,node_schema,node_sqlite,node_master,node_accounts,node_entries toneMint
class node_messages_service,node_socket,node_http_rest toneRose
class node_message_hook,node_ui_lib,node_toast toneIndigo
```

## Components

### Gateway

Entry routing and traffic split

- [Caddyfile](https://github.com/stavomidnite661/sovrfincmdctr/tree/main/Caddyfile): reverse proxy · Routes browser and socket traffic to the appropriate runtime · `Caddyfile`

### Next.js App

Dashboard UI and synchronous API surface

- [Dashboard](https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/app/page.tsx): Next.js page · Main control-plane UI for balances, messages, settlements, and KPIs · `src/app/page.tsx`
- [App shell](https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/app/layout.tsx): Next.js layout · Global app shell and shared page structure · `src/app/layout.tsx`
- [API root](https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/app/api/route.ts): route entry · Top-level API routing boundary for the web app · `src/app/api/route.ts`
- [Stats API](https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/app/api/dashboard/stats/route.ts): route handler · Top-level dashboard KPI endpoint · `src/app/api/dashboard/stats/route.ts`
- [Ledger API](https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/app/api/ledger/transactions/route.ts): route handler · Ledger domain endpoints for master reserve, accounts, transactions, and settlements · `src/app/api/ledger/transactions/route.ts`
- [Messages API](https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/app/api/messages/route.ts): route handler · Message CRUD and message statistics endpoints · `src/app/api/messages/route.ts`
- [Tx API](https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/app/api/transactions/route.ts): route handler · Transaction workflow endpoint · `src/app/api/transactions/route.ts`
- [AI insights](https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/app/api/ai/insights/route.ts): route handler · AI-assisted analysis endpoint · `src/app/api/ai/insights/route.ts`

### Ledger Domain

Core financial persistence model

- [Prisma client](https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/lib/db.ts): db access · Shared database access entrypoint · `src/lib/db.ts`
- [Schema](https://github.com/stavomidnite661/sovrfincmdctr/blob/main/prisma/schema.prisma): Prisma schema · Data model for ledger and message persistence · `prisma/schema.prisma`
- [SQLite DB](https://github.com/stavomidnite661/sovrfincmdctr/blob/main/db/internal-ledger.db): database files · Local persisted datastore backing the ledger system · `db/internal-ledger.db`
- [Master reserve](https://github.com/stavomidnite661/sovrfincmdctr/blob/main/prisma/schema.prisma): ledger entity · Primary reserve account backing internal balances · `prisma/schema.prisma`
- [Internal accounts](https://github.com/stavomidnite661/sovrfincmdctr/blob/main/prisma/schema.prisma): ledger entity · Sub-ledger balances and allocations · `prisma/schema.prisma`
- [Ledger entries](https://github.com/stavomidnite661/sovrfincmdctr/blob/main/prisma/schema.prisma): audit trail · Hash-chained immutable entries for integrity and sequencing · `prisma/schema.prisma`

### Realtime Service

Sidecar message processor and socket event source

- [Message service](https://github.com/stavomidnite661/sovrfincmdctr/blob/main/mini-services/message-service/index.ts): Bun service · Separate real-time service for queueing and settlement events · `mini-services/message-service/index.ts`
- [Socket.IO](https://github.com/stavomidnite661/sovrfincmdctr/blob/main/mini-services/message-service/index.ts): realtime transport · Live event channel for queue and settlement updates · `mini-services/message-service/index.ts`
- [REST API](https://github.com/stavomidnite661/sovrfincmdctr/blob/main/mini-services/message-service/index.ts): service API · HTTP interface for message operations and stats · `mini-services/message-service/index.ts`

### Presentation

Reusable client-side UI and hooks

- [Socket hook](https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/hooks/useMessageSocket.ts): client hook · Browser bridge to the message service event stream · `src/hooks/useMessageSocket.ts`
- [UI library](https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/components/ui/button.tsx): components · Reusable dashboard component set · `src/components/ui/button.tsx`
- [Toasts](https://github.com/stavomidnite661/sovrfincmdctr/blob/main/src/hooks/use-toast.ts): client hooks · Notification and responsive UI helpers · `src/hooks/use-toast.ts`

## Connections

- Caddyfile → Dashboard: routes / · Gateway sends browser UI traffic to the Next.js app
- Caddyfile → Message service: routes socket · Gateway sends socket traffic to the realtime service
- Dashboard → App shell: uses shell · Dashboard is rendered within the shared app shell
- Dashboard → Stats API: loads KPIs · Dashboard reads aggregated operational stats
- Dashboard → Ledger API: reads ledger · Dashboard requests balances, accounts, and settlement state
- Dashboard → Messages API: reads messages · Dashboard fetches message lifecycle data
- Dashboard → Socket hook: subscribes live · Dashboard receives realtime queue and settlement updates
- Dashboard → UI library: composes UI · Dashboard is assembled from shared presentation components
- Dashboard → Toasts: notifies · Client interactions surface feedback through toast helpers
- Stats API → Prisma client: queries · Stats endpoint reads persisted operational data
- Ledger API → Prisma client: queries · Ledger endpoints access the database through Prisma
- Messages API → Prisma client: queries · Message endpoints persist and fetch message state
- Tx API → Prisma client: mutates · Transaction workflow persists through Prisma
- AI insights → Prisma client: reads · AI insights endpoint uses persisted operational context
- Prisma client → Schema: uses model · Prisma client is generated from the schema
- Schema → Master reserve: defines · Schema models the master reserve account
- Schema → Internal accounts: defines · Schema models internal account balances
- Schema → Ledger entries: defines · Schema models immutable ledger entries
- Schema → SQLite DB: stores in · Schema persists to local SQLite databases
- Message service → Socket.IO: emits events · Realtime service broadcasts lifecycle updates over Socket.IO
- Message service → REST API: serves · Realtime service exposes HTTP endpoints for message operations
- Socket hook → Socket.IO: connects to · Client hook listens to live service events
- Messages API → Message service: syncs view · Persisted message APIs complement the realtime service state
- Ledger API → Master reserve: operates on · Ledger routes manage reserve and account state
- Ledger API → Internal accounts: operates on · Ledger routes manage reserve and account state
- Ledger API → Ledger entries: records · Ledger routes append auditable entries
- Tx API → Ledger entries: books · Transaction workflow produces ledger trail entries
