# adarsh0727/typeflow-finance-backend architecture

> Architecture diagram of the GitHub repository adarsh0727/typeflow-finance-backend, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-07-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/adarsh0727/typeflow-finance-backend
- Repository: https://github.com/adarsh0727/typeflow-finance-backend

## Overview

##### Project Type and Purpose
- This is a Node.js/Express RESTful backend ("typeFlow-finance-backend") for managing financial data: users, categories, transactions, reports, and OCR extraction. It exposes HTTP endpoints, applies authentication, orchestrates business logic in controllers, interacts with a database, and calls out to an OCR utility.

##### File Structure Analysis
- server.js: bootstraps the Express app, applies global middleware, mounts route handlers, and starts the HTTP listener.
- routes/*.js: define URL endpoints under /category, /transaction, /report, /ocr, etc., and delegate to controllers.
- controllers/*.controller.js: implement request handling, call model/database APIs or OCR utilities, and return responses.
- models/*.model.js: define the schema and data-access layer (e.g., Mongoose models or ORM entities) for User, Category, Transaction.
- db/connect.js: configures and establishes the database connection pool.
- middleware/auth.middleware.js + errorHandler.middleware.js: cross-cutting concerns for JWT auth, permission checks, and centralized error handling.
- utils/ocrHelper.js + parser.js: encapsulate OCR invocation (e.g., Tesseract or third-party API) and parsing logic for extracting structured data from images.
- package.json: lists dependencies (express, mongoose/knex, jsonwebtoken, etc.) and scripts.

##### Key Architectural Patterns
- Layered MVC-style separation: Routes → Controllers → Models (Data Layer).
- Middleware pipeline for authentication and error handling.
- Single Node.js process as HTTP API gateway.
- Utility modules for external integration (OCR).

##### Main Components
a. External Clients (Web or Mobile App)
b. API Gateway (Express.js Server)
- Global Middleware (Auth, Error Handler)
- Router Layer (category.routes, transaction.routes, etc.)
- Controller Layer (category.controller, ocr.controller, …)
c. Business Logic / Utilities (OCR Helper, Parser)
d. Persistence Layer
- Database Connection (db/connect.js)
- Data Models (User, Category, Transaction)
e. External Service Integration
- OCR Engine (Tesseract or Cloud OCR API)

##### Relationships and Data Flows
- Within Server: Request → Auth Middleware → Route → Controller.
- Controller → Model/DB or Controller → OCR Utility → External OCR Service → Parser → Controller.
- Controller → Response → Client.
- Model methods use the db connection to perform CRUD on the database.

## Diagram (Mermaid)

```mermaid
flowchart TB
    %% External Client
    Client["External Client (Web/Mobile)"]:::extClient

    %% API Gateway and its subcomponents
    subgraph "API Gateway (Express.js Server)"
        Server["server.js\n(Express API Gateway)"]:::core
        subgraph "Global Middleware"
            Auth["auth.middleware.js\n(Authentication)"]:::core
            Error["errorHandler.middleware.js\n(Error Handling)"]:::core
        end
        subgraph "Routes Layer"
            CatRoutes["category.routes.js"]:::core
            TransRoutes["transaction.routes.js"]:::core
            RepRoutes["report.routes.js"]:::core
            OCRRoutes["ocr.routes.js"]:::core
        end
        subgraph "Controller Layer"
            CatCtrl["category.controller.js"]:::core
            TransCtrl["transaction.controller.js"]:::core
            RepCtrl["report.controller.js"]:::core
            OCRCtrl["ocr.controller.js"]:::core
        end
    end

    %% Utilities
    subgraph "Utilities"
        OCRHelper["utils/ocrHelper.js"]:::util
        Parser["utils/parser.js"]:::util
    end

    %% Persistence Layer
    subgraph "Persistence Layer"
        DBConn["db/connect.js"]:::core
        subgraph "Models"
            CatModel["models/category.model.js"]:::database
            TransModel["models/transaction.model.js"]:::database
            UserModel["models/user.model.js"]:::database
        end
    end

    %% External Systems
    ExternalOCR["OCR Engine"]:::external
    Database["Database"]:::databaseCylinder

    %% Request flow
    Client -->|"HTTP Request"| Server
    Server --> Auth --> CatRoutes
    Server --> Auth --> TransRoutes
    Server --> Auth --> RepRoutes
    Server --> Auth --> OCRRoutes
    CatRoutes -->|"route to"| CatCtrl
    TransRoutes -->|"route to"| TransCtrl
    RepRoutes -->|"route to"| RepCtrl
    OCRRoutes -->|"route to"| OCRCtrl

    %% Controller to Model/DB
    CatCtrl -->|"CRUD Operations"| CatModel
    TransCtrl -->|"CRUD Operations"| TransModel
    RepCtrl -->|"CRUD Operations"| UserModel
    CatModel -->|"uses"| DBConn -->|"connects to"| Database
    TransModel --> DBConn
    UserModel --> DBConn

    %% OCR flow
    OCRCtrl -->|"invoke"| OCRHelper -->|"call"| ExternalOCR
    ExternalOCR -->|"response"| Parser -->|"parsed data"| OCRCtrl

    %% Response flow
    CatCtrl -->|"HTTP Response"| Server
    TransCtrl -->|"HTTP Response"| Server
    RepCtrl -->|"HTTP Response"| Server
    OCRCtrl -->|"HTTP Response"| Server
    Server -->|"HTTP Response"| Client

    %% Click Events
    click Server "https://github.com/adarsh0727/typeflow-finance-backend/blob/main/server.js"
    click Auth "https://github.com/adarsh0727/typeflow-finance-backend/blob/main/middleware/auth.middleware.js"
    click Error "https://github.com/adarsh0727/typeflow-finance-backend/blob/main/middleware/errorHandler.middleware.js"
    click CatRoutes "https://github.com/adarsh0727/typeflow-finance-backend/blob/main/routes/category.routes.js"
    click TransRoutes "https://github.com/adarsh0727/typeflow-finance-backend/blob/main/routes/transaction.routes.js"
    click RepRoutes "https://github.com/adarsh0727/typeflow-finance-backend/blob/main/routes/report.routes.js"
    click OCRRoutes "https://github.com/adarsh0727/typeflow-finance-backend/blob/main/routes/ocr.routes.js"
    click CatCtrl "https://github.com/adarsh0727/typeflow-finance-backend/blob/main/controllers/category.controller.js"
    click TransCtrl "https://github.com/adarsh0727/typeflow-finance-backend/blob/main/controllers/transaction.controller.js"
    click RepCtrl "https://github.com/adarsh0727/typeflow-finance-backend/blob/main/controllers/report.controller.js"
    click OCRCtrl "https://github.com/adarsh0727/typeflow-finance-backend/blob/main/controllers/ocr.controller.js"
    click DBConn "https://github.com/adarsh0727/typeflow-finance-backend/blob/main/db/connect.js"
    click CatModel "https://github.com/adarsh0727/typeflow-finance-backend/blob/main/models/category.model.js"
    click TransModel "https://github.com/adarsh0727/typeflow-finance-backend/blob/main/models/transaction.model.js"
    click UserModel "https://github.com/adarsh0727/typeflow-finance-backend/blob/main/models/user.model.js"
    click OCRHelper "https://github.com/adarsh0727/typeflow-finance-backend/blob/main/utils/ocrHelper.js"
    click Parser "https://github.com/adarsh0727/typeflow-finance-backend/blob/main/utils/parser.js"
    click Client "https://github.com/adarsh0727/typeflow-finance-backend/blob/main/package.json"

    %% Styles
    classDef core fill:#D0E6FF,stroke:#0366d6,color:#0366d6;
    classDef util fill:#F2F2F2,stroke:#AAAAAA,color:#444444;
    classDef external fill:#FFE5B4,stroke:#FF8C00,color:#FF8C00;
    classDef database fill:#E6FFEA,stroke:#28A745,color:#28A745;
    classDef extClient fill:#FFF5F5,stroke:#D73A49,color:#D73A49;
    classDef databaseCylinder fill:#E6FFEA,stroke:#28A745,color:#28A745,stroke-width:2px;
    class Database,databaseCylinder databaseCylinder
```
