# park-bit/scrawny-spend architecture

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

- Interactive diagram: https://gitdiagram.com/park-bit/scrawny-spend
- Repository: https://github.com/park-bit/scrawny-spend

## Overview

1. **Repository shape and deployment split**
   This is a three-part monorepo: a **React/Vite frontend** in `frontend`, a **Node/Express API** in `backend`, and a **Python Flask AI service** in `ai-engine`. The README indicates the intended deployment split is **Netlify for the frontend**, **Render for the backend and AI service**, and **MongoDB Atlas** as the shared data store.

2. **Frontend boundary: presentation and client-side state**
   The frontend is a single-page app built with **React**, **Vite**, and **Tailwind CSS**. Its job is presentation and user interaction: dashboards, expense entry, history, analytics, settings, login/register/forgot-password, and AI insights. The component tree is organized by feature (`pages`, `components/expenses`, `components/charts`, `components/layout`) rather than by technical layer alone.

3. **Frontend data access and state**
   The frontend talks only to the backend through `frontend/src/services/api.js` and related service modules. It keeps auth/session state in a **Zustand store** (`store/authStore.js`) and likely uses custom hooks (`useExpenses`, `useAnalytics`) to encapsulate fetching and derived state. This keeps UI components thin and makes the backend the single source of truth.

4. **Backend as the application gateway**
   The Node backend is the main application API and request boundary. `server.js` and `src/app.js` wire the Express app, route registration, middleware, database connection, and error handling. The API surface is split into domains: **auth, expenses, budgets, analytics, reports, and AI**.

5. **Backend domain layering**
   The backend follows a common controller/service/model structure:
   - `routes` define HTTP endpoints
   - `controllers` translate requests into service calls
   - `services` hold business logic and integrations
   - `models` define MongoDB entities through Mongoose
   - `validators` and middleware enforce request shape and security  
   This separation is important for the architecture diagram because the backend is not just a thin proxy; it contains business rules, aggregation logic, and integration orchestration.

6. **Core persistence model**
   The data layer is MongoDB, accessed through Mongoose models such as `User`, `Expense`, `Budget`, `Prediction`, and `Anomaly`. The presence of `Prediction` and `Anomaly` models means the AI outputs are persisted alongside normal finance data, not recomputed every time the UI opens. `aggregationPipelines.js` suggests analytics and reporting are partially computed from MongoDB aggregations.

7. **Auth and user lifecycle**
   Authentication is handled in the backend with dedicated auth routes, controller, service, and validators, plus middleware for authorization. The repo includes email/notification services, so password reset or OTP-style verification is part of the system’s boundary. This auth flow is separate from finance data operations and should be shown as an upstream gate to the rest of the API.

8. **Finance workflows: expenses, budgets, analytics, reports**
   Expense creation and retrieval are the primary business workflows. Budgeting, analytics, and reports sit on top of the expense data. The backend likely computes summary views via aggregation pipelines and service methods, then returns chart-ready JSON for the frontend’s dashboards and analytics pages.

9. **AI integration is a separate service boundary**
   The backend does not appear to run ML in-process. Instead, `src/services/aiService.js` and the README indicate it acts as an **HTTP client/bridge** to the Python AI engine. This makes the AI component an independently deployable subsystem, with the Node backend responsible for triggering inference and serving stored AI results back to the UI.

10. **AI engine internals**
    The Python service is a Flask API with route modules for **classification**, **prediction**, and **anomaly detection**. Its model assets include a serialized classifier, a TensorFlow/Keras predictor, and an anomaly detector. `utils/preprocessor.py` suggests inputs are normalized before inference, which is a key boundary in the AI subsystem.

11. **AI data flow**
    The AI engine is designed around recent financial context pulled from MongoDB, then runs inference and stores outputs. The README describes a non-blocking flow where an expense is saved first, then the backend triggers AI processing asynchronously, and finally the results are written back as predictions/anomalies for later retrieval.

12. **Cross-cutting backend infrastructure**
    Several backend modules support system-wide concerns: `rateLimiter` for abuse control, `auth` middleware for protected routes, `validate` for request validation, and `errorHandler` plus `AppError` for consistent failure handling. `logger.js` and `responseHelper.js` indicate the API is standardized around structured responses and observable errors.

13. **External integrations**
    Two external integrations matter architecturally: **MongoDB Atlas** as the persistent store, and an email/notification path used by auth or user messaging flows. There is also a `geminiService.js`, which indicates an LLM integration exists as a separate service concern, even if it is not the core data path.

