# riitkagarwal/hackinmotion-ricr-him-1182 architecture

> Architecture diagram of the GitHub repository riitkagarwal/hackinmotion-ricr-him-1182, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/riitkagarwal/hackinmotion-ricr-him-1182
- Repository: https://github.com/riitkagarwal/hackinmotion-ricr-him-1182

## Overview

#### Purpose and entry points
SpenSight is a personal-finance expense analyzer: authenticated users add or import transactions, receive automated categories, budget comparisons, analytics, health scoring, and text-based financial insights. The browser entry pages are `Frontend/login.html`, `Frontend/dashboard.html`, and `Frontend/insights.html`. The Node/Express API starts at `backend/server.js`.

#### Client application
The frontend is a static Vanilla JS application, not a separate SPA build. Page scripts authenticate through the API, retain/use JWT credentials, request dashboard and insight data, and render charts/UI. Shared API access is centralized in `Frontend/js/api.js`; dashboard and insight orchestration reside in `Frontend/js/dashboard.js` and `Frontend/js/insights.js`. Styling/design-system concerns are in `Frontend/css/style.css`.

#### HTTP API and tenant boundary
Express routes divide the API by domain—authentication, transactions, accounts/categories, budgets, analytics, health, insights, and AI. Route modules dispatch to controllers, while JWT middleware protects user-scoped endpoints and enforces tenant isolation before database access. Principal paths: `backend/routes/authRoutes.js`, `backend/routes/transactionRoutes.js`, `backend/middleware/authMiddleware.js`, `backend/controllers/transactionController.js`.

#### Transaction ingestion pipeline
Transactions enter either as manual API submissions or CSV uploads. Upload middleware accepts multipart files; CSV processing is designed as an asynchronous BullMQ job path backed by Redis. The worker parses records, categorizes them, and persists results rather than requiring synchronous bulk processing. Key boundaries: upload/API (`backend/middleware/uploadMiddleware.js`, `backend/controllers/transactionController.js`), queue (`backend/queues/csvQueue.js`), and worker/import service (`backend/workers/csvWorker.js`, `backend/services/csvImportService.js`, `backend/utils/csvParser.js`).

#### Classification and financial computation
A local deterministic categorization engine normalizes transaction descriptions and maps merchants/keywords to categories, avoiding external AI providers for transaction data. Analytics and insight services aggregate persisted user transactions into category, trend, spike, budget, and recommendation data; the health controller exposes the composite 0–100 score. Core paths: `backend/services/categorizationService.js`, `backend/services/analyticsService.js`, `backend/services/insightsService.js`, `backend/controllers/healthController.js`. The separate assistant-facing logic is in `backend/services/aiEngine.js` behind `backend/routes/aiRoutes.js`.

#### Persistence, cache, and infrastructure
PostgreSQL is the source of truth, with normalized users, categories, transactions, and budgets; its schema and initialization flow are `backend/schema.sql`, `backend/config/db.js`, and `backend/scripts/initDb.js`. Redis supports both caching and BullMQ queueing through `backend/config/redis.js` and `backend/services/cacheService.js`. Runtime is Node.js 18+ with Express; deployment/container topology is represented by `backend/Dockerfile` and `docker-compose.yml`. Production infrastructure is intended for Neon PostgreSQL, Redis, and Render; secrets, CORS origins, and connection settings are environment-driven.

Main entry points: [`backend/controllers/transactionController.js`](https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/backend/controllers/transactionController.js) (Transaction controller), [`Frontend/js/api.js`](https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/Frontend/js/api.js) (Shared API client & JWT use), [`backend/queues/csvQueue.js`](https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/backend/queues/csvQueue.js) (CSV import queue), [`backend/server.js`](https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/backend/server.js) (Express server)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_client["Static browser client"]
  node_pages["Login, dashboard &amp; insights pages<br/>static HTML"]
  node_client_api["Shared API client &amp; JWT use<br/>Vanilla JS<br/>[api.js]"]
  node_dashboard_ui["Dashboard orchestration<br/>Vanilla JS<br/>[dashboard.js]"]
  node_insights_ui["Insights orchestration<br/>Vanilla JS<br/>[insights.js]"]
