# laybaatariq/spendai architecture

> Architecture diagram of the GitHub repository laybaatariq/spendai, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-10-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/laybaatariq/spendai
- Repository: https://github.com/laybaatariq/spendai

## Overview

SpendAI is a PKR personal-expense tracker. Users register or log in, then manage expenses and budgets, review dashboard summaries, and request explainable insights; settings cover profile, security, and preferences. The React routes and feature screens call API service modules, while Express routes dispatch to controllers that operate on MongoDB models. The README documents trend, anomaly, recommendation, forecast, score, and budget-aware insight capabilities; sampled excerpts confirm the insight-generation service uses expense, insight, and budget models. Some feature-to-API links are documented rather than verified from service excerpts.

Main entry points: [`ai-expense-tracker-frontend/src/App.jsx`](https://github.com/laybaatariq/spendai/blob/main/ai-expense-tracker-frontend/src/App.jsx) (Application routes), [`backend/server.js`](https://github.com/laybaatariq/spendai/blob/main/backend/server.js) (Express API server), [`backend/middleware/authMiddleware.js`](https://github.com/laybaatariq/spendai/blob/main/backend/middleware/authMiddleware.js) (JWT authorization), [`ai-expense-tracker-frontend/src/components/dashboard/Dashboard.jsx`](https://github.com/laybaatariq/spendai/blob/main/ai-expense-tracker-frontend/src/components/dashboard/Dashboard.jsx) (Dashboard)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend experience"]
  node_app["Application routes<br/>[App.jsx]"]
  node_auth_ui["Sign in / register"]
  node_auth_state["Auth state<br/>[AuthContext.jsx]"]
  node_dashboard["Dashboard<br/>[Dashboard.jsx]"]
  node_expenses_ui["Expense management<br/>[Expenses.jsx]"]
  node_budget_ui["Budget management<br/>[Budget.jsx]"]
  node_insights_ui["AI insights<br/>[AIInsights.jsx]"]
  node_settings_ui["Settings<br/>[Settings.jsx]"]
end

subgraph group_frontend_data["Frontend API access"]
  node_expense_api["Expense API client<br/>[expenseService.js]"]
  node_budget_api["Budget API client<br/>[budgetService.js]"]
  node_insight_api["Insight API client<br/>[insightService.js]"]
  node_settings_api["Settings API client<br/>[settingsService.js]"]
end

subgraph group_api["Backend API"]
  node_server["Express API server<br/>[server.js]"]
  node_auth_routes["Authentication routes<br/>[authRoutes.js]"]
  node_expense_routes["Expense routes<br/>[expenseRoutes.js]"]
  node_budget_routes["Budget routes<br/>[budgetRoutes.js]"]
  node_insight_routes["Insight routes<br/>[insightRoutes.js]"]
  node_settings_routes["Settings routes<br/>[settingsRoutes.js]"]
  node_auth_middleware["JWT authorization<br/>[authMiddleware.js]"]
end

subgraph group_domain["Domain services"]
  node_auth_controller["Authentication<br/>[authController.js]"]
  node_expense_controller["Expense operations"]
  node_budget_controller["Budget calculations"]
  node_insight_controller["Insight operations"]
  node_insight_engine["Insight generation"]
  node_settings_controller["Profile and preferences"]
end

subgraph group_storage["Persistence"]
  node_user_model[("User records<br/>[User.js]")]
  node_expense_model[("Expense records<br/>[Expense.js]")]
  node_budget_model[("Budget records<br/>[Budget.js]")]
  node_insight_model[("Insight records<br/>[Insight.js]")]
end

node_person(("User"))
node_mongo[("MongoDB")]

node_person -->|"uses"| node_app
node_app -->|"routes to"| node_auth_ui
node_app -->|"routes to"| node_dashboard
node_app -->|"routes to"| node_expenses_ui
node_app -->|"routes to"| node_budget_ui
node_app -->|"routes to"| node_insights_ui
node_app -->|"routes to"| node_settings_ui
node_auth_ui -->|"authenticates via"| node_auth_state
node_dashboard -->|"requests expenses"| node_expense_api
node_dashboard -->|"requests budgets"| node_budget_api
node_dashboard -->|"requests insights"| node_insight_api
node_expenses_ui -->|"manages expenses"| node_expense_api
node_budget_ui -->|"manages budgets"| node_budget_api
node_insights_ui -->|"loads insights"| node_insight_api
node_settings_ui -->|"updates settings"| node_settings_api
node_auth_state -->|"calls auth API"| node_auth_routes
node_expense_api -->|"calls expense API"| node_expense_routes
node_insight_api -->|"calls insight API"| node_insight_routes
node_budget_api -->|"calls budget API"| node_budget_routes
node_settings_api -->|"calls settings API"| node_settings_routes
node_server -->|"mounts"| node_auth_routes
node_server -->|"mounts"| node_expense_routes
node_server -->|"mounts"| node_budget_routes
node_server -->|"mounts"| node_insight_routes
node_server -->|"mounts"| node_settings_routes
node_auth_routes -->|"dispatches"| node_auth_controller
node_expense_routes -->|"dispatches"| node_expense_controller
node_budget_routes -->|"dispatches"| node_budget_controller
node_insight_routes -->|"dispatches"| node_insight_controller
node_settings_routes -->|"dispatches"| node_settings_controller
node_expense_routes -->|"requires auth"| node_auth_middleware
node_budget_routes -->|"requires auth"| node_auth_middleware
node_insight_routes -->|"requires auth"| node_auth_middleware
node_settings_routes -->|"requires auth"| node_auth_middleware
node_auth_controller -->|"reads / writes"| node_user_model
node_auth_middleware -->|"loads user"| node_user_model
node_expense_controller -->|"reads / writes"| node_expense_model
node_budget_controller -->|"reads / writes"| node_budget_model
node_budget_controller -->|"reads spending"| node_expense_model
node_insight_controller -->|"generates insights"| node_insight_engine
node_insight_controller -->|"reads / writes"| node_insight_model
node_insight_engine -->|"analyzes history"| node_expense_model
node_insight_engine -->|"checks budgets"| node_budget_model
node_insight_engine -->|"stores results"| node_insight_model
node_settings_controller -->|"updates profile"| node_user_model
node_server -->|"connects"| node_mongo

click node_app "https://github.com/laybaatariq/spendai/blob/main/ai-expense-tracker-frontend/src/App.jsx"
click node_auth_ui "https://github.com/laybaatariq/spendai/tree/main/ai-expense-tracker-frontend/src/components/auth"
click node_auth_state "https://github.com/laybaatariq/spendai/blob/main/ai-expense-tracker-frontend/src/context/AuthContext.jsx"
click node_dashboard "https://github.com/laybaatariq/spendai/blob/main/ai-expense-tracker-frontend/src/components/dashboard/Dashboard.jsx"
click node_expenses_ui "https://github.com/laybaatariq/spendai/blob/main/ai-expense-tracker-frontend/src/components/expenses/Expenses.jsx"
click node_budget_ui "https://github.com/laybaatariq/spendai/blob/main/ai-expense-tracker-frontend/src/components/budget/Budget.jsx"
click node_insights_ui "https://github.com/laybaatariq/spendai/blob/main/ai-expense-tracker-frontend/src/components/ai-insights/AIInsights.jsx"
click node_settings_ui "https://github.com/laybaatariq/spendai/blob/main/ai-expense-tracker-frontend/src/components/settings/Settings.jsx"
click node_expense_api "https://github.com/laybaatariq/spendai/blob/main/ai-expense-tracker-frontend/src/services/expenseService.js"
click node_budget_api "https://github.com/laybaatariq/spendai/blob/main/ai-expense-tracker-frontend/src/services/budgetService.js"
click node_insight_api "https://github.com/laybaatariq/spendai/blob/main/ai-expense-tracker-frontend/src/services/insightService.js"
click node_settings_api "https://github.com/laybaatariq/spendai/blob/main/ai-expense-tracker-frontend/src/services/settingsService.js"
click node_server "https://github.com/laybaatariq/spendai/blob/main/backend/server.js"
click node_auth_routes "https://github.com/laybaatariq/spendai/blob/main/backend/routes/authRoutes.js"
click node_expense_routes "https://github.com/laybaatariq/spendai/blob/main/backend/routes/expenseRoutes.js"
click node_budget_routes "https://github.com/laybaatariq/spendai/blob/main/backend/routes/budgetRoutes.js"
click node_insight_routes "https://github.com/laybaatariq/spendai/blob/main/backend/routes/insightRoutes.js"
click node_settings_routes "https://github.com/laybaatariq/spendai/blob/main/backend/routes/settingsRoutes.js"
click node_auth_controller "https://github.com/laybaatariq/spendai/blob/main/backend/controllers/authController.js"
click node_expense_controller "https://github.com/laybaatariq/spendai/blob/main/backend/controllers/expenseController.js"
click node_budget_controller "https://github.com/laybaatariq/spendai/blob/main/backend/controllers/budgetController.js"
click node_insight_controller "https://github.com/laybaatariq/spendai/blob/main/backend/controllers/insightController.js"
click node_insight_engine "https://github.com/laybaatariq/spendai/blob/main/backend/services/aiInsightService.js"
click node_settings_controller "https://github.com/laybaatariq/spendai/blob/main/backend/controllers/settingsController.js"
click node_auth_middleware "https://github.com/laybaatariq/spendai/blob/main/backend/middleware/authMiddleware.js"
click node_user_model "https://github.com/laybaatariq/spendai/blob/main/backend/models/User.js"
click node_expense_model "https://github.com/laybaatariq/spendai/blob/main/backend/models/Expense.js"
click node_budget_model "https://github.com/laybaatariq/spendai/blob/main/backend/models/Budget.js"
click node_insight_model "https://github.com/laybaatariq/spendai/blob/main/backend/models/Insight.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_app,node_auth_ui,node_auth_state,node_dashboard,node_expenses_ui,node_budget_ui,node_insights_ui,node_settings_ui,node_person toneBlue
class node_expense_api,node_budget_api,node_insight_api,node_settings_api,node_mongo toneAmber
class node_server,node_auth_routes,node_expense_routes,node_budget_routes,node_insight_routes,node_settings_routes,node_auth_middleware toneMint
class node_auth_controller,node_expense_controller,node_budget_controller,node_insight_controller,node_insight_engine,node_settings_controller toneRose
class node_user_model,node_expense_model,node_budget_model,node_insight_model toneIndigo
```

## Components

### Frontend experience

- [Application routes](https://github.com/laybaatariq/spendai/blob/main/ai-expense-tracker-frontend/src/App.jsx): `ai-expense-tracker-frontend/src/App.jsx`
- [Sign in / register](https://github.com/laybaatariq/spendai/tree/main/ai-expense-tracker-frontend/src/components/auth): `ai-expense-tracker-frontend/src/components/auth`
- [Auth state](https://github.com/laybaatariq/spendai/blob/main/ai-expense-tracker-frontend/src/context/AuthContext.jsx): `ai-expense-tracker-frontend/src/context/AuthContext.jsx`
- [Dashboard](https://github.com/laybaatariq/spendai/blob/main/ai-expense-tracker-frontend/src/components/dashboard/Dashboard.jsx): `ai-expense-tracker-frontend/src/components/dashboard/Dashboard.jsx`
- [Expense management](https://github.com/laybaatariq/spendai/blob/main/ai-expense-tracker-frontend/src/components/expenses/Expenses.jsx): `ai-expense-tracker-frontend/src/components/expenses/Expenses.jsx`
- [Budget management](https://github.com/laybaatariq/spendai/blob/main/ai-expense-tracker-frontend/src/components/budget/Budget.jsx): `ai-expense-tracker-frontend/src/components/budget/Budget.jsx`
- [AI insights](https://github.com/laybaatariq/spendai/blob/main/ai-expense-tracker-frontend/src/components/ai-insights/AIInsights.jsx): `ai-expense-tracker-frontend/src/components/ai-insights/AIInsights.jsx`
- [Settings](https://github.com/laybaatariq/spendai/blob/main/ai-expense-tracker-frontend/src/components/settings/Settings.jsx): `ai-expense-tracker-frontend/src/components/settings/Settings.jsx`

### Frontend API access

- [Expense API client](https://github.com/laybaatariq/spendai/blob/main/ai-expense-tracker-frontend/src/services/expenseService.js): `ai-expense-tracker-frontend/src/services/expenseService.js`
- [Budget API client](https://github.com/laybaatariq/spendai/blob/main/ai-expense-tracker-frontend/src/services/budgetService.js): `ai-expense-tracker-frontend/src/services/budgetService.js`
- [Insight API client](https://github.com/laybaatariq/spendai/blob/main/ai-expense-tracker-frontend/src/services/insightService.js): `ai-expense-tracker-frontend/src/services/insightService.js`
- [Settings API client](https://github.com/laybaatariq/spendai/blob/main/ai-expense-tracker-frontend/src/services/settingsService.js): `ai-expense-tracker-frontend/src/services/settingsService.js`

### Backend API

- [Express API server](https://github.com/laybaatariq/spendai/blob/main/backend/server.js): `backend/server.js`
- [Authentication routes](https://github.com/laybaatariq/spendai/blob/main/backend/routes/authRoutes.js): `backend/routes/authRoutes.js`
- [Expense routes](https://github.com/laybaatariq/spendai/blob/main/backend/routes/expenseRoutes.js): `backend/routes/expenseRoutes.js`
- [Budget routes](https://github.com/laybaatariq/spendai/blob/main/backend/routes/budgetRoutes.js): `backend/routes/budgetRoutes.js`
- [Insight routes](https://github.com/laybaatariq/spendai/blob/main/backend/routes/insightRoutes.js): `backend/routes/insightRoutes.js`
- [Settings routes](https://github.com/laybaatariq/spendai/blob/main/backend/routes/settingsRoutes.js): `backend/routes/settingsRoutes.js`
- [JWT authorization](https://github.com/laybaatariq/spendai/blob/main/backend/middleware/authMiddleware.js): `backend/middleware/authMiddleware.js`

### Domain services

- [Authentication](https://github.com/laybaatariq/spendai/blob/main/backend/controllers/authController.js): `backend/controllers/authController.js`
- [Expense operations](https://github.com/laybaatariq/spendai/blob/main/backend/controllers/expenseController.js): `backend/controllers/expenseController.js`
- [Budget calculations](https://github.com/laybaatariq/spendai/blob/main/backend/controllers/budgetController.js): `backend/controllers/budgetController.js`
- [Insight operations](https://github.com/laybaatariq/spendai/blob/main/backend/controllers/insightController.js): `backend/controllers/insightController.js`
- [Insight generation](https://github.com/laybaatariq/spendai/blob/main/backend/services/aiInsightService.js): `backend/services/aiInsightService.js`
- [Profile and preferences](https://github.com/laybaatariq/spendai/blob/main/backend/controllers/settingsController.js): `backend/controllers/settingsController.js`

### Persistence

- [User records](https://github.com/laybaatariq/spendai/blob/main/backend/models/User.js): `backend/models/User.js`
- [Expense records](https://github.com/laybaatariq/spendai/blob/main/backend/models/Expense.js): `backend/models/Expense.js`
- [Budget records](https://github.com/laybaatariq/spendai/blob/main/backend/models/Budget.js): `backend/models/Budget.js`
- [Insight records](https://github.com/laybaatariq/spendai/blob/main/backend/models/Insight.js): `backend/models/Insight.js`

### Other components

- **User**
- **MongoDB**

## Connections

- User → Application routes: uses (evidence: `ai-expense-tracker-frontend/src/App.jsx`)
- Application routes → Sign in / register: routes to (evidence: `ai-expense-tracker-frontend/src/App.jsx`)
- Application routes → Dashboard: routes to (evidence: `ai-expense-tracker-frontend/src/App.jsx`)
- Application routes → Expense management: routes to (evidence: `ai-expense-tracker-frontend/src/App.jsx`)
- Application routes → Budget management: routes to (evidence: `ai-expense-tracker-frontend/src/App.jsx`)
- Application routes → AI insights: routes to (evidence: `ai-expense-tracker-frontend/src/App.jsx`)
- Application routes → Settings: routes to (evidence: `ai-expense-tracker-frontend/src/App.jsx`)
- Sign in / register → Auth state: authenticates via (evidence: `ai-expense-tracker-frontend/src/components/auth/Login.jsx`)
- Dashboard → Expense API client: requests expenses (evidence: `ai-expense-tracker-frontend/src/components/dashboard/Dashboard.jsx`)
- Dashboard → Budget API client: requests budgets (evidence: `ai-expense-tracker-frontend/src/components/dashboard/Dashboard.jsx`)
- Dashboard → Insight API client: requests insights (evidence: `ai-expense-tracker-frontend/src/components/dashboard/Dashboard.jsx`)
- Expense management → Expense API client: manages expenses (evidence: `ai-expense-tracker-frontend/src/components/expenses/Expenses.jsx`)
- Budget management → Budget API client: manages budgets (evidence: `ai-expense-tracker-frontend/src/components/budget/Budget.jsx`)
- AI insights → Insight API client: loads insights (evidence: `ai-expense-tracker-frontend/src/components/ai-insights/AIInsights.jsx`)
- Settings → Settings API client: updates settings (evidence: `ai-expense-tracker-frontend/src/components/settings/Settings.jsx`)
- Auth state → Authentication routes: calls auth API (evidence: `ai-expense-tracker-frontend/src/context/AuthContext.jsx`)
- Expense API client → Expense routes: calls expense API (evidence: `ai-expense-tracker-frontend/src/services/expenseService.js`)
- Insight API client → Insight routes: calls insight API (evidence: `ai-expense-tracker-frontend/src/services/insightService.js`)
- Budget API client → Budget routes: calls budget API (evidence: `README.md`)
- Settings API client → Settings routes: calls settings API (evidence: `README.md`)
- Express API server → Authentication routes: mounts (evidence: `backend/server.js`)
- Express API server → Expense routes: mounts (evidence: `backend/server.js`)
- Express API server → Budget routes: mounts (evidence: `backend/server.js`)
- Express API server → Insight routes: mounts (evidence: `backend/server.js`)
- Express API server → Settings routes: mounts (evidence: `backend/server.js`)
- Authentication routes → Authentication: dispatches (evidence: `backend/routes/authRoutes.js`)
- Expense routes → Expense operations: dispatches (evidence: `backend/routes/expenseRoutes.js`)
- Budget routes → Budget calculations: dispatches (evidence: `backend/routes/budgetRoutes.js`)
- Insight routes → Insight operations: dispatches (evidence: `backend/routes/insightRoutes.js`)
- Settings routes → Profile and preferences: dispatches (evidence: `backend/routes/settingsRoutes.js`)
- Expense routes → JWT authorization: requires auth (evidence: `backend/routes/expenseRoutes.js`)
- Budget routes → JWT authorization: requires auth (evidence: `backend/routes/budgetRoutes.js`)
- Insight routes → JWT authorization: requires auth (evidence: `backend/routes/insightRoutes.js`)
- Settings routes → JWT authorization: requires auth (evidence: `backend/routes/settingsRoutes.js`)
- Authentication → User records: reads / writes (evidence: `backend/controllers/authController.js`)
- JWT authorization → User records: loads user (evidence: `backend/middleware/authMiddleware.js`)
- Expense operations → Expense records: reads / writes (evidence: `backend/controllers/expenseController.js`)
- Budget calculations → Budget records: reads / writes (evidence: `backend/controllers/budgetController.js`)
- Budget calculations → Expense records: reads spending (evidence: `backend/controllers/budgetController.js`)
- Insight operations → Insight generation: generates insights (evidence: `backend/controllers/insightController.js`)
- Insight operations → Insight records: reads / writes (evidence: `backend/controllers/insightController.js`)
- Insight generation → Expense records: analyzes history (evidence: `backend/services/aiInsightService.js`)
- Insight generation → Budget records: checks budgets (evidence: `backend/services/aiInsightService.js`)
- Insight generation → Insight records: stores results (evidence: `backend/services/aiInsightService.js`)
- Profile and preferences → User records: updates profile (evidence: `backend/controllers/settingsController.js`)
- Express API server → MongoDB: connects (evidence: `backend/server.js`)
