# sudarshanprocoder/expense-tracker architecture

> Architecture diagram of the GitHub repository sudarshanprocoder/expense-tracker, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-06). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/sudarshanprocoder/expense-tracker
- Repository: https://github.com/sudarshanprocoder/expense-tracker

## Overview

##### Project Type and Purpose
- The project is an Expense Tracker where users can add, delete, and download expense or income details.
- It serves both as a user-facing application (frontend) for managing expenses and as a service (backend) providing RESTful API endpoints.

##### Analyzing the File Structure
- Backend:
- Key directories include config (for database connections), controllers (handling business logic for authentication, dashboard, expense, and income operations), middleware (authentication and file-upload processing), models (Expense, Income, User models indicating MongoDB documents), and routes (mapping URL endpoints to controllers).
- The use of an MVC pattern is evident with segregated controllers, models, and routes.
- The backend entry point is server.js.
- Frontend:
- Projects under the “frontend/expense-tracker” directory include core files like index.html and main.jsx, along with a well-organized src directory covering components (cards, charts, dashboard features, expense & income related), pages (authentication and dashboard pages), hooks, context, layouts, and utilities (including API paths and axios instance).
- Vite is used as the build tool along with React and Tailwind CSS.

##### Key Components and Their Interactions
- User Interface (Browser/Client):
- The React frontend provides pages for login, sign-up, dashboard (expense/income views), and additional UI elements (charts, cards, alerts).
- It leverages context and hooks to maintain user authentication state and interact with the backend.
- Backend APIs:
- The Express server (server.js) listens for HTTP requests from the frontend.
- Routes are organized (authRoutes.js, dashboardRoutes.js, expenseRoutes.js, incomeRoutes.js) and call respective controllers that handle logic like adding, deleting, or downloading expense/income details.
- Middleware components serve as gatekeepers for authentication and file uploads.
- Database:
- A MongoDB database stores users, expense, and income records. The configuration file (db.js) establishes the database connection.
- Other Elements:
- Static file serving for uploaded files is facilitated via the “uploads” directory.
- Excel files (expense_details.xlsx and income_details.xlsx) might be used for data export or import functionalities.

