# belalyasin/expense_management_api architecture

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

- Interactive diagram: https://gitdiagram.com/belalyasin/expense_management_api
- Repository: https://github.com/belalyasin/expense_management_api

## Overview

1. **What this repository is**
   - This is a Node.js backend API for personal finance tracking.
   - The core domain is **users, expenses, and categories**, with extra endpoints for **income/expense statistics** and monthly summaries.

2. **Runtime and startup path**
   - The app is structured as an **Express-style API server**.
   - `index.js` is likely the process entrypoint that boots the server.
   - `app.js` is the application wiring layer where middleware, routes, and shared configuration are assembled.

3. **Main architectural pattern**
   - The codebase follows a clean **route → controller → model** flow.
   - `Routes/` holds HTTP endpoint definitions.
   - `Controllers/` contains request handling and business logic.
   - `Models/` defines the MongoDB-backed data entities.

4. **Persistence layer**
   - The database integration is centralized in `Config/dbConnection.js`.
   - The README indicates a **MongoDB** backend via `MONGO_URI`, so the models are almost certainly **Mongoose models**.
   - This means the API is a typical document-oriented design rather than a relational one.

5. **Domain models**
   - `Models/User.js` represents authenticated users and likely stores credentials plus income-related fields.
   - `Models/Expenses.js` represents expense records and probably includes amount, date, category, and user ownership.
   - `Models/Category.js` represents expense categories for organizing and filtering spend.

6. **Authentication and request protection**
   - `Middleware/auth.js` is the security boundary for protected endpoints.
   - The README mentions register/login/logout and `JWT_KEY`, so authentication is JWT-based.
   - Protected routes likely require a valid token before accessing expense/category operations or user-specific statistics.

7. **User subsystem**
   - `Controllers/userController.js` and `Routes/userRoute.js` implement user lifecycle endpoints.
   - This subsystem handles authentication and account-related actions.
   - It is the gateway to the rest of the API because the finance data is user-scoped.

8. **Expense subsystem**
   - `Controllers/expenseController.js` and `Routes/expenseRoute.js` are the main business area.
   - They likely support create, update, list, and summary queries for expenses.
   - The README’s monthly statistics and “expense type statistics” suggest this controller also computes aggregations over time and category/type dimensions.

9. **Category subsystem**
   - `Controllers/categoryController.js` and `Routes/categoryRoute.js` manage category CRUD or retrieval.
   - Categories are a supporting domain object used to classify expenses and drive reporting.
   - This subsystem probably exists both for normalization and for filtering/analytics.

10. **Data flow through the API**
   - A request enters a route in `Routes/`.
   - If protected, `Middleware/auth.js` validates the JWT and attaches the user context.
   - The controller performs validation and business logic, then reads/writes through a model.
   - Results come back from MongoDB and are returned as JSON responses.

11. **Configuration and environment**
   - The runtime is configured through environment variables: `PORT`, `MONGO_URI`, and `JWT_KEY`.
   - That means the app is designed to be deployed as a single backend service with external database connectivity and token signing secret management.
   - No frontend, job runner, or queue system is visible in this repository.

12. **How to draw the architecture**
   - At the top, show clients calling an **HTTP API**.
   - In the middle, show **routes**, **controllers**, and **JWT auth middleware** as the application layer.
   - At the bottom, show **MongoDB** with three main collections/entities: **users**, **expenses**, and **categories**.
   - Highlight the expense controller as the place where most reporting/aggregation logic lives.

