# hdlegend08/happy-bot-backend architecture

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

- Interactive diagram: https://gitdiagram.com/hdlegend08/happy-bot-backend
- Repository: https://github.com/hdlegend08/happy-bot-backend

## Overview

##### Main Components
- Overall Application: At the heart is the NestJS application, represented by the main entry point (src/main.ts) and the app module (src/app.module.ts).
- Modules: Break down the system into its individual feature modules. In this project, notable modules include:
- Auth Module (src/modules/auth): Handles authentication strategies (local, JWT, and even socket-based strategies and guards) and permissions.
- Chat Module (src/modules/chat): Contains the chat gateway, indicating real-time or WebSocket communication, possibly with a Redis-based IO adapter for scaling.
- Chat History Module (src/modules/chat-history): Manages chat data persistence with related schema and interfaces.
- User Module (src/modules/user): Provides user management features with its own schema and service layer.

##### Detail Interactions and Responsibilities
- Each module typically includes controllers (handling HTTP/WebSocket routes), services (business logic), and, where applicable, strategies/guards (for security and authentication).
- The Auth module, for example, has multiple guards and strategies which interact with both the controller layer and possibly with other modules that require authentication.
- The Chat module involves real-time communications through a gateway, which leverages a Redis adapter to potentially connect with external messaging infrastructure.
- The Chat History module involves a controller and service, and likely communicates with a database layer through defined schemas.
- The User module similarly defines interfaces, controllers, and services that work with the underlying database.

##### Architectural Patterns and Design Principles
- Modular Architecture: Emphasized by the folder structure where each domain (auth, chat, chat-history, user) is separated, following NestJS’s recommended modular structure.
- Dependency Injection: Although not explicitly drawn in the file tree, the use of providers (services) and guards/strategies hints at dependency injection—a central design pattern in NestJS.
- Separation of Concerns: Each module houses its own controllers, services, and tests (indicated by *.spec.ts files), keeping the responsibilities cleanly separated.
- Real-time Communication: The chat module includes a gateway and a Redis adapter, indicating the use of event-driven communication patterns.

##### Relevant Technologies and Frameworks
- NestJS Framework: The entire application is built on NestJS, leveraging the framework’s capabilities for creating scalable server-side applications.
- Typescript: All source files are TypeScript, ensuring type safety and modern JavaScript features.
- Redis: Indicated by the presence of redis-io adapter, used for the chat system.
- Testing: Both unit tests and end-to-end (e2e) tests are in place, as seen in the spec files and the test directory.

- Link the Auth module to both HTTP endpoints (for RESTful calls) and WebSocket connections (for socket-based authentication in the chat module).

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "NestJS Application"
        A1["Bootstrap (Entry)"]:::app
        A2["Main Module"]:::app
    end

    A1 --> A2

    subgraph "Auth Module"
        AuthMod["Auth Module Definition"]:::module
        Local["Local Auth Strategy/Guard"]:::controller
        JWT["JWT Auth Strategy/Guard"]:::controller
        Socket["Socket Auth Strategy/Guard"]:::controller
        Perm["Permissions (Decorator & Guard)"]:::controller
        AuthServ["Auth Service"]:::service
    end

    subgraph "Chat Module"
        ChatMod["Chat Module Definition"]:::module
        ChatGW["Chat Gateway"]:::gateway
        RedisAd["Redis Adapter"]:::adapter
    end

    subgraph "Chat History Module"
        CHMod["Chat History Module Definition"]:::module
        CHCtrl["Chat History Controller"]:::controller
        CHServ["Chat History Service"]:::service
        CHSchema["Chat History Schema & Interface"]:::schema
    end

    subgraph "User Module"
        UserMod["User Module Definition"]:::module
        UCtrl["Users Controller"]:::controller
        UServ["User Service"]:::service
        USchema["User Schema & Interface"]:::schema
    end

    Database["Database"]:::database

    %% Connections from Main Module to Feature Modules
    A2 --> AuthMod
    A2 --> ChatMod
    A2 --> CHMod
    A2 --> UserMod

    %% Auth Module Internal Connections
    AuthMod --> AuthServ
    AuthServ --> Local
    AuthServ --> JWT
    AuthServ --> Socket
    AuthMod --> Perm

    %% Chat Module Internal Connections
    ChatMod --> ChatGW
    ChatGW -->|"real-time"| RedisAd

    %% Chat History Module Internal Connections
    CHMod --> CHCtrl
    CHMod --> CHServ
    CHServ --> CHSchema
    CHServ -->|"DB_CRUD"| Database

    %% User Module Internal Connections
    UserMod --> UCtrl
    UserMod --> UServ
    UServ --> USchema
    UServ -->|"DB_CRUD"| Database

    %% Cross-Module Interactions
    ChatGW -->|"auth_check"| AuthServ
    UCtrl -->|"calls_auth"| AuthServ

    %% Click Events
    click A1 "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/main.ts"
    click A2 "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/app.module.ts"
    click AuthMod "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/auth/auth.module.ts"
    click Local "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/auth/local-auth.guard.ts"
    click Local "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/auth/local.strategy.ts"
    click JWT "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/auth/jwt-auth.guard.ts"
    click JWT "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/auth/jwt.strategy.ts"
    click Socket "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/auth/socket-jwt-auth.guard.ts"
    click Socket "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/auth/socket-jwt.strategy.ts"
    click Perm "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/auth/permission.decorator.ts"
    click Perm "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/auth/permission.guard.ts"
    click AuthServ "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/auth/auth.service.ts"
    click AuthServ "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/auth/auth.service.spec.ts"
    click ChatMod "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/chat/chat.module.ts"
    click ChatGW "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/chat/chat.gateway.ts"
    click ChatGW "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/chat/chat.gateway.spec.ts"
    click RedisAd "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/chat/redis-io.adapter.ts"
    click CHMod "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/chat-history/chat-history.module.ts"
    click CHCtrl "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/chat-history/chat-history.controller.ts"
    click CHCtrl "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/chat-history/chat-history.controller.spec.ts"
    click CHServ "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/chat-history/chat-history.service.ts"
    click CHServ "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/chat-history/chat-history.service.spec.ts"
    click CHSchema "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/chat-history/chat-history.interface.ts"
    click CHSchema "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/chat-history/chat-history.schema.ts"
    click UserMod "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/user/user.module.ts"
    click UCtrl "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/user/users.controller.ts"
    click UServ "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/user/user.service.ts"
    click UServ "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/user/user.service.spec.ts"
    click USchema "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/user/user.interface.ts"
    click USchema "https://github.com/hdlegend08/happy-bot-backend/blob/master/src/modules/user/user.schema.ts"

    %% Styles
    classDef app fill:#FCE181,stroke:#333,stroke-width:2px;
    classDef module fill:#A2D2FF,stroke:#333,stroke-width:2px;
    classDef controller fill:#FFADAD,stroke:#333,stroke-width:2px;
    classDef service fill:#FFD6A5,stroke:#333,stroke-width:2px;
    classDef gateway fill:#FDFFB6,stroke:#333,stroke-width:2px;
    classDef adapter fill:#CDEAC0,stroke:#333,stroke-width:2px;
    classDef schema fill:#FFC6FF,stroke:#333,stroke-width:2px;
    classDef database fill:#d1c4e9,stroke:#333,stroke-width:2px;
```
