# ayhanarashtasin/sust-ai-hackathon-onsiteround architecture

> Architecture diagram of the GitHub repository ayhanarashtasin/sust-ai-hackathon-onsiteround, 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/ayhanarashtasin/sust-ai-hackathon-onsiteround
- Repository: https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround

## Overview

1. **Repo shape and runtimes**
   - This is a three-part system: a **React/Vite client** (`client/`), a **Node/Express API** (`server/`), and a **Python ML/training package** (`ml/`).
   - The application data lives in **MongoDB via Mongoose**. The client never talks to MongoDB directly; all reads and writes go through the API.

2. **Primary product flow**
   - The core loop is: **login → select agent outlet → view balances/forecast/alerts → inspect case details → take case actions**.
   - The UI is designed as a **read-heavy console**: the client polls the server for fresh state, while the server computes analytics and persists results.

3. **Client boundary**
   - The client is a dashboard-style SPA with pages for **Login, Dashboard, Agent Detail, Case View, and Service Status**.
   - It uses a shared API client, JWT auth context, polling hooks, and localized strings in **English, Bengali, and Banglish**.
   - The client is intentionally **read-only for decision data**; it does not generate alerts or run ML.

4. **Server boundary**
   - The Express server is the system of record for **auth, agent state, alerts, case workflow, simulation control, and model status**.
   - JWT middleware attaches **role, area, and provider scope** to the request context, and controllers enforce those boundaries on every query and mutation.
   - Server-side scoping is important: outlets, areas, and providers are not merged even if the UI shows them together.

5. **Data model**
   - Main Mongo collections are **Agent, Transaction, Alert, Prediction, and User**.
   - `Agent` stores one shared **cash drawer** plus separate **e-money balances per provider**.
   - `Alert` stores case state, audit history, and evidence snapshots; `Transaction` stores signed balance deltas; `Prediction` is the audit log for model outputs.

6. **Simulation engine as the data producer**
   - The repo is built around a **simulated live environment** rather than real integrations.
   - `server/services/simEngine.js` generates scenario transactions, applies them atomically to agent balances, runs analytics, and upserts alerts on each tick.
   - This is the main source of state change in the system; the UI mostly observes its output.

7. **Analytics pipeline**
   - The decision-support path is assembled in `server/services/ml/decisionSupport.js`.
   - It combines:
     - **data quality checks** for stale/missing/mismatched feeds,
     - **forecasting** for cash and each provider’s e-money,
     - **statistical anomaly detection**,
     - **rule engine output**,
     - **tabular ML predictions**,
     - and a **hybrid decision combiner**.
   - The resulting outputs are written back as alert/prediction state for the client to display.

8. **Forecast subsystem**
   - Liquidity forecasting is per resource: **shared cash + separate provider balances**.
   - It estimates burn rate, depletion ETA, confidence, and top-up guidance.
   - A key boundary is that **bad or inconsistent data suppresses recommendations** instead of producing confident advice.

9. **Anomaly and evidence subsystem**
   - Anomaly detection focuses on two behaviors: **velocity spikes** and **repeated near-identical amounts**.
   - Results are classified into review-worthy findings versus a benign **demand_surge** category.
   - Evidence is made human-readable through an explanation layer and kept tied to specific transactions and data freshness signals.

10. **Case workflow and governance**
   - Alerts are not just notifications; they are **cases** with a validated lifecycle: acknowledge, assign, escalate, resolve, dismiss.
   - Role permissions differ by staff type: agent, field officer, ops, risk, and management.
   - Every action appends to an audit trail, so the system supports **coordination and traceability**, not automated enforcement.

11. **Safety and language controls**
   - The system is intentionally constrained to **advisory-only behavior**.
   - A runtime language guard blocks prohibited terms, and alert prose is produced via **OpenAI when available** or deterministic templates otherwise.
   - This means the explanation layer is a guarded downstream formatting step, not the source of truth for risk.

12. **Seed data, demo data, and offline training**
   - The repo ships with **deterministic synthetic sample data** in `data/` and database seeding scripts in `server/scripts/`.
   - The `ml/` package contains **training artifacts, evaluation outputs, and tests** for anomaly/liquidity models; it is separate from the runtime API path.
   - In production-like demo mode, the server consumes these artifacts for inference but does not depend on a separate ML service.

