# shuvodipdassowmik/somonnoy-sustc architecture

> Architecture diagram of the GitHub repository shuvodipdassowmik/somonnoy-sustc, 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/shuvodipdassowmik/somonnoy-sustc
- Repository: https://github.com/shuvodipdassowmik/somonnoy-sustc

## Overview

1. **Top-level shape**
   - The repo is a three-tier prototype: **React client** in `client/`, **Node/Express API** in `server/`, and a separate **FastAPI ML service** in `ml/`.
   - The main architecture boundary is between the interactive UI, the business/API layer, and the model-serving layer.

2. **Client subsystem**
   - The client is a **Vite + React** app that renders the landing page, auth pages, and role-specific dashboards.
   - It is organized by feature area: `auth`, `dashboard`, `operations`, `alerts`, `insights`, `management`, and `landing`.
   - Shared UI concerns live in reusable components like `PageSpinner`, `FormError`, and `ProviderLogo`, with a `DashboardLayout` wrapping authenticated screens.

3. **Client state and API access**
   - Authentication state is centralized in `AuthContext`, and feature hooks encapsulate fetching and local view logic.
   - API usage is routed through a shared client service layer in `client/src/services`, with endpoint definitions separated from request code.
   - The UI is designed around role-scoped dashboards rather than a single generic app surface.

4. **Server subsystem**
   - The Node/Express server is the main orchestration layer: it exposes routes for **auth**, **operations**, **alerts**, **management**, and **graph insights**.
   - Controllers and services are separated so that request handling, domain logic, and external integrations do not mix.
   - The server is responsible for combining database data, ML output, and Gemini-based anomaly analysis into consumable dashboard payloads.

5. **Auth and access boundaries**
   - Authentication is JWT-based with **HttpOnly cookies**, and the server enforces role- and provider-aware access through middleware.
   - `auth.middleware.js` handles protected access, `providerScope.middleware.js` limits data visibility to the authenticated provider context, and `ratelimiter.middleware.js` protects selected endpoints.
   - This makes provider, agent, analyst, and management views distinct security boundaries rather than just UI filters.

6. **Primary data store**
   - The persistent store is **Postgres**, with domain-specific schema and migrations under `server/src/db`.
   - Migrations are split by functional area: `alerts`, `management`, and `operations`.
   - Seeding/setup scripts build the synthetic demo data used by the dashboards, including provider ops and risk analyst data.

7. **Liquidity shortage prediction path**
   - The server sends recent synthetic transaction and balance records to the separate **FastAPI ML service** in `ml/`.
   - The ML side uses **LightGBM** model artifacts stored under `ml/models` and returns shortage timing, alert level, confidence, and evidence metrics.
   - The ML repo also contains training, feature engineering, and evaluation scripts, which indicates this is a standalone model-serving boundary rather than an in-process library.

8. **Transaction anomaly analysis path**
   - Risk analyst alert generation is handled in the server by `transactionAnomaly` and `riskAnomalyRefresh` services.
   - The flow fetches recent transactions, skips already-recorded alert keys, analyzes activity per agent, and uses **Gemini** for schema-validated JSON anomaly reasoning.
   - If Gemini fails or returns invalid output, the server falls back to deterministic checks, so the anomaly system has an external-AI path and a local safety net.

9. **Alerting and review workflow**
   - Alerts are first-class entities in the server, with validators, services, and routes dedicated to them.
   - The alert lifecycle is review-oriented: results are stored as pending analyst or operations alerts with evidence, uncertainty, and guidance.
   - The README makes clear this is a **decision-support** system, not an automation system that executes financial actions.

10. **Operations and provider-facing analytics**
   - The operations feature area covers provider and agent dashboards, including liquidity monitoring and the Bangladesh area heat map.
   - Supporting code includes geocode constants, summary hooks, and utilities for operations-specific aggregation.
   - This subsystem ties together operational pressure signals from the database and ML outputs for network-level coordination.

11. **Graph and peer-comparison insights**
   - The insights area is a separate analytical surface that visualizes customer-network relationships and clustering behavior.
   - The graph pipeline uses server-side graph insight services and a client `ForceGraph`/detail-panel UI to explain repeated amounts, repeated interactions, and cross-provider clusters.
   - Peer comparison analytics sit alongside this as a contextual baseline for agent behavior rather than a hard enforcement mechanism.

12. **Management and admin scope**
   - Management functionality is isolated into its own routes, controller/service, and client feature area.
   - This is the highest administrative boundary in the app, distinct from provider ops and risk analyst workflows.
   - The structure suggests user provisioning or oversight flows, not just dashboard viewing.

