# edilsonlm217/whatsapp-session-manager architecture

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

- Interactive diagram: https://gitdiagram.com/edilsonlm217/whatsapp-session-manager
- Repository: https://github.com/edilsonlm217/whatsapp-session-manager

## Overview

##### Main Components
- App Module – The entry point of the application which wires together the various modules (see src/app.module.ts).
- WhatsApp Module – Contains the controller and service handling WhatsApp-related operations (see src/core/whatsapp/whatsapp.module.ts, whatsapp.controller.ts, and whatsapp.service.ts).
- WhatsApp Session Manager – A specialized sub-module handling WhatsApp session management and authentication (under src/core/whatsapp/session-manager) that includes:
- Auth State Module – Managing different authentication states with its own service and helper classes (see auth-state.module.ts, auth-state.service.ts, and the services in auth-state/services).
- Session management service – Orchestrates session-related actions using components like whatsapp-session.ts.
- Database Module – Manages data persistence and repository operations (located in src/database). It includes repositories for credentials (creds.repository.ts) and keys (keys.repository.ts).
- Testing components – Indicated by integration and e2e tests in the test folder.
- Configuration files – Files such as docker-compose.yml, tsconfig.json, nest-cli.json, and various linting/formatting configuration files support deployment, building, and coding standards.

##### Relationships and Interactions
- Flow of a Request:
- External requests go to the App Controller(s) or a dedicated WhatsApp Controller. The WhatsApp Controller (src/core/whatsapp/whatsapp.controller.ts) handles incoming API calls.
- The controller delegates business logic to the WhatsApp Service (src/core/whatsapp/whatsapp.service.ts). This service, in turn, may interact with the session manager to handle authentication and session persistence.
- The Session Manager orchestrates interactions with the Auth State components. The Auth State Service utilizes its helper services (for credentials and keys processing) contained in auth-state/services.
- Database interactions are handled by the Database Module. Specific repositories (creds.repository.ts and keys.repository.ts) are used by session manager components or other services to store or retrieve data.
- Dependency Relationships:
- Modules are decoupled using NestJS’s dependency injection and modular architecture.
- The WhatsApp module depends on both the session manager and the database modules.
- The session manager leverages the Auth State sub-module to manage authentication states.

##### Architectural Patterns and Key Technologies
- Modular Architecture – The clear separation of features into separate modules (e.g., app, whatsapp, database) follows the NestJS modular pattern.
- Service-Controller-Repository Pattern – Controllers receiving HTTP requests, services for business logic, and repositories for database interactions.
- Dependency Injection – NestJS’s DI is used extensively to wire modules and services together.
- Docker and Environment Configuration – Presence of docker-compose.yml suggests containerized deployment.
- Testing – Presence of unit and e2e test folders indicates focus on quality with proper test organization.

## Diagram (Mermaid)

```mermaid
graph TD
    %% External Request
    ExternalRequest("External Request"):::external

    %% Application Layer
    AppModule("App Module"):::module
    ExternalRequest -->|"routes"| AppModule

    %% WhatsApp Feature Subgraph
    subgraph "WhatsApp Feature"
        WhatsAppModule("WhatsApp Module"):::module
        WhatsAppController("WhatsApp Controller"):::controller
        WhatsAppService("WhatsApp Service"):::service
        AppModule -->|"imports"| WhatsAppModule
        WhatsAppModule -->|"exposes"| WhatsAppController
        WhatsAppModule -->|"provides"| WhatsAppService
    end

    %% Session Management Subgraph
    subgraph "Session Management"
        WhatsAppSessionManager("WhatsApp Session Manager"):::module
        WhatsAppService -->|"delegates"| WhatsAppSessionManager

        subgraph "Auth State"
            AuthStateModule("Auth State Module"):::module
            AuthStateService("Auth State Service"):::service
            CredsService("Creds Service"):::service
            KeysService("Keys Service"):::service
            AuthStateModule -->|"contains"| AuthStateService
            AuthStateService -->|"uses"| CredsService
            AuthStateService -->|"uses"| KeysService
        end

        WhatsAppSessionManager -->|"manages"| AuthStateModule
    end

    %% Data Persistence Subgraph
    subgraph "Data Persistence"
        DatabaseModule("Database Module"):::module
        CredsRepository("Creds Repository"):::repository
        KeysRepository("Keys Repository"):::repository
        DatabaseModule -->|"offers"| CredsRepository
        DatabaseModule -->|"offers"| KeysRepository
    end

    %% Interaction with Database from Session Management
    CredsService -->|"stores/reads"| CredsRepository
    KeysService -->|"stores/reads"| KeysRepository
    WhatsAppSessionManager -->|"integrates with"| DatabaseModule

    %% Testing Component
    TestingComponents("Testing Components"):::test
    AppModule -->|"validated by"| TestingComponents

    %% Click Events
    click AppModule "https://github.com/edilsonlm217/whatsapp-session-manager/blob/main/src/app.module.ts"
    click WhatsAppModule "https://github.com/edilsonlm217/whatsapp-session-manager/blob/main/src/core/whatsapp/whatsapp.module.ts"
    click WhatsAppController "https://github.com/edilsonlm217/whatsapp-session-manager/blob/main/src/core/whatsapp/whatsapp.controller.ts"
    click WhatsAppService "https://github.com/edilsonlm217/whatsapp-session-manager/blob/main/src/core/whatsapp/whatsapp.service.ts"
    click WhatsAppSessionManager "https://github.com/edilsonlm217/whatsapp-session-manager/tree/main/src/core/whatsapp/session-manager"
    click AuthStateModule "https://github.com/edilsonlm217/whatsapp-session-manager/blob/main/src/core/whatsapp/session-manager/auth-state/auth-state.module.ts"
    click AuthStateService "https://github.com/edilsonlm217/whatsapp-session-manager/blob/main/src/core/whatsapp/session-manager/auth-state/auth-state.service.ts"
    click CredsService "https://github.com/edilsonlm217/whatsapp-session-manager/blob/main/src/core/whatsapp/session-manager/auth-state/services/creds.service.ts"
    click KeysService "https://github.com/edilsonlm217/whatsapp-session-manager/blob/main/src/core/whatsapp/session-manager/auth-state/services/keys.service.ts"
    click DatabaseModule "https://github.com/edilsonlm217/whatsapp-session-manager/blob/main/src/database/database.module.ts"
    click CredsRepository "https://github.com/edilsonlm217/whatsapp-session-manager/blob/main/src/database/repositories/creds.repository.ts"
    click KeysRepository "https://github.com/edilsonlm217/whatsapp-session-manager/blob/main/src/database/repositories/keys.repository.ts"
    click TestingComponents "https://github.com/edilsonlm217/whatsapp-session-manager/tree/main/test"

    %% Styles
    classDef external fill:#fdd,stroke:#333,stroke-width:2px;
    classDef module fill:#bbf,stroke:#333,stroke-width:2px;
    classDef controller fill:#cfc,stroke:#333,stroke-width:2px;
    classDef service fill:#fc9,stroke:#333,stroke-width:2px;
    classDef repository fill:#ccf,stroke:#333,stroke-width:2px;
    classDef test fill:#f99,stroke:#333,stroke-width:2px;
```
