How park-bit/scrawny-spend works
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: app.js Express app · models Domain models · pages Pages · aiService.js AI bridge
Components 21
Frontend
React/Vite client app
- React app · Vite entry and app composition
frontend/src/App.jsx - Pages · Dashboard, auth, settings, analytics and history screens
frontend/src/pages - UI components · Charts, expense widgets, layout and common UI pieces
frontend/src/components - Client hooks · Derived data and fetch orchestration for expenses and analytics
frontend/src/hooks - API client · HTTP access to backend endpoints
frontend/src/services/api.js - Auth store · Session and auth state
frontend/src/store/authStore.js
Backend
Express API and domain services
- Server entry · Process startup and app wiring
backend/server.js - Express app · Middleware, routes, db hookup and error handling
backend/src/app.js - Auth domain · Login, registration, password reset and protection
backend/src/controllers/auth.controller.js - Expenses domain · Expense CRUD and business rules
backend/src/controllers/expense.controller.js - Budgets domain · Budget tracking over expense data
backend/src/controllers/budget.controller.js - Analytics domain · Summary metrics and chart-ready responses
backend/src/controllers/analytics.controller.js - Reports domain · Report generation over finance data
backend/src/controllers/report.controller.js - AI bridge · Triggers Python inference and persists results
backend/src/services/aiService.js - MongoDB · Shared document store via Mongoose
backend/src/config/db.js - Domain models · User, expense, budget, prediction and anomaly entities
backend/src/models - Shared infra · Validation, auth, logging, errors and aggregation pipelines
backend/src/utils
AI Engine
Flask inference service
- Flask app · Inference service entry and routing
ai-engine/app.py - AI routes · Classification, prediction and anomaly APIs
ai-engine/routes - Preprocessor · Normalizes inputs before inference
ai-engine/utils/preprocessor.py - ML models · Classifier, predictor and anomaly detector artifacts
ai-engine/models
Connections 26
- 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