13. **Operational tooling and validation**
   - There are dedicated scripts for **seeding, validation, sample-data generation, and HTTP latency measurement**.
   - GitHub Actions runs **SonarCloud** analysis.
   - The test suite is broad across server services, controllers, workflow, simulation, and ML helpers, which makes the architecture heavily exercised around the server-side analytics pipeline.

14. **What an architecture diagram should emphasize**
   - Draw the system as:
     - **Client UI** polling
     - **Express API** enforcing auth/scope
     - **Simulation + analytics pipeline** producing alerts and forecasts
     - **MongoDB** storing outlet, transaction, alert, prediction, and user state
     - **Offline Python ML training/artifacts** feeding runtime inference
   - The most important boundary is that **the server owns all authoritative logic and state transitions**; the browser is a consumer of computed results.

Main entry points: [`server/services/ml/decisionSupport.js`](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/server/services/ml/decisionSupport.js) (Decision Support), [`server/controllers`](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/tree/main/server/controllers) (Domain Controllers), [`server/models`](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/tree/main/server/models) (Mongo Models), [`server/services/simEngine.js`](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/server/services/simEngine.js) (Simulation Engine)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Client UI"]
  node_client_app["App Shell<br/>SPA router<br/>[App.jsx]"]
  node_client_auth["Auth Context<br/>JWT session<br/>[AuthContext.jsx]"]
  node_client_api["API Client<br/>HTTP adapter<br/>[client.js]"]
  node_client_polling["Polling Hooks<br/>refresh loop<br/>[usePolling.js]"]
  node_client_pages["Dashboard Pages<br/>screens"]
  node_client_ui["Decision UI<br/>read-only console"]
end

subgraph group_server["Express API"]
  node_server_routes["API Routes<br/>Express routes"]
  node_server_auth["Auth Middleware<br/>JWT scope guard<br/>[auth.js]"]
  node_server_models[("Mongo Models<br/>Mongoose schemas")]
  node_server_sim["Simulation Engine<br/>state producer<br/>[simEngine.js]"]
  node_server_decision["Decision Support<br/>analytics pipeline<br/>[decisionSupport.js]"]
  node_server_forecast["Forecasting<br/>liquidity model<br/>[forecast.js]"]
  node_server_anomaly["Anomaly Detection<br/>signal detector<br/>[anomaly.js]"]
  node_server_rules["Rule Engine<br/>policy logic<br/>[ruleEngine.js]"]
  node_server_workflow["Case Workflow<br/>lifecycle<br/>[caseWorkflow.js]"]
  node_server_explain["Explanation Layer<br/>guarded prose<br/>[explain.js]"]
  node_server_live["Live Updates<br/>status feed<br/>[liveUpdates.js]"]
  node_server_controller["Domain Controllers<br/>HTTP handlers"]
  node_server_config["Decision Config<br/>runtime settings<br/>[decisionConfig.js]"]
end

subgraph group_ml["Offline ML"]
  node_ml_pkg["Python Package<br/>training code"]
  node_ml_artifacts[("Model Artifacts<br/>packaged models")]
end

subgraph group_data["Data & Artifacts"]
  node_seed_data["Sample Data<br/>fixtures"]
end

node_client_app -->|"routes"| node_client_pages
node_client_app -->|"session"| node_client_auth
node_client_pages -->|"compose"| node_client_ui
node_client_ui -->|"fetch"| node_client_api
node_client_polling -->|"refresh"| node_client_api
node_client_api -->|"http"| node_server_routes
node_server_routes -->|"protect"| node_server_auth
node_server_routes -->|"dispatch"| node_server_controller
node_server_controller -->|"control"| node_server_sim
node_server_controller -->|"case ops"| node_server_workflow
node_server_controller -->|"analyze"| node_server_decision
node_server_controller -->|"status"| node_server_live
node_server_sim -->|"persist"| node_server_models
node_server_sim -->|"produce"| node_server_decision
node_server_decision -->|"forecast"| node_server_forecast
node_server_decision -->|"detect"| node_server_anomaly
node_server_decision -->|"rule score"| node_server_rules
node_server_decision -->|"explain"| node_server_explain
node_server_decision -->|"write back"| node_server_models
node_server_workflow -->|"audit"| node_server_models
node_server_live -->|"serve refresh"| node_client_polling
node_ml_pkg -->|"train"| node_ml_artifacts
node_server_decision -->|"infer"| node_ml_artifacts
node_seed_data -->|"seed"| node_server_sim
node_seed_data -->|"bootstrap"| node_server_models
node_server_config -->|"tune"| node_server_decision
node_server_explain -->|"annotate"| node_server_models

