# nnminh-sam/messaging-app-backend architecture

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

- Interactive diagram: https://gitdiagram.com/nnminh-sam/messaging-app-backend
- Repository: https://github.com/nnminh-sam/messaging-app-backend

## Overview

##### Main Components
- Overall NestJS ecosystem as the core framework.
- Primary domain modules as distinct components:
- Authentication Module (auth) – handling user login, JWT strategies, guards and registration.
- User Module (user) – managing user profiles and related data.
- Conversation Module (conversation) – handling chat or conversation state with DTOs and entities.
- Message Module (message) – processing and storing messages.
- Media Module (media) – processing media uploads along with status management.
- Membership Module (membership) – covering membership creation, updates, bans, etc.
- Relationship Module (relationship) – managing user connections (friendship, blocking, etc.).
- Event Module (event) – enabling real-time communication (websocket gateway, events, and guards).
- Additional core components like:
- The main application bootstrap (main.ts) where the NestJS application starts.
- Common/shared utilities – decorators, helper functions (exception filters, pipes, interceptors, document transformers).
- Testing components (e2e tests) and containerization (Dockerfile, docker-compose.yml).

##### And Detail the Relationships
- How each module is a building block integrated through the NestJS dependency injection pattern.
- Dependency interactions between modules where applicable. For example, the “membership” module may interact with the “user” module to validate user roles.
- Usage of data transfer objects (DTOs) – indicating a clear separation between request validation and business logic.
- For the event module, show the bi-directional communication pattern (e.g., gateway handling incoming socket events and pushing messages through event service).

##### Important Architectural Patterns & Technologies
- The modular structure used in NestJS (each folder in src having its own module, controller, service, DTOs, and entities).
- Usage of authentication strategies (JWT and local) demonstrating standard guard and security patterns.
- The incorporation of websockets (via the event gateway) to support real-time communication.
- Middleware-like constructs such as pipes for validation, exception filters, and interceptors for API logging and response transformation.
- Docker support with the Dockerfile and docker-compose.yml, which may be depicted as part of the deployment/infrastructure layer.
- Helpful utilities for integrating with Mongoose, suggesting the use of MongoDB as part of the data storage strategy.

##### Essential Elements
- Each component (module) is clearly labeled (e.g., “Auth Module”, “Conversation Module”, etc.).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Entities
    CR["Client Request"]:::external
    NJ["NestJS Bootstrap (main.ts)"]:::bootstrap
    CR --> NJ
    click NJ "https://github.com/nnminh-sam/messaging-app-backend/blob/main/src/main.ts"

    %% Domain Modules
    AM["Authentication Module"]:::module
    click AM "https://github.com/nnminh-sam/messaging-app-backend/tree/main/src/auth"
    UM["User Module"]:::module
    click UM "https://github.com/nnminh-sam/messaging-app-backend/tree/main/src/user"
    CM["Conversation Module"]:::module
    click CM "https://github.com/nnminh-sam/messaging-app-backend/tree/main/src/conversation"
    MM["Message Module"]:::module
    click MM "https://github.com/nnminh-sam/messaging-app-backend/tree/main/src/message"
    MeM["Media Module"]:::module
    click MeM "https://github.com/nnminh-sam/messaging-app-backend/tree/main/src/media"
    MMb["Membership Module"]:::module
    click MMb "https://github.com/nnminh-sam/messaging-app-backend/tree/main/src/membership"
    RM["Relationship Module"]:::module
    click RM "https://github.com/nnminh-sam/messaging-app-backend/tree/main/src/relationship"
    EM["Event Module (Websockets)"]:::module
    click EM "https://github.com/nnminh-sam/messaging-app-backend/tree/main/src/event"

    %% Modules vertical flow from NestJS
    NJ --> AM
    AM --> UM
    UM --> CM
    CM --> MM
    MM --> MeM
    MeM --> MMb
    MMb --> RM
    RM --> EM

    %% Authentication Module Internal
    ACtrl["Auth Controller"]:::controller
    AServ["Auth Service"]:::service
    AM --> ACtrl
    ACtrl -->|"API_Call"| AServ

    %% User Module Internal
    UCtrl["User Controller"]:::controller
    UServ["User Service"]:::service
    UM --> UCtrl
    UCtrl -->|"API_Call"| UServ

    %% Conversation Module Internal
    CCtrl["Conversation Controller"]:::controller
    CServ["Conversation Service"]:::service
    CM --> CCtrl
    CCtrl -->|"API_Call"| CServ

    %% Message Module Internal
    MCtrl["Message Controller"]:::controller
    MServ["Message Service"]:::service
    MM --> MCtrl
    MCtrl -->|"API_Call"| MServ

    %% Media Module Internal
    MeCtrl["Media Controller"]:::controller
    MeServ["Media Service"]:::service
    MeM --> MeCtrl
    MeCtrl -->|"API_Call"| MeServ

    %% Membership Module Internal
    MbCtrl["Membership Controller"]:::controller
    MbServ["Membership Service"]:::service
    MMb --> MbCtrl
    MbCtrl -->|"API_Call"| MbServ

    %% Relationship Module Internal
    RCtrl["Relationship Controller"]:::controller
    RServ["Relationship Service"]:::service
    RM --> RCtrl
    RCtrl -->|"API_Call"| RServ

    %% Event Module Internal
    EGW["Event Gateway"]:::gateway
    EServ["Event Service"]:::service
    EM --> EGW
    EGW ---|"bi-directional"| EServ

    %% Cross-module dependency: Membership validates User roles
    MMb ---|"validates_roles"| UM

    %% Common/Shared Utilities
    Dec["Decorators"]:::utilities
    click Dec "https://github.com/nnminh-sam/messaging-app-backend/tree/main/src/decorator"
    Hel["Helpers"]:::utilities
    click Hel "https://github.com/nnminh-sam/messaging-app-backend/tree/main/src/helper"
    Int["Interceptors"]:::utilities
    click Int "https://github.com/nnminh-sam/messaging-app-backend/tree/main/src/interceptor"
    
    %% Utilities vertical linkage
    EM --> Dec
    Dec --> Hel
    Hel --> Int

    %% Testing Components
    Test["Testing Components"]:::test
    click Test "https://github.com/nnminh-sam/messaging-app-backend/tree/main/test"
    Int --> Test

    %% Deployment/Containerization
    DF["Dockerfile"]:::deployment
    click DF "https://github.com/nnminh-sam/messaging-app-backend/tree/main/Dockerfile"
    DC["docker-compose.yml"]:::deployment
    click DC "https://github.com/nnminh-sam/messaging-app-backend/blob/main/docker-compose.yml"
    Test --> DF
    DF --> DC

    %% Styling Classes
    classDef bootstrap fill:#b3cde0,stroke:#1f77b4,stroke-width:2px;
    classDef module fill:#c7e9c0,stroke:#2ca02c,stroke-width:2px;
    classDef controller fill:#fdae6b,stroke:#ff7f0e,stroke-width:2px;
    classDef service fill:#fdc086,stroke:#d62728,stroke-width:2px;
    classDef gateway fill:#c5b0d5,stroke:#9467bd,stroke-width:2px;
    classDef external fill:#e5f5e0,stroke:#31a354,stroke-width:2px;
    classDef utilities fill:#ffffcc,stroke:#ff7f0e,stroke-width:2px;
    classDef test fill:#fdd49e,stroke:#e6550d,stroke-width:2px;
    classDef deployment fill:#d9d9d9,stroke:#636363,stroke-width:2px;
```