##### Important Architectural Patterns and Technologies
- MVC Architecture in the backend.
- Component-driven architecture for the frontend.
- RESTful API design for communication between frontend and backend.
- Use of external libraries/frameworks such as React.js, Express.js, tailwindcss, and Vite.
- Security and user session management via JWT authentication, as indicated by the .env configuration parameters.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Subgraph
    subgraph "Frontend (React Application)"
        F1["React Frontend (Expense Tracker)"]:::frontend
        F2["Entry: index.html"]:::frontend
        F3["Bootstrap: main.jsx"]:::frontend
        subgraph "Pages"
            F4["Auth Pages"]:::frontend
            F5["Dashboard Pages"]:::frontend
        end
        F6["UI Components"]:::frontend
        subgraph "State Management"
            F7["Context (userContext.jsx)"]:::frontend
            F8["Hooks (useUserAuth.jsx)"]:::frontend
        end
        F9["Utils & API Helpers"]:::frontend
    end

    %% Backend Subgraph
    subgraph "Backend (Express & MVC)"
        B1["Express Server (server.js)"]:::backend
        B2["Config (db.js)"]:::backend

        subgraph "Routes"
            R1["Auth Routes (authRoutes.js)"]:::backend
            R2["Dashboard Routes (dashboardRoutes.js)"]:::backend
            R3["Expense Routes (expenseRoutes.js)"]:::backend
            R4["Income Routes (incomeRoutes.js)"]:::backend
        end

        subgraph "Controllers"
            C1["Auth Controller (authController.js)"]:::backend
            C2["Dashboard Controller (dashboardController.js)"]:::backend
            C3["Expense Controller (expenseController.js)"]:::backend
            C4["Income Controller (incomeController.js)"]:::backend
        end

        subgraph "Models"
            M1["User Model (User.js)"]:::backend
            M2["Expense Model (Expense.js)"]:::backend
            M3["Income Model (Income.js)"]:::backend
        end

        subgraph "Middleware"
            MW1["Auth Middleware (authMiddleware.js)"]:::middleware
            MW2["Upload Middleware (uploadMiddleware.js)"]:::middleware
        end

        B3["Static File Uploads (uploads)"]:::backend
        subgraph "Excel Exports/Imports"
            B4a["Expense Excel (expense_details.xlsx)"]:::backend
            B4b["Income Excel (income_details.xlsx)"]:::backend
        end
    end

    %% Database Node
    DB["MongoDB Database"]:::database

    %% Connections between Frontend and Backend
    F1 -->|"API Calls/HTTP Requests"| B1

    %% Backend internal flows
    B1 --> R1
    B1 --> R2
    B1 --> R3
    B1 --> R4

    %% Route to Controller flows (with Middleware interception)
    R1 -->|"passesThrough"| MW1
    MW1 -->|"calls"| C1

    R2 -->|"calls"| C2

    R3 -->|"passesThrough"| MW2
    MW2 -->|"calls"| C3

    R4 -->|"passesThrough"| MW2
    MW2 -->|"calls"| C4

    %% Controllers to Models
    C1 -->|"dataOperation"| M1
    C2 -->|"dataOperation"| M1
    C3 -->|"dataOperation"| M2
    C4 -->|"dataOperation"| M3

    %% Backend configuration to Database
    B2 -->|"EstablishesConnection"| DB

    %% Express Server interacting with Database
    B1 -->|"Queries/Updates"| DB

    %% Click Events for Frontend Components
    click F1 "https://github.com/sudarshanprocoder/expense-tracker/tree/main/frontend/expense-tracker"
    click F2 "https://github.com/sudarshanprocoder/expense-tracker/blob/main/frontend/expense-tracker/index.html"
    click F3 "https://github.com/sudarshanprocoder/expense-tracker/blob/main/frontend/expense-tracker/src/main.jsx"
    click F4 "https://github.com/sudarshanprocoder/expense-tracker/tree/main/frontend/expense-tracker/src/pages/Auth"
    click F5 "https://github.com/sudarshanprocoder/expense-tracker/tree/main/frontend/expense-tracker/src/pages/Dashboard"
    click F6 "https://github.com/sudarshanprocoder/expense-tracker/tree/main/frontend/expense-tracker/src/components"
    click F7 "https://github.com/sudarshanprocoder/expense-tracker/blob/main/frontend/expense-tracker/src/context/userContext.jsx"
    click F8 "https://github.com/sudarshanprocoder/expense-tracker/blob/main/frontend/expense-tracker/src/hooks/useUserAuth.jsx"
    click F9 "https://github.com/sudarshanprocoder/expense-tracker/tree/main/frontend/expense-tracker/src/utils"

    %% Click Events for Backend Components
    click B1 "https://github.com/sudarshanprocoder/expense-tracker/blob/main/backend/server.js"
    click B2 "https://github.com/sudarshanprocoder/expense-tracker/blob/main/backend/config/db.js"
    click R1 "https://github.com/sudarshanprocoder/expense-tracker/blob/main/backend/routes/authRoutes.js"
    click R2 "https://github.com/sudarshanprocoder/expense-tracker/blob/main/backend/routes/dashboardRoutes.js"
    click R3 "https://github.com/sudarshanprocoder/expense-tracker/blob/main/backend/routes/expenseRoutes.js"
    click R4 "https://github.com/sudarshanprocoder/expense-tracker/blob/main/backend/routes/incomeRoutes.js"
    click C1 "https://github.com/sudarshanprocoder/expense-tracker/blob/main/backend/controllers/authController.js"
    click C2 "https://github.com/sudarshanprocoder/expense-tracker/blob/main/backend/controllers/dashboardController.js"
    click C3 "https://github.com/sudarshanprocoder/expense-tracker/blob/main/backend/controllers/expenseController.js"
    click C4 "https://github.com/sudarshanprocoder/expense-tracker/blob/main/backend/controllers/incomeController.js"
    click M1 "https://github.com/sudarshanprocoder/expense-tracker/blob/main/backend/models/User.js"
    click M2 "https://github.com/sudarshanprocoder/expense-tracker/blob/main/backend/models/Expense.js"
    click M3 "https://github.com/sudarshanprocoder/expense-tracker/blob/main/backend/models/Income.js"
    click MW1 "https://github.com/sudarshanprocoder/expense-tracker/blob/main/backend/middleware/authMiddleware.js"
    click MW2 "https://github.com/sudarshanprocoder/expense-tracker/blob/main/backend/middleware/uploadMiddleware.js"
    click B3 "https://github.com/sudarshanprocoder/expense-tracker/tree/main/backend/uploads"
    click B4a "https://github.com/sudarshanprocoder/expense-tracker/blob/main/backend/expense_details.xlsx"
    click B4b "https://github.com/sudarshanprocoder/expense-tracker/blob/main/backend/income_details.xlsx"

    %% Styles
    classDef frontend fill:#bbf,stroke:#333,stroke-width:2px;
    classDef backend fill:#fbb,stroke:#333,stroke-width:2px;
    classDef database fill:#bfb,stroke:#333,stroke-width:2px;
    classDef middleware fill:#ff9,stroke:#333,stroke-width:2px;
```