14. **What the main diagram should emphasize**
    The key architecture to draw is: **React client → Express API → MongoDB**, with a side branch from the Express API to the **Python AI service**, and the AI service reading/writing finance insights back to MongoDB. The frontend should be shown as a consumer of backend APIs only, while the backend should be shown as the orchestrator for auth, finance operations, analytics, and AI triggering.

Main entry points: [`backend/src/app.js`](https://github.com/park-bit/scrawny-spend/blob/main/backend/src/app.js) (Express app), [`backend/src/models`](https://github.com/park-bit/scrawny-spend/tree/main/backend/src/models) (Domain models), [`frontend/src/pages`](https://github.com/park-bit/scrawny-spend/tree/main/frontend/src/pages) (Pages), [`backend/src/services/aiService.js`](https://github.com/park-bit/scrawny-spend/blob/main/backend/src/services/aiService.js) (AI bridge)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_group_frontend["Frontend"]
  node_frontend_app["React app<br/>SPA shell<br/>[App.jsx]"]
  node_frontend_pages["Pages<br/>screens"]
  node_frontend_components["UI components<br/>view components"]
  node_frontend_hooks["Client hooks<br/>state hooks"]
  node_frontend_api["API client<br/>service layer<br/>[api.js]"]
  node_frontend_store[("Auth store<br/>zustand state<br/>[authStore.js]")]
end

subgraph group_group_backend["Backend"]
  node_backend_entry["Server entry<br/>node bootstrap<br/>[server.js]"]
  node_backend_app["Express app<br/>api shell<br/>[app.js]"]
  node_backend_auth["Auth domain<br/>controller-service<br/>[auth.controller.js]"]
  node_backend_expenses["Expenses domain<br/>controller-service"]
  node_backend_budgets["Budgets domain<br/>controller-service"]
  node_backend_analytics["Analytics domain<br/>aggregation service"]
  node_backend_reports["Reports domain<br/>reporting"]
  node_backend_ai_bridge["AI bridge<br/>integration service<br/>[aiService.js]"]
  node_backend_db[("MongoDB<br/>persistence<br/>[db.js]")]
  node_backend_models[("Domain models<br/>mongoose models")]
  node_backend_utils["Shared infra<br/>cross-cutting"]
end

subgraph group_group_ai["AI Engine"]
  node_ai_app["Flask app<br/>python api<br/>[app.py]"]
  node_ai_routes["AI routes<br/>inference endpoints"]
  node_ai_preprocess["Preprocessor<br/>feature prep<br/>[preprocessor.py]"]
  node_ai_models[("ML models<br/>trained assets")]
end

node_frontend_pages -->|"renders"| node_frontend_components
node_frontend_app -->|"routes"| node_frontend_pages
node_frontend_app -->|"reads auth"| node_frontend_store
node_frontend_pages -->|"uses"| node_frontend_hooks
node_frontend_hooks -->|"calls"| node_frontend_api
node_frontend_components -->|"submit data"| node_frontend_api
node_frontend_api -->|"HTTP"| node_backend_app
node_backend_entry -->|"boots"| node_backend_app
node_backend_app -->|"middleware"| node_backend_utils
node_backend_app -->|"mounts"| node_backend_auth
node_backend_app -->|"mounts"| node_backend_expenses
node_backend_app -->|"mounts"| node_backend_budgets
node_backend_app -->|"mounts"| node_backend_analytics
node_backend_app -->|"mounts"| node_backend_reports
node_backend_app -->|"mounts"| node_backend_ai_bridge
node_backend_auth -->|"uses"| node_backend_models
node_backend_expenses -->|"uses"| node_backend_models
node_backend_budgets -->|"uses"| node_backend_models
node_backend_analytics -->|"aggregates"| node_backend_db
node_backend_reports -->|"reads"| node_backend_db
node_backend_ai_bridge -->|"HTTP"| node_ai_app
node_ai_app -->|"serves"| node_ai_routes
node_ai_routes -->|"prep"| node_ai_preprocess
node_ai_routes -->|"runs"| node_ai_models
node_ai_app -->|"reads/writes"| node_backend_db
node_backend_ai_bridge -->|"stores outputs"| node_backend_models

click node_frontend_app "https://github.com/park-bit/scrawny-spend/blob/main/frontend/src/App.jsx"
click node_frontend_pages "https://github.com/park-bit/scrawny-spend/tree/main/frontend/src/pages"
click node_frontend_components "https://github.com/park-bit/scrawny-spend/tree/main/frontend/src/components"
click node_frontend_hooks "https://github.com/park-bit/scrawny-spend/tree/main/frontend/src/hooks"
click node_frontend_api "https://github.com/park-bit/scrawny-spend/blob/main/frontend/src/services/api.js"
click node_frontend_store "https://github.com/park-bit/scrawny-spend/blob/main/frontend/src/store/authStore.js"
click node_backend_entry "https://github.com/park-bit/scrawny-spend/blob/main/backend/server.js"
click node_backend_app "https://github.com/park-bit/scrawny-spend/blob/main/backend/src/app.js"
click node_backend_auth "https://github.com/park-bit/scrawny-spend/blob/main/backend/src/controllers/auth.controller.js"
click node_backend_expenses "https://github.com/park-bit/scrawny-spend/blob/main/backend/src/controllers/expense.controller.js"
click node_backend_budgets "https://github.com/park-bit/scrawny-spend/blob/main/backend/src/controllers/budget.controller.js"
click node_backend_analytics "https://github.com/park-bit/scrawny-spend/blob/main/backend/src/controllers/analytics.controller.js"
click node_backend_reports "https://github.com/park-bit/scrawny-spend/blob/main/backend/src/controllers/report.controller.js"
click node_backend_ai_bridge "https://github.com/park-bit/scrawny-spend/blob/main/backend/src/services/aiService.js"
click node_backend_db "https://github.com/park-bit/scrawny-spend/blob/main/backend/src/config/db.js"
click node_backend_models "https://github.com/park-bit/scrawny-spend/tree/main/backend/src/models"
click node_backend_utils "https://github.com/park-bit/scrawny-spend/tree/main/backend/src/utils"
click node_ai_app "https://github.com/park-bit/scrawny-spend/blob/main/ai-engine/app.py"
click node_ai_routes "https://github.com/park-bit/scrawny-spend/tree/main/ai-engine/routes"
click node_ai_preprocess "https://github.com/park-bit/scrawny-spend/blob/main/ai-engine/utils/preprocessor.py"
click node_ai_models "https://github.com/park-bit/scrawny-spend/tree/main/ai-engine/models"

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_frontend_app,node_frontend_pages,node_frontend_components,node_frontend_hooks,node_frontend_api,node_frontend_store toneBlue
class node_backend_entry,node_backend_app,node_backend_auth,node_backend_expenses,node_backend_budgets,node_backend_analytics,node_backend_reports,node_backend_ai_bridge,node_backend_db,node_backend_models,node_backend_utils toneAmber
class node_ai_app,node_ai_routes,node_ai_preprocess,node_ai_models toneMint
```

## Components

### Frontend

React/Vite client app

- [React app](https://github.com/park-bit/scrawny-spend/blob/main/frontend/src/App.jsx): SPA shell · Vite entry and app composition · `frontend/src/App.jsx`
- [Pages](https://github.com/park-bit/scrawny-spend/tree/main/frontend/src/pages): screens · Dashboard, auth, settings, analytics and history screens · `frontend/src/pages`
- [UI components](https://github.com/park-bit/scrawny-spend/tree/main/frontend/src/components): view components · Charts, expense widgets, layout and common UI pieces · `frontend/src/components`
- [Client hooks](https://github.com/park-bit/scrawny-spend/tree/main/frontend/src/hooks): state hooks · Derived data and fetch orchestration for expenses and analytics · `frontend/src/hooks`
- [API client](https://github.com/park-bit/scrawny-spend/blob/main/frontend/src/services/api.js): service layer · HTTP access to backend endpoints · `frontend/src/services/api.js`
- [Auth store](https://github.com/park-bit/scrawny-spend/blob/main/frontend/src/store/authStore.js): zustand state · Session and auth state · `frontend/src/store/authStore.js`

### Backend

Express API and domain services

- [Server entry](https://github.com/park-bit/scrawny-spend/blob/main/backend/server.js): node bootstrap · Process startup and app wiring · `backend/server.js`
- [Express app](https://github.com/park-bit/scrawny-spend/blob/main/backend/src/app.js): api shell · Middleware, routes, db hookup and error handling · `backend/src/app.js`
- [Auth domain](https://github.com/park-bit/scrawny-spend/blob/main/backend/src/controllers/auth.controller.js): controller-service · Login, registration, password reset and protection · `backend/src/controllers/auth.controller.js`
- [Expenses domain](https://github.com/park-bit/scrawny-spend/blob/main/backend/src/controllers/expense.controller.js): controller-service · Expense CRUD and business rules · `backend/src/controllers/expense.controller.js`
- [Budgets domain](https://github.com/park-bit/scrawny-spend/blob/main/backend/src/controllers/budget.controller.js): controller-service · Budget tracking over expense data · `backend/src/controllers/budget.controller.js`
- [Analytics domain](https://github.com/park-bit/scrawny-spend/blob/main/backend/src/controllers/analytics.controller.js): aggregation service · Summary metrics and chart-ready responses · `backend/src/controllers/analytics.controller.js`
- [Reports domain](https://github.com/park-bit/scrawny-spend/blob/main/backend/src/controllers/report.controller.js): reporting · Report generation over finance data · `backend/src/controllers/report.controller.js`
- [AI bridge](https://github.com/park-bit/scrawny-spend/blob/main/backend/src/services/aiService.js): integration service · Triggers Python inference and persists results · `backend/src/services/aiService.js`
- [MongoDB](https://github.com/park-bit/scrawny-spend/blob/main/backend/src/config/db.js): persistence · Shared document store via Mongoose · `backend/src/config/db.js`
- [Domain models](https://github.com/park-bit/scrawny-spend/tree/main/backend/src/models): mongoose models · User, expense, budget, prediction and anomaly entities · `backend/src/models`
- [Shared infra](https://github.com/park-bit/scrawny-spend/tree/main/backend/src/utils): cross-cutting · Validation, auth, logging, errors and aggregation pipelines · `backend/src/utils`

### AI Engine

Flask inference service

- [Flask app](https://github.com/park-bit/scrawny-spend/blob/main/ai-engine/app.py): python api · Inference service entry and routing · `ai-engine/app.py`
- [AI routes](https://github.com/park-bit/scrawny-spend/tree/main/ai-engine/routes): inference endpoints · Classification, prediction and anomaly APIs · `ai-engine/routes`
- [Preprocessor](https://github.com/park-bit/scrawny-spend/blob/main/ai-engine/utils/preprocessor.py): feature prep · Normalizes inputs before inference · `ai-engine/utils/preprocessor.py`
- [ML models](https://github.com/park-bit/scrawny-spend/tree/main/ai-engine/models): trained assets · Classifier, predictor and anomaly detector artifacts · `ai-engine/models`

## Connections

- Pages → UI components: renders · Screens compose shared UI pieces
- React app → Pages: routes · App selects the main page views
- React app → Auth store: reads auth · Session state drives protected UI
- Pages → Client hooks: uses · Screens delegate data fetching and derived state
- Client hooks → API client: calls · Hooks fetch backend data through the client
- UI components → API client: submit data · Forms and actions use the API client
- API client → Express app: HTTP · Frontend talks only to backend endpoints
- Server entry → Express app: boots · Process startup loads the Express app
- Express app → Shared infra: middleware · Validation, auth, rate limiting and error handling
- Express app → Auth domain: mounts · Auth routes and controllers
- Express app → Expenses domain: mounts · Expense routes and controllers
- Express app → Budgets domain: mounts · Budget routes and controllers
- Express app → Analytics domain: mounts · Analytics routes and controllers
- Express app → Reports domain: mounts · Report routes and controllers
- Express app → AI bridge: mounts · AI endpoints and orchestration
- Auth domain → Domain models: uses · User lifecycle persists through Mongoose models
- Expenses domain → Domain models: uses · Expense writes and reads are model-backed
- Budgets domain → Domain models: uses · Budget state is stored in MongoDB
- Analytics domain → MongoDB: aggregates · Summaries query shared financial data
- Reports domain → MongoDB: reads · Reports are built from stored data
- AI bridge → Flask app: HTTP · Backend triggers Python inference service
- Flask app → AI routes: serves · Flask app exposes inference endpoints
- AI routes → Preprocessor: prep · Routes normalize inputs before inference
- AI routes → ML models: runs · Endpoints load trained model artifacts
- Flask app → MongoDB: reads/writes · AI uses financial context and persists results via the shared store
- AI bridge → Domain models: stores outputs · Predictions and anomalies are saved as domain records