end

subgraph group_api["Node / Express API"]
  node_express{{"Express server<br/>Node.js entry point<br/>[server.js]"}}
  node_routes["Domain routes<br/>Express routes"]
  node_auth["JWT tenant guard<br/>Express middleware<br/>[authMiddleware.js]"]
  node_transactions["Transaction controller"]
  node_health["Health score endpoint<br/>controller"]
  node_upload["Multipart upload middleware<br/>Express middleware"]
end

subgraph group_processing["Async processing &amp; finance logic"]
  node_csv_queue["CSV import queue<br/>BullMQ queue<br/>[csvQueue.js]"]
  node_csv_worker["CSV import worker<br/>BullMQ worker<br/>[csvWorker.js]"]
  node_csv_import["CSV import service &amp; parser<br/>import pipeline"]
  node_categorization["Deterministic categorization"]
  node_finance_services["Analytics &amp; insights services<br/>financial computation"]
  node_ai_engine["Assistant AI engine<br/>[aiEngine.js]"]
end

subgraph group_data["Data infrastructure"]
  node_postgres[("PostgreSQL<br/>system of record")]
  node_redis[("Redis<br/>cache &amp; queue backend")]
  node_containers["Container deployment<br/>Docker topology"]
end

node_pages -->|"loads and calls"| node_client_api
node_dashboard_ui -->|"requests dashboard data"| node_client_api
node_insights_ui -->|"requests insight data"| node_client_api
node_client_api -->|"JWT API requests"| node_express
node_express -->|"mounts"| node_routes
node_routes -->|"protects scoped endpoints"| node_auth
node_auth -->|"authorized requests"| node_transactions
node_routes -->|"health endpoint"| node_health
node_routes -->|"AI endpoint"| node_ai_engine
node_transactions -->|"CSV submissions"| node_upload
node_upload -->|"enqueues import"| node_csv_queue
node_csv_queue -->|"jobs"| node_csv_worker
node_csv_worker -->|"processes"| node_csv_import
node_csv_import -->|"classifies records"| node_categorization
node_transactions -->|"categorizes manual entries"| node_categorization
node_categorization -->|"uses categories and persists results"| node_postgres
node_csv_import -->|"persists imported transactions"| node_postgres
node_transactions -->|"persists transactions"| node_postgres
node_finance_services -->|"aggregates user finances"| node_postgres
node_health -->|"computes score from"| node_finance_services
node_routes -->|"analytics and insights endpoints"| node_finance_services
node_csv_queue -->|"BullMQ backend"| node_redis
node_finance_services -.->|"cached results"| node_redis
node_containers -->|"runs API"| node_express
node_containers -->|"provisions"| node_postgres
node_containers -->|"provisions"| node_redis