13. **Deployment and runtime boundary**
   - The architecture is split across three deployable surfaces: **Vercel** for the client, **Render** for the Node server, and **Render-hosted FastAPI** for ML.
   - Environment variables define the integration points: database connection, JWT secrets, Gemini key/model, client base URL, and ML API base URL.
   - This means the client never talks directly to Postgres, Gemini, or ML; all those dependencies are brokered by the server except the server-to-ML and server-to-Gemini calls.

14. **What to show in an architecture diagram**
   - The most important boxes are: **Users**, **React client**, **Express API**, **Postgres**, **FastAPI LightGBM service**, and **Gemini API**.
   - The most important arrows are:
     - client → server for authenticated dashboard requests
     - server → Postgres for operational data and alerts
     - server → ML service for shortage prediction
     - server → Gemini for anomaly reasoning
     - server → client for role-specific dashboards, alerts, insights, and graph data

Main entry points: [`client/src/App.jsx`](https://github.com/shuvodipdassowmik/somonnoy-sustc/blob/main/client/src/App.jsx) (React Client), [`client/src/services`](https://github.com/shuvodipdassowmik/somonnoy-sustc/tree/main/client/src/services) (State + API), [`server/src/app.js`](https://github.com/shuvodipdassowmik/somonnoy-sustc/blob/main/server/src/app.js) (Express API), [`server/src/services`](https://github.com/shuvodipdassowmik/somonnoy-sustc/tree/main/server/src/services) (Domain Services)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_grp_client["Client"]
  node_client_app["React Client<br/>vite-react app<br/>[App.jsx]"]
  node_client_auth["Auth UI<br/>feature area"]
  node_client_dashboards["Dashboards<br/>feature area<br/>[Dashboard.jsx]"]
  node_client_operations["Operations UI<br/>feature area"]
  node_client_alerts["Alerts UI<br/>feature area"]
  node_client_insights["Insights UI<br/>feature area"]
  node_client_management["Management UI<br/>feature area"]
  node_client_state_api["State + API<br/>shared client layer"]
end

subgraph group_grp_server["Server"]
  node_server_api["Express API<br/>node api<br/>[app.js]"]
  node_server_auth["Auth Boundary<br/>middleware + routes<br/>[auth.middleware.js]"]
  node_server_domain["Domain Services<br/>controllers + services"]
  node_server_alerts["Alerts Engine<br/>alert workflow<br/>[alert.service.js]"]
  node_server_ops["Operations Data<br/>ops + analytics"]
  node_server_graph["Graph Insights<br/>network analytics"]
  node_server_llm["Gemini Reasoning<br/>external ai<br/>[gemini.js]"]
  node_server_db[("Postgres<br/>relational store<br/>[schema.sql]")]
end

subgraph group_grp_ml["ML Service"]
  node_ml_service["FastAPI ML<br/>fastapi model service<br/>[app.py]"]
  node_ml_models["LightGBM Models<br/>model artifacts"]
  node_ml_pipeline["Training Pipeline<br/>feature engineering + training"]
end

node_users(("Users<br/>actors"))

node_users -->|"use app"| node_client_app
node_client_app -->|"auth flow"| node_client_auth
node_client_app -->|"role dashboards"| node_client_dashboards
node_client_app -->|"operations views"| node_client_operations
node_client_app -->|"alerts views"| node_client_alerts
node_client_app -->|"graph views"| node_client_insights
node_client_app -->|"admin views"| node_client_management
node_client_auth -->|"auth state"| node_client_state_api
node_client_dashboards -->|"fetch data"| node_client_state_api
node_client_operations -->|"fetch data"| node_client_state_api
node_client_alerts -->|"fetch data"| node_client_state_api
node_client_insights -->|"fetch data"| node_client_state_api
node_client_management -->|"fetch data"| node_client_state_api
node_client_app -->|"https"| node_server_api
node_server_api -->|"protect routes"| node_server_auth
node_server_api -->|"route to logic"| node_server_domain
node_server_domain -->|"alerts"| node_server_alerts
node_server_domain -->|"operations"| node_server_ops
node_server_domain -->|"insights"| node_server_graph
node_server_alerts -->|"read/write"| node_server_db
node_server_ops -->|"read"| node_server_db
node_server_graph -->|"read"| node_server_db
node_server_alerts -->|"analyze"| node_server_llm
node_server_ops -->|"predict shortage"| node_ml_service
node_ml_service -->|"load models"| node_ml_models
node_ml_service -.->|"trained by"| node_ml_pipeline
node_server_api -->|"data access"| node_server_db

click node_client_app "https://github.com/shuvodipdassowmik/somonnoy-sustc/blob/main/client/src/App.jsx"
click node_client_auth "https://github.com/shuvodipdassowmik/somonnoy-sustc/tree/main/client/src/features/auth"
click node_client_dashboards "https://github.com/shuvodipdassowmik/somonnoy-sustc/blob/main/client/src/pages/Dashboard.jsx"
click node_client_operations "https://github.com/shuvodipdassowmik/somonnoy-sustc/tree/main/client/src/features/operations"
click node_client_alerts "https://github.com/shuvodipdassowmik/somonnoy-sustc/tree/main/client/src/features/alerts"
click node_client_insights "https://github.com/shuvodipdassowmik/somonnoy-sustc/tree/main/client/src/features/insights"
click node_client_management "https://github.com/shuvodipdassowmik/somonnoy-sustc/tree/main/client/src/features/management"
click node_client_state_api "https://github.com/shuvodipdassowmik/somonnoy-sustc/tree/main/client/src/services"
click node_server_api "https://github.com/shuvodipdassowmik/somonnoy-sustc/blob/main/server/src/app.js"
click node_server_auth "https://github.com/shuvodipdassowmik/somonnoy-sustc/blob/main/server/src/middlewares/auth.middleware.js"
click node_server_domain "https://github.com/shuvodipdassowmik/somonnoy-sustc/tree/main/server/src/services"
click node_server_alerts "https://github.com/shuvodipdassowmik/somonnoy-sustc/blob/main/server/src/services/alert.service.js"
click node_server_ops "https://github.com/shuvodipdassowmik/somonnoy-sustc/blob/main/server/src/services/liquidityPayload.service.js"
click node_server_graph "https://github.com/shuvodipdassowmik/somonnoy-sustc/blob/main/server/src/services/graphInsight.service.js"
click node_server_llm "https://github.com/shuvodipdassowmik/somonnoy-sustc/blob/main/server/src/services/llm/gemini.js"
click node_server_db "https://github.com/shuvodipdassowmik/somonnoy-sustc/blob/main/server/src/db/schema.sql"
click node_ml_service "https://github.com/shuvodipdassowmik/somonnoy-sustc/blob/main/ml/app.py"
click node_ml_models "https://github.com/shuvodipdassowmik/somonnoy-sustc/tree/main/ml/models"
click node_ml_pipeline "https://github.com/shuvodipdassowmik/somonnoy-sustc/blob/main/ml/src/03_liquidity_forecast_model.py"

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_client_app,node_client_auth,node_client_dashboards,node_client_operations,node_client_alerts,node_client_insights,node_client_management,node_client_state_api toneBlue
class node_server_api,node_server_auth,node_server_domain,node_server_alerts,node_server_ops,node_server_graph,node_server_llm,node_server_db toneAmber
class node_ml_service,node_ml_models,node_ml_pipeline toneMint
class node_users toneNeutral
```

## Components

### Client

React Vite frontend and feature surfaces

- [React Client](https://github.com/shuvodipdassowmik/somonnoy-sustc/blob/main/client/src/App.jsx): vite-react app · Browser UI for auth, dashboards, alerts, insights, and management · `client/src/App.jsx`
- [Auth UI](https://github.com/shuvodipdassowmik/somonnoy-sustc/tree/main/client/src/features/auth): feature area · Login, register, and auth shell flows · `client/src/features/auth`
- [Dashboards](https://github.com/shuvodipdassowmik/somonnoy-sustc/blob/main/client/src/pages/Dashboard.jsx): feature area · Role-scoped landing and authenticated dashboard surfaces · `client/src/pages/Dashboard.jsx`
- [Operations UI](https://github.com/shuvodipdassowmik/somonnoy-sustc/tree/main/client/src/features/operations): feature area · Provider and agent operational monitoring views · `client/src/features/operations`
- [Alerts UI](https://github.com/shuvodipdassowmik/somonnoy-sustc/tree/main/client/src/features/alerts): feature area · Provider and risk analyst alert review screens · `client/src/features/alerts`
- [Insights UI](https://github.com/shuvodipdassowmik/somonnoy-sustc/tree/main/client/src/features/insights): feature area · Graph and cluster exploration interface · `client/src/features/insights`
- [Management UI](https://github.com/shuvodipdassowmik/somonnoy-sustc/tree/main/client/src/features/management): feature area · Administrative oversight and user management screens · `client/src/features/management`
- [State + API](https://github.com/shuvodipdassowmik/somonnoy-sustc/tree/main/client/src/services): shared client layer · Auth context, API service, endpoint definitions, and feature hooks · `client/src/services`

### Server

Express API, domain services, and persistence

- [Express API](https://github.com/shuvodipdassowmik/somonnoy-sustc/blob/main/server/src/app.js): node api · Main orchestration layer for auth, operations, alerts, management, and insights · `server/src/app.js`
- [Auth Boundary](https://github.com/shuvodipdassowmik/somonnoy-sustc/blob/main/server/src/middlewares/auth.middleware.js): middleware + routes · JWT cookie auth and protected access enforcement · `server/src/middlewares/auth.middleware.js`
- [Domain Services](https://github.com/shuvodipdassowmik/somonnoy-sustc/tree/main/server/src/services): controllers + services · Request handling and business logic for alerts, operations, management, and graphs · `server/src/services`
- [Alerts Engine](https://github.com/shuvodipdassowmik/somonnoy-sustc/blob/main/server/src/services/alert.service.js): alert workflow · Alert generation, validation, storage, and review lifecycle · `server/src/services/alert.service.js`
- [Operations Data](https://github.com/shuvodipdassowmik/somonnoy-sustc/blob/main/server/src/services/liquidityPayload.service.js): ops + analytics · Provider and agent operational payloads and summaries · `server/src/services/liquidityPayload.service.js`
- [Graph Insights](https://github.com/shuvodipdassowmik/somonnoy-sustc/blob/main/server/src/services/graphInsight.service.js): network analytics · Customer-network and peer-comparison insight generation · `server/src/services/graphInsight.service.js`
- [Gemini Reasoning](https://github.com/shuvodipdassowmik/somonnoy-sustc/blob/main/server/src/services/llm/gemini.js): external ai · Schema-validated anomaly reasoning with deterministic fallback support · `server/src/services/llm/gemini.js`
- [Postgres](https://github.com/shuvodipdassowmik/somonnoy-sustc/blob/main/server/src/db/schema.sql): relational store · Domain schema, migrations, and seeded demo data · `server/src/db/schema.sql`

### ML Service

Standalone FastAPI model-serving boundary

- [FastAPI ML](https://github.com/shuvodipdassowmik/somonnoy-sustc/blob/main/ml/app.py): fastapi model service · Standalone liquidity prediction service for shortage inference · `ml/app.py`
- [LightGBM Models](https://github.com/shuvodipdassowmik/somonnoy-sustc/tree/main/ml/models): model artifacts · Serialized shortage and pressure models used by the ML API · `ml/models`
- [Training Pipeline](https://github.com/shuvodipdassowmik/somonnoy-sustc/blob/main/ml/src/03_liquidity_forecast_model.py): feature engineering + training · Data generation, feature engineering, training, and evaluation scripts · `ml/src/03_liquidity_forecast_model.py`

### Other components

- **Users**: actors · End users and internal roles consuming the system

## Connections

- Users → React Client: use app · People interact with the browser UI
- React Client → Auth UI: auth flow · Login and registration screens
- React Client → Dashboards: role dashboards · Authenticated landing and dashboard surfaces
- React Client → Operations UI: operations views · Provider and agent monitoring screens
- React Client → Alerts UI: alerts views · Alert review and status screens
- React Client → Insights UI: graph views · Network and peer-comparison analysis screens
- React Client → Management UI: admin views · Management and oversight screens
- Auth UI → State + API: auth state · Auth context and form hooks
- Dashboards → State + API: fetch data · Shared client API access and hooks
- Operations UI → State + API: fetch data · Operations data access through services
- Alerts UI → State + API: fetch data · Alert-specific API access
- Insights UI → State + API: fetch data · Graph data retrieval and view state
- Management UI → State + API: fetch data · Management API access
- React Client → Express API: https · Authenticated UI requests to the API
- Express API → Auth Boundary: protect routes · JWT and provider-aware access control
- Express API → Domain Services: route to logic · Controllers delegate into domain services
- Domain Services → Alerts Engine: alerts · Alert lifecycle and review workflow
- Domain Services → Operations Data: operations · Operational summaries and dashboards
- Domain Services → Graph Insights: insights · Graph and peer-comparison payloads
- Alerts Engine → Postgres: read/write · Persist alerts and consume domain records
- Operations Data → Postgres: read · Load transactions, balances, and provider data
- Graph Insights → Postgres: read · Build relationship and clustering views from stored data
- Alerts Engine → Gemini Reasoning: analyze · Use Gemini for anomaly reasoning when needed
- Operations Data → FastAPI ML: predict shortage · Send recent records for liquidity inference
- FastAPI ML → LightGBM Models: load models · Serve LightGBM artifacts from disk
- FastAPI ML → Training Pipeline: trained by · Training and evaluation generate the model artifacts
- Express API → Postgres: data access · Shared persistence for auth, management, operations, and alerts
