# raushan-kumar7/cathago architecture

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

- Interactive diagram: https://gitdiagram.com/raushan-kumar7/cathago
- Repository: https://github.com/raushan-kumar7/cathago

## Overview

##### & Separate the Main Components
- Web Server / Express App: Represent the core Express application (src/app.js and src/index.js) that ties all components together.
- Routing Layer: Include the various route groups from the “src/routes” directory (e.g., auth.routes, document.routes, scan.routes, admin.routes, etc.), which determine how HTTP requests are handled.
- Controllers: Indicate the controllers (from src/controllers) that contain the business logic for each module (such as authentication, document management, scanning, admin functionalities, etc.).
- Services & Business Logic: Highlight the services (in src/services) that encapsulate the core operations (processing scans, handling credit requests, managing users, etc.) and orchestrate tasks between controllers and models.
- Models & Database Access: Outline the models (in src/models) that interface with the SQLite database via Sequelize, representing entities like users, documents, scans, logs, etc.
- Middleware: Include security and request-handling middleware (in src/middleware) such as authentication checks and file upload handlers.
- File & AI Processing Utilities: Represent helper utilities (src/utils) used for PDF/Word text extraction, AI comparison, error handling, and log export. Also include the file storage/uploads (src/uploads) that handle user uploaded documents.
- Templating & UI: Show the view layer (src/views) which leverages EJS layouts, partials, and pages for various UI components (admin dashboard, user dashboard, document upload forms, etc.).
- Security & External Services: Emphasize integrations like JWT (authentication), BcryptJS for password hashing, Helmet for security, and note the integration with external AI services (OpenAI & Google Generative AI) used in the scanning/matching process.
- Database: Represent SQLite (with Sequelize as ORM) as the persistent storage for user data, document metadata, credit information, and system logs.

##### Relationships & Interactions
- Flow of a Request: Start with the client UI sending a request (e.g., document upload) to the Express routes.
- The Route passes the request to the appropriate Controller.
- The Controller invokes business logic in the Services.
- The Service interacts with the Models to fetch or update data in the SQLite database.
- Middleware components (auth, upload) intercept or process requests as needed.
- Client ↔ Frontend (views served by Express) ↔ Routes → Controllers → Middleware/Services → Database.
- Credit-Based Logic: Visually separate the credit request components (accessible via dedicated routes and controllers) that connect with both user services and admin services.

##### Architectural Patterns & Design Principles
- MVC Pattern: Clearly separate Model (src/models), View (src/views), and Controller (src/controllers) layers.
- Service Layer: A dedicated service layer (src/services) promotes separation of concerns by handling domain-specific logic outside of the controllers.
- Middleware Use: Centralized middleware for requests ensures security (authentication & access control) and file processing.
- External Integrations: Represent external APIs or libraries for AI processing as separate blocks that interact with the core scanning logic.

