# quangngonz/isph-stock-exchange-express-clientapi architecture

> Architecture diagram of the GitHub repository quangngonz/isph-stock-exchange-express-clientapi, 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/quangngonz/isph-stock-exchange-express-clientapi
- Repository: https://github.com/quangngonz/isph-stock-exchange-express-clientapi

## Overview

##### Main Components
- API Server: Represent the Node.js/Express server (the entry point is server.js). This component handles incoming HTTP requests, routing them to the appropriate controllers.
- Controllers: Group these as distinct functional units (admin, teacher, user, stocks, transactions) that process business logic and interact with services.
- Routes: Map each controller to a set of defined endpoints (e.g., /admin, /teacher, /user, /stocks, /health) that detail the exposed endpoints of your API.
- Middleware: Highlight components such as authentication (authMiddleware.js) and role verification middleware (roleMiddleware.js) that secure the endpoints.
- Services: Identify auxiliary services that handle specialized tasks such as evaluating events (evaluateEventService.js), managing WebSocket connections (webSocketServer.js), and integrating with Firebase (firebaseService.js).
- Database Layer: While the project relies on Firebase for real-time data and authentication, also include the DBML-defined schema (from database.dbml) that outlines relational or structured data concepts (like Users, Stocks, Portfolios, Transactions, etc.). This illustrates that even if Firebase is main, the design is supported by a detailed database schema.
- Deployment/Configuration: Include aspects such as Vercel and Heroku configurations (Procfile, vercel.json) to indicate deployment and environment settings.