Main entry points: [`app.js`](https://github.com/belalyasin/expense_management_api/blob/master/app.js) (app.js), [`Controllers/expenseController.js`](https://github.com/belalyasin/expense_management_api/blob/master/Controllers/expenseController.js) (Expense API), [`Routes/userRoute.js`](https://github.com/belalyasin/expense_management_api/blob/master/Routes/userRoute.js) (User routes), [`Routes/expenseRoute.js`](https://github.com/belalyasin/expense_management_api/blob/master/Routes/expenseRoute.js) (Expense routes)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_index_js["index.js<br/>entrypoint<br/>[index.js]"]
  node_app_js["app.js<br/>app wiring<br/>[app.js]"]
end

subgraph group_api["API Layer"]
  node_auth_middleware{{"Auth guard<br/>jwt middleware<br/>[auth.js]"}}
  node_user_routes["User routes<br/>http routes<br/>[userRoute.js]"]
  node_expense_routes["Expense routes<br/>http routes<br/>[expenseRoute.js]"]
  node_category_routes["Category routes<br/>http routes<br/>[categoryRoute.js]"]
  node_user_controller["User API<br/>controller<br/>[userController.js]"]
  node_expense_controller["Expense API<br/>controller"]
  node_category_controller["Category API<br/>controller"]
end

subgraph group_domain["Domain"]
  node_users(("Users<br/>domain"))
  node_spending(("Spending<br/>domain"))
end

subgraph group_data["Persistence"]
  node_db_connection[("DB connect<br/>mongodb config<br/>[dbConnection.js]")]
  node_user_model[("User<br/>mongoose model<br/>[User.js]")]
  node_expense_model[("Expenses<br/>mongoose model<br/>[Expenses.js]")]
  node_category_model[("Category<br/>mongoose model<br/>[Category.js]")]
end

node_index_js -->|"bootstraps"| node_app_js
node_app_js -->|"connects"| node_db_connection
node_app_js -->|"mounts"| node_user_routes
node_app_js -->|"mounts"| node_expense_routes
node_app_js -->|"mounts"| node_category_routes
node_user_routes -.->|"protects"| node_auth_middleware
node_expense_routes -->|"protects"| node_auth_middleware
node_category_routes -->|"protects"| node_auth_middleware
node_user_routes -->|"delegates"| node_user_controller
node_expense_routes -->|"delegates"| node_expense_controller
node_category_routes -->|"delegates"| node_category_controller
node_user_controller -->|"reads/writes"| node_user_model
node_expense_controller -->|"reads/writes"| node_expense_model
node_category_controller -->|"reads/writes"| node_category_model
node_expense_controller -.->|"joins"| node_category_model
node_expense_controller -.->|"scopes by user"| node_user_model
node_user_controller -.->|"manages"| node_users
node_expense_controller -.->|"manages"| node_spending

click node_index_js "https://github.com/belalyasin/expense_management_api/blob/master/index.js"
click node_app_js "https://github.com/belalyasin/expense_management_api/blob/master/app.js"
click node_db_connection "https://github.com/belalyasin/expense_management_api/blob/master/Config/dbConnection.js"
click node_auth_middleware "https://github.com/belalyasin/expense_management_api/blob/master/Middleware/auth.js"
click node_user_routes "https://github.com/belalyasin/expense_management_api/blob/master/Routes/userRoute.js"
click node_expense_routes "https://github.com/belalyasin/expense_management_api/blob/master/Routes/expenseRoute.js"
click node_category_routes "https://github.com/belalyasin/expense_management_api/blob/master/Routes/categoryRoute.js"
click node_user_controller "https://github.com/belalyasin/expense_management_api/blob/master/Controllers/userController.js"
click node_expense_controller "https://github.com/belalyasin/expense_management_api/blob/master/Controllers/expenseController.js"
click node_category_controller "https://github.com/belalyasin/expense_management_api/blob/master/Controllers/categoryController.js"
click node_user_model "https://github.com/belalyasin/expense_management_api/blob/master/Models/User.js"
click node_expense_model "https://github.com/belalyasin/expense_management_api/blob/master/Models/Expenses.js"
click node_category_model "https://github.com/belalyasin/expense_management_api/blob/master/Models/Category.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_index_js,node_app_js toneBlue
class node_auth_middleware,node_user_routes,node_expense_routes,node_category_routes,node_user_controller,node_expense_controller,node_category_controller toneAmber
class node_users,node_spending toneMint
class node_db_connection,node_user_model,node_expense_model,node_category_model toneRose
```

## Components

### Runtime

Process entry and app bootstrap

- [index.js](https://github.com/belalyasin/expense_management_api/blob/master/index.js): entrypoint · Starts the server process · `index.js`
- [app.js](https://github.com/belalyasin/expense_management_api/blob/master/app.js): app wiring · Assembles middleware, routes, and shared setup · `app.js`

### API Layer

HTTP routing, controllers, and auth middleware

- [Auth guard](https://github.com/belalyasin/expense_management_api/blob/master/Middleware/auth.js): jwt middleware · Validates protected requests and user context · `Middleware/auth.js`
- [User routes](https://github.com/belalyasin/expense_management_api/blob/master/Routes/userRoute.js): http routes · Authentication and account endpoints · `Routes/userRoute.js`
- [Expense routes](https://github.com/belalyasin/expense_management_api/blob/master/Routes/expenseRoute.js): http routes · Expense and reporting endpoints · `Routes/expenseRoute.js`
- [Category routes](https://github.com/belalyasin/expense_management_api/blob/master/Routes/categoryRoute.js): http routes · Category management endpoints · `Routes/categoryRoute.js`
- [User API](https://github.com/belalyasin/expense_management_api/blob/master/Controllers/userController.js): controller · Handles registration, login, and account actions · `Controllers/userController.js`
- [Expense API](https://github.com/belalyasin/expense_management_api/blob/master/Controllers/expenseController.js): controller · Handles expense CRUD and aggregations · `Controllers/expenseController.js`
- [Category API](https://github.com/belalyasin/expense_management_api/blob/master/Controllers/categoryController.js): controller · Handles category CRUD and lookup · `Controllers/categoryController.js`

### Domain

Core finance entities

- **Users**: domain · Account and identity boundary
- **Spending**: domain · Expense tracking and reporting boundary

### Persistence

Database connection and MongoDB models

- [DB connect](https://github.com/belalyasin/expense_management_api/blob/master/Config/dbConnection.js): mongodb config · Initializes MongoDB connectivity · `Config/dbConnection.js`
- [User](https://github.com/belalyasin/expense_management_api/blob/master/Models/User.js): mongoose model · Authenticated user records and credentials · `Models/User.js`
- [Expenses](https://github.com/belalyasin/expense_management_api/blob/master/Models/Expenses.js): mongoose model · User-scoped expense records · `Models/Expenses.js`
- [Category](https://github.com/belalyasin/expense_management_api/blob/master/Models/Category.js): mongoose model · Expense classification records · `Models/Category.js`

## Connections

- index.js → app.js: bootstraps · Process entry loads the application layer
- app.js → DB connect: connects · Application initialization opens the database connection
- app.js → User routes: mounts · Registers user endpoints on the server
- app.js → Expense routes: mounts · Registers expense endpoints on the server
- app.js → Category routes: mounts · Registers category endpoints on the server
- User routes → Auth guard: protects · Authentication endpoints may use JWT protection where needed
- Expense routes → Auth guard: protects · Expense access is guarded by JWT auth
- Category routes → Auth guard: protects · Category access is guarded by JWT auth
- User routes → User API: delegates · Routes forward to user controller logic
- Expense routes → Expense API: delegates · Routes forward to expense controller logic
- Category routes → Category API: delegates · Routes forward to category controller logic
- User API → User: reads/writes · User actions persist to the user model
- Expense API → Expenses: reads/writes · Expense actions persist to the expense model
- Category API → Category: reads/writes · Category actions persist to the category model
- Expense API → Category: joins · Reporting and filtering rely on categories
- Expense API → User: scopes by user · Expense queries are user-specific
- User API → Users: manages · User controller defines the account boundary
- Expense API → Spending: manages · Expense controller defines tracking and stats