##### Technology, Frameworks, and Libraries
- Backend: Node.js, Express.js.
- Database: SQLite with Sequelize.
- Security: JWT, Helmet, BcryptJS.
- UI: EJS-Mate templating engine and TailwindCSS.
- File Handling: Multer for file uploads and FS-Extra for file system utilities.
- Document Parsing: PDF-Parse for PDFs and Mammoth for DOCX files.
- AI Integration: OpenAI & Google Generative AI used in document scanning & matching.
- Utility Tools: Include generic utilities like AsyncHandler, ExpressError for error management.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    subgraph "Client Layer"
        client("Client UI (Browser)"):::frontend
    end

    %% Express Application Layer
    subgraph "Express Application"
        app("Express App"):::backend
        index("Index.js"):::backend
    end

    %% Middleware and Routing Layer
    subgraph "Middleware & Routes"
        middleware("Middleware (Auth,Upload)"):::backend
        routes("Routing Layer"):::backend
        subgraph "Routing Components"
            authRoutes("Auth Routes"):::backend
            adminRoutes("Admin Routes"):::backend
            documentRoutes("Document Routes"):::backend
            scanRoutes("Scan Routes"):::backend
        end
    end

    %% Controllers and Services Layer
    subgraph "Controllers & Services"
        subgraph "Controllers Components"
            authController("Auth Controller"):::backend
            adminController("Admin Controller"):::backend
            documentController("Document Controller"):::backend
            scanController("Scan Controller"):::backend
        end
        subgraph "Services Components"
            userService("User Service"):::backend
            documentService("Document Service"):::backend
            scanService("Scan Service"):::backend
            aiScanService("AI Scan Service"):::backend
            creditService("Credit Request Service"):::backend
            systemLogService("System Log Service"):::backend
        end
    end

    %% Data and Processing Layer
    subgraph "Data & Processing Layer"
        subgraph "Database Layer"
            models("Models & Database"):::database
            db("SQLite Database"):::database
        end
        subgraph "File & Utilities"
            utils("Processing Utilities"):::utils
            uploads("Uploads Storage"):::utils
        end
    end

    %% View Layer
    subgraph "View Layer"
        views("Templating & UI"):::frontend
        public("Static Assets"):::frontend
    end

    %% Security & External Services Layer
    subgraph "Security & External Services"
        security("Security Modules & Integrations"):::external
        ai("External AI Services"):::external
    end

    %% Connections (Data Flow)
    client -->|"HTTPRequest"| app
    app -->|"Initializes"| index
    app -->|"SecuredBy"| security
    app -->|"UsesMiddleware"| middleware
    middleware -->|"ForwardsTo"| routes
    routes --> authRoutes
    routes --> adminRoutes
    routes --> documentRoutes
    routes --> scanRoutes

    authRoutes -->|"Calls"| authController
    adminRoutes -->|"Calls"| adminController
    documentRoutes -->|"Calls"| documentController
    scanRoutes -->|"Calls"| scanController

    authController -->|"Invokes"| userService
    adminController -->|"Invokes"| systemLogService
    adminController -->|"ManagesCredits"| creditService
    documentController -->|"Invokes"| documentService
    scanController -->|"Invokes"| scanService

    scanService -->|"TriggersAI"| aiScanService
    aiScanService -->|"IntegratesWith"| ai

    userService -->|"DBOps"| models
    documentService -->|"DBOps"| models
    scanService -->|"DBOps"| models
    creditService -->|"DBOps"| models
    systemLogService -->|"LogsTo"| models

    models -->|"StoresData"| db

    documentService -->|"Uses"| utils
    scanService -->|"Uses"| utils

    middleware -->|"HandlesUploads"| uploads

    app -->|"ServesUI"| views
    views -->|"DeliversAssets"| public

    %% Click Events
    click app "https://github.com/raushan-kumar7/cathago/blob/main/src/app.js"
    click index "https://github.com/raushan-kumar7/cathago/blob/main/src/index.js"
    click middleware "https://github.com/raushan-kumar7/cathago/tree/main/src/middleware"
    click authRoutes "https://github.com/raushan-kumar7/cathago/blob/main/src/routes/auth.routes.js"
    click adminRoutes "https://github.com/raushan-kumar7/cathago/blob/main/src/routes/admin.routes.js"
    click documentRoutes "https://github.com/raushan-kumar7/cathago/blob/main/src/routes/document.routes.js"
    click scanRoutes "https://github.com/raushan-kumar7/cathago/blob/main/src/routes/scan.routes.js"
    click authController "https://github.com/raushan-kumar7/cathago/blob/main/src/controllers/auth.controller.js"
    click adminController "https://github.com/raushan-kumar7/cathago/blob/main/src/controllers/admin.controller.js"
    click documentController "https://github.com/raushan-kumar7/cathago/blob/main/src/controllers/document.controller.js"
    click scanController "https://github.com/raushan-kumar7/cathago/blob/main/src/controllers/scan.controller.js"
    click userService "https://github.com/raushan-kumar7/cathago/blob/main/src/services/user.service.js"
    click documentService "https://github.com/raushan-kumar7/cathago/blob/main/src/services/document.service.js"
    click scanService "https://github.com/raushan-kumar7/cathago/blob/main/src/services/scan.service.js"
    click aiScanService "https://github.com/raushan-kumar7/cathago/blob/main/src/services/ai_scan.service.js"
    click creditService "https://github.com/raushan-kumar7/cathago/blob/main/src/services/credit_request.service.js"
    click systemLogService "https://github.com/raushan-kumar7/cathago/blob/main/src/services/system_log.service.js"
    click models "https://github.com/raushan-kumar7/cathago/tree/main/src/models"
    click db "https://github.com/raushan-kumar7/cathago/blob/main/database.sqlite"
    click utils "https://github.com/raushan-kumar7/cathago/tree/main/src/utils"
    click uploads "https://github.com/raushan-kumar7/cathago/tree/main/src/uploads"
    click views "https://github.com/raushan-kumar7/cathago/tree/main/src/views"
    click public "https://github.com/raushan-kumar7/cathago/tree/main/src/public"
    click security "https://github.com/raushan-kumar7/cathago/tree/main/src/security"

    %% Styles
    classDef frontend fill:#FFDDCC,stroke:#333,stroke-width:2px;
    classDef backend fill:#CCEEFF,stroke:#333,stroke-width:2px;
    classDef database fill:#CCFFCC,stroke:#333,stroke-width:2px;
    classDef utils fill:#FFCCCC,stroke:#333,stroke-width:2px;
    classDef external fill:#FFFFCC,stroke:#333,stroke-width:2px;
```