click node_client_api "https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/Frontend/js/api.js"
click node_dashboard_ui "https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/Frontend/js/dashboard.js"
click node_insights_ui "https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/Frontend/js/insights.js"
click node_express "https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/backend/server.js"
click node_auth "https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/backend/middleware/authMiddleware.js"
click node_transactions "https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/backend/controllers/transactionController.js"
click node_health "https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/backend/controllers/healthController.js"
click node_upload "https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/backend/middleware/uploadMiddleware.js"
click node_csv_queue "https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/backend/queues/csvQueue.js"
click node_csv_worker "https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/backend/workers/csvWorker.js"
click node_categorization "https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/backend/services/categorizationService.js"
click node_ai_engine "https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/backend/services/aiEngine.js"

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_pages,node_client_api,node_dashboard_ui,node_insights_ui toneBlue
class node_express,node_routes,node_auth,node_transactions,node_health,node_upload toneAmber
class node_csv_queue,node_csv_worker,node_csv_import,node_categorization,node_finance_services,node_ai_engine toneMint
class node_postgres,node_redis,node_containers toneRose
```

## Components

### Static browser client

- **Login, dashboard & insights pages**: static HTML
- [Shared API client & JWT use](https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/Frontend/js/api.js): Vanilla JS · `Frontend/js/api.js`
- [Dashboard orchestration](https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/Frontend/js/dashboard.js): Vanilla JS · `Frontend/js/dashboard.js`
- [Insights orchestration](https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/Frontend/js/insights.js): Vanilla JS · `Frontend/js/insights.js`

### Node / Express API

- [Express server](https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/backend/server.js): Node.js entry point · `backend/server.js`
- **Domain routes**: Express routes · Authentication, transactions, accounts, budgets, analytics, health, insights, and AI endpoints.
- [JWT tenant guard](https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/backend/middleware/authMiddleware.js): Express middleware · Protects user-scoped endpoints before database access. · `backend/middleware/authMiddleware.js`
- [Transaction controller](https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/backend/controllers/transactionController.js): controller · Handles manual transaction submissions and CSV upload requests. · `backend/controllers/transactionController.js`
- [Health score endpoint](https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/backend/controllers/healthController.js): controller · Exposes the composite 0–100 financial health score. · `backend/controllers/healthController.js`
- [Multipart upload middleware](https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/backend/middleware/uploadMiddleware.js): Express middleware · `backend/middleware/uploadMiddleware.js`

### Async processing & finance logic

- [CSV import queue](https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/backend/queues/csvQueue.js): BullMQ queue · `backend/queues/csvQueue.js`
- [CSV import worker](https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/backend/workers/csvWorker.js): BullMQ worker · Processes imports asynchronously. · `backend/workers/csvWorker.js`
- **CSV import service & parser**: import pipeline · Parses records, categorizes them, and persists transactions.
- [Deterministic categorization](https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/backend/services/categorizationService.js): service · Normalizes descriptions and maps merchant keywords locally. · `backend/services/categorizationService.js`
- **Analytics & insights services**: financial computation · Builds category, trend, spike, budget, and recommendation data.
- [Assistant AI engine](https://github.com/riitkagarwal/hackinmotion-ricr-him-1182/blob/main/backend/services/aiEngine.js): service · Separate assistant-facing logic. · `backend/services/aiEngine.js`

### Data infrastructure

- **PostgreSQL**: system of record · Normalized users, categories, transactions, and budgets.
- **Redis**: cache & queue backend · Supports cached responses and BullMQ.
- **Container deployment**: Docker topology · Node API runs with PostgreSQL and Redis infrastructure.

## Connections

- Login, dashboard & insights pages → Shared API client & JWT use: loads and calls
- Dashboard orchestration → Shared API client & JWT use: requests dashboard data
- Insights orchestration → Shared API client & JWT use: requests insight data
- Shared API client & JWT use → Express server: JWT API requests
- Express server → Domain routes: mounts
- Domain routes → JWT tenant guard: protects scoped endpoints
- JWT tenant guard → Transaction controller: authorized requests
- Domain routes → Health score endpoint: health endpoint
- Domain routes → Assistant AI engine: AI endpoint
- Transaction controller → Multipart upload middleware: CSV submissions
- Multipart upload middleware → CSV import queue: enqueues import
- CSV import queue → CSV import worker: jobs
- CSV import worker → CSV import service & parser: processes
- CSV import service & parser → Deterministic categorization: classifies records
- Transaction controller → Deterministic categorization: categorizes manual entries
- Deterministic categorization → PostgreSQL: uses categories and persists results
- CSV import service & parser → PostgreSQL: persists imported transactions
- Transaction controller → PostgreSQL: persists transactions
- Analytics & insights services → PostgreSQL: aggregates user finances
- Health score endpoint → Analytics & insights services: computes score from
- Domain routes → Analytics & insights services: analytics and insights endpoints
- CSV import queue → Redis: BullMQ backend
- Analytics & insights services → Redis: cached results
- Container deployment → Express server: runs API
- Container deployment → PostgreSQL: provisions
- Container deployment → Redis: provisions