##### Relationships and Data Flow
- Controllers should show connections to Middleware, ensuring that each request is authenticated and authorized before processing.
- From Controllers, indicate how the business logic is delegated to Services for tasks like connecting to Firebase or handling real-time events.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% API Server at the top
    API["Express Server (server.js)"]:::server

    %% Core Application Logic Subgraph
    subgraph "Core Application Logic"
        direction TB
        %% Routes Subgraph
        subgraph "Routes"
            direction TB
            AdminR["Admin Routes (/admin)"]:::routes
            TeacherR["Teacher Routes (/teacher)"]:::routes
            UserR["User Routes (/user)"]:::routes
            StocksR["Stocks Routes (/stocks)"]:::routes
            Health["Health Check (/health)"]:::routes
        end

        %% Middleware Subgraph
        subgraph "Middleware"
            direction TB
            AuthMW["Auth Middleware"]:::middleware
            RoleMW["Role Verification Middleware"]:::middleware
        end

        %% Controllers Subgraph
        subgraph "Controllers"
            direction TB
            AdminC["Admin Controller"]:::controller
            AdminDash["Admin Dashboard Controller"]:::controller
            TeacherC["Teacher Controller"]:::controller
            UserC["User Controller"]:::controller
            StocksC["Stocks Controller"]:::controller
            TransC["Transaction Controller"]:::controller
        end
    end

    %% Supporting Infrastructure Subgraph
    subgraph "Supporting Infrastructure"
        direction TB
        %% Services Subgraph
        subgraph "Services"
            direction TB
            EvalService["Evaluate Event Service"]:::service
            FirebaseS["Firebase Service"]:::service
            WebSocketS["WebSocket Server"]:::service
            RefData["Auxiliary Data Reference Service"]:::service
        end
        %% Database Layer
        DBSchema["Database Schema (DBML)"]:::database
        %% Utility Scripts Subgraph
        subgraph "Utility Scripts"
            direction TB
            EnvConfig["Environment Configuration Tool"]:::utility
            StockGen["Stock Generation Script"]:::utility
            FakeData["Fake Data Push Script"]:::utility
        end
        %% Deployment/Configuration Subgraph
        subgraph "Deployment/Configuration"
            direction TB
            Heroku["Heroku Configuration"]:::deployment
            Vercel["Vercel Configuration"]:::deployment
        end
    end

    %% Connections from API Server to Core Application Logic
    API -->|"handles HTTP requests"| AdminR
    API --> TeacherR
    API --> UserR
    API --> StocksR
    API --> Health

    %% Routes to Middleware Flow
    AdminR -->|"passes through"| AuthMW
    TeacherR --> AuthMW
    UserR --> AuthMW
    StocksR --> AuthMW
    Health --> AuthMW

    %% Middleware to Controllers Flow
    AuthMW --> RoleMW
    RoleMW --> AdminC
    RoleMW --> AdminDash
    RoleMW --> TeacherC
    RoleMW --> UserC
    RoleMW --> StocksC
    RoleMW --> TransC

    %% Controllers calling Services
    AdminC -->|"calls"| FirebaseS
    AdminDash -->|"calls"| RefData
    TeacherC -->|"calls"| FirebaseS
    UserC -->|"calls"| FirebaseS
    StocksC -->|"calls"| EvalService
    TransC -->|"calls"| WebSocketS

    %% Services interconnection to Database Schema
    FirebaseS -->|"updates"| DBSchema
    EvalService --> DBSchema

    %% Utility Scripts support Deployment and environment
    EnvConfig ---|"configures environment"| API
    StockGen ---|"generates test stocks"| DBSchema
    FakeData ---|"pushes fake data"| DBSchema

    %% Deployment influence
    Heroku ---|"deploys API"| API
    Vercel ---|"deploys API"| API

    %% Click Events for API Server and Core Components
    click API "https://github.com/quangngonz/isph-stock-exchange-express-clientapi/blob/main/server.js"
    click AdminR "https://github.com/quangngonz/isph-stock-exchange-express-clientapi/blob/main/routes/admin.js"
    click TeacherR "https://github.com/quangngonz/isph-stock-exchange-express-clientapi/blob/main/routes/teacher.js"
    click UserR "https://github.com/quangngonz/isph-stock-exchange-express-clientapi/blob/main/routes/user.js"
    click StocksR "https://github.com/quangngonz/isph-stock-exchange-express-clientapi/blob/main/routes/stocks.js"
    click Health "https://github.com/quangngonz/isph-stock-exchange-express-clientapi/blob/main/routes/health.js"
    click AuthMW "https://github.com/quangngonz/isph-stock-exchange-express-clientapi/blob/main/middleware/authMiddleware.js"
    click RoleMW "https://github.com/quangngonz/isph-stock-exchange-express-clientapi/blob/main/middleware/roleMiddleware.js"
    click AdminC "https://github.com/quangngonz/isph-stock-exchange-express-clientapi/blob/main/controllers/adminController.js"
    click AdminDash "https://github.com/quangngonz/isph-stock-exchange-express-clientapi/tree/main/controllers/adminDashboardController/"
    click TeacherC "https://github.com/quangngonz/isph-stock-exchange-express-clientapi/blob/main/controllers/teacherController.js"
    click UserC "https://github.com/quangngonz/isph-stock-exchange-express-clientapi/blob/main/controllers/userController.js"
    click StocksC "https://github.com/quangngonz/isph-stock-exchange-express-clientapi/blob/main/controllers/stocksController.js"
    click TransC "https://github.com/quangngonz/isph-stock-exchange-express-clientapi/blob/main/controllers/transactionController.js"

    %% Click Events for Supporting Infrastructure Components
    click EvalService "https://github.com/quangngonz/isph-stock-exchange-express-clientapi/blob/main/services/evaluateEventService.js"
    click FirebaseS "https://github.com/quangngonz/isph-stock-exchange-express-clientapi/blob/main/services/firebaseService.js"
    click WebSocketS "https://github.com/quangngonz/isph-stock-exchange-express-clientapi/blob/main/services/webSocketServer.js"
    click RefData "https://github.com/quangngonz/isph-stock-exchange-express-clientapi/blob/main/services/getRefData.js"
    click DBSchema "https://github.com/quangngonz/isph-stock-exchange-express-clientapi/blob/main/database.dbml"
    click EnvConfig "https://github.com/quangngonz/isph-stock-exchange-express-clientapi/blob/main/utils/json_to_env.py"
    click StockGen "https://github.com/quangngonz/isph-stock-exchange-express-clientapi/blob/main/utils/generateStock.py"
    click FakeData "https://github.com/quangngonz/isph-stock-exchange-express-clientapi/blob/main/utils/pushFakeData.py"
    click Heroku "https://github.com/quangngonz/isph-stock-exchange-express-clientapi/tree/main/Procfile"
    click Vercel "https://github.com/quangngonz/isph-stock-exchange-express-clientapi/blob/main/vercel.json"

    %% Styles
    classDef server fill:#f8c,stroke:#333,stroke-width:2px;
    classDef routes fill:#bbf,stroke:#333,stroke-width:2px;
    classDef controller fill:#bfb,stroke:#333,stroke-width:2px;
    classDef middleware fill:#ff9,stroke:#333,stroke-width:2px;
    classDef service fill:#fcc,stroke:#333,stroke-width:2px;
    classDef database fill:#cfc,stroke:#333,stroke-width:2px;
    classDef utility fill:#ccf,stroke:#333,stroke-width:2px;
    classDef deployment fill:#fc9,stroke:#333,stroke-width:2px;
```
