# tranhai15/voiviet architecture

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

- Interactive diagram: https://gitdiagram.com/tranhai15/voiviet
- Repository: https://github.com/tranhai15/voiviet

## Overview

##### Main Components
- Frontend Application:
- Built using React (with Vite as the build tool).
- Contains several sections such as admin pages, chat pages, forms (login, register), and several utility components (contexts, API services, and hooks).
- Organized into multiple component directories (chat, admin, form, notFound, loading) that articulate different UI/UX functions.
- Backend Server:
- Built on Node.js with Express (the app.js file is the entry point).
- Organized into clear MVC-like structure with directories for controllers (authController, dataController, fileControllers, userController), models (User, Data, Files), and routes corresponding to various functionalities (auth, data, files, ai, user).
- Contains authentication configuration (passport.js) and middleware (authenticateToken.js) for secure API access.
- Integrates with additional modules like Redis (redis.js) and socket handling (socket.js) for real-time communication.
- Database & Cache:
- Although the specific database technology isn’t explicitly named, the presence of db.js in the Backend hints at database connectivity. This component should be depicted as the system’s storage.
- Redis is referenced in the file tree, implying its use as a caching layer or for managing session data.

##### Out the Relationships and Interactions
- Frontend ⇨ Backend APIs:
- The frontend communicates with the backend via REST API calls (as indicated by the multiple API client modules and axios usage in the Frontend directory).
- Frontend ⇨ WebSocket Connection:
- There is a dedicated socket.js in the Backend, signifying that real-time chat messages are routed via WebSockets.
- Backend Internal Communication:
- The backend’s controllers interact with the models to perform database operations.
- Configuration files (passport and middlewares) enforce authentication before handling requests.
- Redis connects with the backend to support caching or session management; this should be clearly shown.
- Administrative Operations:

##### Architectural Patterns & Design Principles
- MVC-Like Structure in the Backend:
- The separation of concerns into controllers, models, and route definitions.
- Frontend Componentization:
- Break-down of the user interface into reusable components, contexts (for state management and authentication), and APIs.
- Real-time Communication:
- Dedicated socket management for handling chat, emphasizing asynchronous and bidirectional data flows.
- Security & Authentication:
- Usage of passport and custom middleware to secure API endpoints.
- Modularity and Scalability:
- Clear directory separation for components, utilities, contexts, and assets which reinforces the maintainability of the project.

##### Final Detailed Steps for the Principal Software Engineer
- Within the Backend layer, break out sub-components:
- REST API endpoints (organized by feature such as authentication, data, files, user management, and AI functions).
- An authentication/security module (showing passport setup and middleware usage).
- A dedicated WebSocket service/module to handle real-time chat.
- Within the Frontend layer, mark the key feature areas:
- Admin interfaces that manage accounts, dashboards, files, and departments.
- Login/Register forms and overall routing/contexts that manage state and authentication.
- Outbound API requests from Frontend to corresponding Backend routes.
- Real-time message traffic (websockets) between the Frontend chat components and the Backend socket server.
- Internal data flow in the backend from controllers to the database and cache.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Frontend Layer
    subgraph "Client Side (Frontend)"
        FE1["React with Vite (Frontend UI)"]
        FE2["Chat UI Components"]
        FE3["Admin Dashboard Components"]
        FE4["Login/Register Forms"]
        FE5["API Services"]
        FE6["Utilities"]
    end

    %% Backend Layer
    subgraph "Server Side (Backend)"
        BE1["Express Server (Node.js/Express)"]
        BE2["REST API Controllers (Auth,Data,Files,User)"]
        BE3["Route Definitions"]
        BE4["Authentication Module (Passport & Middleware)"]
        BE5["Real-time Socket Server"]
    end

    %% Data Layer
    subgraph "Data Layer (Database/Cache)"
        DB["Database Storage"]
        REDIS["Redis Cache"]
    end

    %% Connections from Frontend to Backend
    FE1 -->|"REST_API_calls"| BE1
    FE5 -->|"REST_API_calls"| BE1
    FE2 -->|"WebSocket"| BE5

    %% Backend internal connections
    BE1 -->|"uses"| BE2
    BE1 -->|"defines"| BE3
    BE1 -->|"authenticates_with"| BE4
    BE1 -->|"real-time"| BE5

    %% Backend to Data Layer
    BE2 -->|"DB_operations"| DB
    BE3 -->|"DB_calls"| DB
    BE2 -->|"cache_lookup"| REDIS
    BE3 -->|"cache_write"| REDIS

    %% Click Events for Frontend Components
    click FE1 "https://github.com/tranhai15/voiviet/tree/main/Frontend/"
    click FE2 "https://github.com/tranhai15/voiviet/tree/main/Frontend/src/components/chat/"
    click FE3 "https://github.com/tranhai15/voiviet/tree/main/Frontend/src/components/admin/"
    click FE4 "https://github.com/tranhai15/voiviet/tree/main/Frontend/src/components/form/"
    click FE5 "https://github.com/tranhai15/voiviet/tree/main/Frontend/src/api/"
    click FE6 "https://github.com/tranhai15/voiviet/tree/main/Frontend/src/utils/"

    %% Click Events for Backend Components
    click BE1 "https://github.com/tranhai15/voiviet/blob/main/Backend/app.js"
    click BE2 "https://github.com/tranhai15/voiviet/tree/main/Backend/controllers/"
    click BE3 "https://github.com/tranhai15/voiviet/tree/main/Backend/routes/"
    click BE4 "https://github.com/tranhai15/voiviet/blob/main/Backend/config/passport.js"
    click BE4 "https://github.com/tranhai15/voiviet/blob/main/Backend/middlewares/authenticateToken.js"
    click BE5 "https://github.com/tranhai15/voiviet/blob/main/Backend/socket.js"

    %% Click Events for Data Layer
    click DB "https://github.com/tranhai15/voiviet/blob/main/Backend/db.js"
    click REDIS "https://github.com/tranhai15/voiviet/blob/main/Backend/redis.js"

    %% Styles
    classDef frontend fill:#cce5ff,stroke:#004085,stroke-width:2px;
    classDef backend fill:#d4edda,stroke:#155724,stroke-width:2px;
    classDef datalayer fill:#f8d7da,stroke:#721c24,stroke-width:2px;
    
    class FE1,FE2,FE3,FE4,FE5,FE6 frontend;
    class BE1,BE2,BE3,BE4,BE5 backend;
    class DB,REDIS datalayer;
```