click node_client_app "https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/client/src/App.jsx"
click node_client_auth "https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/client/src/context/AuthContext.jsx"
click node_client_api "https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/client/src/api/client.js"
click node_client_polling "https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/client/src/hooks/usePolling.js"
click node_client_pages "https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/tree/main/client/src/pages"
click node_client_ui "https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/tree/main/client/src/components"
click node_server_routes "https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/tree/main/server/routes"
click node_server_auth "https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/server/middleware/auth.js"
click node_server_models "https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/tree/main/server/models"
click node_server_sim "https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/server/services/simEngine.js"
click node_server_decision "https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/server/services/ml/decisionSupport.js"
click node_server_forecast "https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/server/services/forecast.js"
click node_server_anomaly "https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/server/services/anomaly.js"
click node_server_rules "https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/server/services/rules/ruleEngine.js"
click node_server_workflow "https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/server/services/caseWorkflow.js"
click node_server_explain "https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/server/services/explain.js"
click node_server_live "https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/server/services/liveUpdates.js"
click node_server_controller "https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/tree/main/server/controllers"
click node_server_config "https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/server/config/decisionConfig.js"
click node_ml_pkg "https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/tree/main/ml/sust_ml"
click node_ml_artifacts "https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/tree/main/ml/artifacts"
click node_seed_data "https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/tree/main/data/sample"

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_api,node_client_polling,node_client_pages,node_client_ui toneBlue
class node_server_routes,node_server_auth,node_server_models,node_server_sim,node_server_decision,node_server_forecast,node_server_anomaly,node_server_rules,node_server_workflow,node_server_explain,node_server_live,node_server_controller,node_server_config toneAmber
class node_ml_pkg,node_ml_artifacts toneMint
class node_seed_data toneRose
```

## Components

### Client UI

React/Vite dashboard SPA that polls the API and renders decision-support state.

- [App Shell](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/client/src/App.jsx): SPA router · Top-level client composition and page routing. · `client/src/App.jsx`
- [Auth Context](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/client/src/context/AuthContext.jsx): JWT session · Holds login state and bearer token handling for the UI. · `client/src/context/AuthContext.jsx`
- [API Client](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/client/src/api/client.js): HTTP adapter · Shared browser client for API requests. · `client/src/api/client.js`
- [Polling Hooks](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/client/src/hooks/usePolling.js): refresh loop · Client-side refresh cadence for live dashboard state. · `client/src/hooks/usePolling.js`
- [Dashboard Pages](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/tree/main/client/src/pages): screens · Login, dashboard, agent detail, case view, and service status screens. · `client/src/pages`
- [Decision UI](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/tree/main/client/src/components): read-only console · Balance, forecast, alerts, timeline, explanation, and model status components. · `client/src/components`

### Express API

Authoritative runtime for auth, scoping, simulation, case workflow, and analytics.

- [API Routes](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/tree/main/server/routes): Express routes · HTTP boundary for auth, agents, alerts, model status, public endpoints, and simulation control. · `server/routes`
- [Auth Middleware](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/server/middleware/auth.js): JWT scope guard · Attaches and enforces role, area, and provider scope on requests. · `server/middleware/auth.js`
- [Mongo Models](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/tree/main/server/models): Mongoose schemas · Agent, Alert, Transaction, Prediction, and User persistence models. · `server/models`
- [Simulation Engine](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/server/services/simEngine.js): state producer · Generates scenario transactions, applies atomic balance changes, and triggers analytics on each tick. · `server/services/simEngine.js`
- [Decision Support](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/server/services/ml/decisionSupport.js): analytics pipeline · Combines data quality, forecasting, anomaly detection, rules, tabular ML, and hybrid scoring. · `server/services/ml/decisionSupport.js`
- [Forecasting](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/server/services/forecast.js): liquidity model · Estimates burn rate, depletion ETA, confidence, and top-up guidance for cash and provider balances. · `server/services/forecast.js`
- [Anomaly Detection](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/server/services/anomaly.js): signal detector · Finds velocity spikes and repeated near-identical amounts for review-worthy evidence. · `server/services/anomaly.js`
- [Rule Engine](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/server/services/rules/ruleEngine.js): policy logic · Deterministic rule definitions and case classification logic. · `server/services/rules/ruleEngine.js`
- [Case Workflow](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/server/services/caseWorkflow.js): lifecycle · Acknowledgement, assignment, escalation, resolution, dismissal, and audit trail handling. · `server/services/caseWorkflow.js`
- [Explanation Layer](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/server/services/explain.js): guarded prose · Human-readable alert narration with language guard and optional OpenAI-backed phrasing. · `server/services/explain.js`
- [Live Updates](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/server/services/liveUpdates.js): status feed · Server-side state refresh and publication helpers for client polling. · `server/services/liveUpdates.js`
- [Domain Controllers](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/tree/main/server/controllers): HTTP handlers · Agent, alert, decision, model, sim, auth, and public controllers that coordinate service calls. · `server/controllers`
- [Decision Config](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/blob/main/server/config/decisionConfig.js): runtime settings · Server configuration for model/runtime behavior and decision thresholds. · `server/config/decisionConfig.js`

### Offline ML

Python training and packaged artifacts used for inference in the server runtime.

- [Python Package](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/tree/main/ml/sust_ml): training code · Offline data prep, metrics, and pipeline code for model training and validation. · `ml/sust_ml`
- [Model Artifacts](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/tree/main/ml/artifacts): packaged models · Versioned anomaly and liquidity artifacts, manifests, metrics, and model cards. · `ml/artifacts`

### Data & Artifacts

Seed data and persisted model/sample artifacts used by the app and tooling.

- [Sample Data](https://github.com/ayhanarashtasin/sust-ai-hackathon-onsiteround/tree/main/data/sample): fixtures · Deterministic demo agents, transactions, users, and ML manifests. · `data/sample`

## Connections

- App Shell → Dashboard Pages: routes · App shell renders the page-level experience.
- App Shell → Auth Context: session · UI reads login and token state.
- Dashboard Pages → Decision UI: compose · Pages assemble the dashboard components.
- Decision UI → API Client: fetch · Visuals depend on API-backed decision data.
- Polling Hooks → API Client: refresh · Polling drives repeated reads from the server.
- API Client → API Routes: http · All browser reads and writes go through the API.
- API Routes → Auth Middleware: protect · Routes are wrapped by JWT scope enforcement.
- API Routes → Domain Controllers: dispatch · Route handlers delegate into domain controllers.
- Domain Controllers → Simulation Engine: control · Simulation endpoints start and manage the live scenario engine.
- Domain Controllers → Case Workflow: case ops · Alert actions mutate case lifecycle state.
- Domain Controllers → Decision Support: analyze · Decision endpoints compute model-backed state.
- Domain Controllers → Live Updates: status · Controllers expose current system state to the UI.
- Simulation Engine → Mongo Models: persist · Ticks apply transactions and update alert/state documents.
- Simulation Engine → Decision Support: produce · Each tick triggers analytics and alert generation.
- Decision Support → Forecasting: forecast · Liquidity forecasts are a core input to the hybrid output.
- Decision Support → Anomaly Detection: detect · Anomaly findings contribute evidence and case signals.
- Decision Support → Rule Engine: rule score · Rule outputs shape advisory classification.
- Decision Support → Explanation Layer: explain · Final outputs are rendered into guarded prose and evidence.
- Decision Support → Mongo Models: write back · Predictions and alerts are persisted as system state.
- Case Workflow → Mongo Models: audit · Case transitions append to alert history and audit trails.
- Live Updates → Polling Hooks: serve refresh · Live status endpoints support the client polling loop.
- Python Package → Model Artifacts: train · Offline training produces packaged inference artifacts.
- Decision Support → Model Artifacts: infer · Runtime loads packaged model outputs for tabular inference.
- Sample Data → Simulation Engine: seed · Deterministic fixtures feed demo and validation scenarios.
- Sample Data → Mongo Models: bootstrap · Sample data initializes collections for demo mode.
- Decision Config → Decision Support: tune · Decision thresholds and runtime settings shape analytics behavior.
- Explanation Layer → Mongo Models: annotate · Explanations are tied back to alerts and predictions.
