# tanloc10203/mail-system-api architecture

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

- Interactive diagram: https://gitdiagram.com/tanloc10203/mail-system-api
- Repository: https://github.com/tanloc10203/mail-system-api

## Overview

##### Main Components
- Inbound Port/Adapter (Controllers & API endpoints)
- Components like the user.controller.ts and endpoints for registration, login, getting email lists, sending emails, etc.
- Domain Layer (Core Business Logic)
- Domain entities defined under folders such as src/@core/domain and src/user/domain.
- Business rules, validations, and value objects (e.g., API response, user domain entities).
- Application/Service Layer
- Use cases implemented in services, such as user.service.ts and modules orchestrating business logic.
- Outbound Port/Adapter (Infrastructure)
- Persistence adapters: see the infrastructure persistence directories and document repositories (e.g., user/infrastructure/persistence/document).
- Database configuration (MongoDB via Mongoose; note database-config files).
- External integrations: Gmail API integration as a third-party service.
- Cross-Cutting Concerns
- Internationalization (i18n) folders (src/i18n) with language keys.
- Interceptors (e.g., serializer and language interceptors in src/@core/interceptor).
- Exception handling and filters (located in src/@core/exceptions and src/@core/filters).
- Utility modules and config loaders (src/configs, src/utils, and environment setup files).

##### Relationships and Interactions
- The Controllers (inbound adapters) receive HTTP requests and forward them to the respective Services.
- Services interact with domain entities from the domain layer to execute business logic.
- Outbound adapters (repositories, persistence layers) implement the ports (interfaces) defined in the domain layer. They interact with MongoDB and translate between domain entities and database schemas (via mappers).
- The configuration files and i18n setups are used across the system to manage environment variables, error messages, and localization.
- Integration with the Gmail API acts as an external adapter facilitating email-sending functions.

##### Architectural Patterns
- Layered Modularity: Clearly isolate controllers, services, and repositories.
- Use of NestJS Modules: Divide the system into modules (e.g., user module, auth module, core module) that encapsulate functionality.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Client
    A("Client Request"):::external

    %% Inbound Adapter
    subgraph "Inbound Adapter"
        B("User Controller"):::inbound
    end

    %% Application/Service Layer
    subgraph "Application Layer"
        C("User Service"):::service
    end

    %% Domain Layer
    subgraph "Domain Layer"
        D("Core Domain"):::domain
        E("User Domain Entity"):::domain
    end

    %% Outbound Adapter (Persistence)
    subgraph "Outbound Adapter (Persistence)"
        F("Document Persistence Module"):::infrastructure
        G("User Repository"):::infrastructure
        H("User Mapper"):::infrastructure
        F -->|"calls"| G
        G -->|"maps"| H
    end

    %% External Integration
    subgraph "External Integration"
        I("Gmail API Adapter"):::external
    end

    %% Cross-Cutting Concerns
    subgraph "Cross-Cutting Concerns"
        J("Interceptors"):::cross
        K("Exception Filters"):::cross
        L("Internationalization"):::cross
        M("JWT Auth"):::cross
    end

    %% Configuration/Environment Setup
    subgraph "Configuration"
        N("App Configurations"):::config
        O("Database Configurations"):::config
        P("Mongoose Config Service"):::config
    end

    %% Data Flow Connections
    A -->|"HTTP"| B
    B -->|"calls"| C
    C -->|"executes"| D
    C -->|"utilizes"| E
    C -->|"persists"| G
    C -->|"sendsEmail"| I

    %% Configuration Flows
    N --> B
    N --> C
    O --> F
    P --> F

    %% Cross-Cutting Concern Integration (dashed lines)
    J --- B
    K --- B
    L --- B
    M --- B

    %% Styles
    classDef inbound fill:#FDEBD0,stroke:#000,stroke-width:2px;
    classDef service fill:#D5F5E3,stroke:#000,stroke-width:2px;
    classDef domain fill:#AED6F1,stroke:#000,stroke-width:2px;
    classDef infrastructure fill:#F9E79F,stroke:#000,stroke-width:2px;
    classDef external fill:#FFA07A,stroke:#000,stroke-width:2px;
    classDef cross fill:#D2B4DE,stroke:#000,stroke-width:2px;
    classDef config fill:#F5CBA7,stroke:#000,stroke-width:2px;

    %% Click Events
    click B "https://github.com/tanloc10203/mail-system-api/blob/main/src/user/user.controller.ts"
    click C "https://github.com/tanloc10203/mail-system-api/blob/main/src/user/user.service.ts"
    click D "https://github.com/tanloc10203/mail-system-api/tree/main/src/@core/domain"
    click E "https://github.com/tanloc10203/mail-system-api/blob/main/src/user/domain/user.ts"
    click F "https://github.com/tanloc10203/mail-system-api/blob/main/src/user/infrastructure/persistence/document/document-persistence.module.ts"
    click G "https://github.com/tanloc10203/mail-system-api/blob/main/src/user/infrastructure/persistence/document/repositories/user.repository.ts"
    click H "https://github.com/tanloc10203/mail-system-api/blob/main/src/user/infrastructure/persistence/document/mappers/user.mapper.ts"
    click J "https://github.com/tanloc10203/mail-system-api/tree/main/src/@core/interceptor"
    click K "https://github.com/tanloc10203/mail-system-api/tree/main/src/@core/exceptions"
    click L "https://github.com/tanloc10203/mail-system-api/tree/main/src/i18n"
    click N "https://github.com/tanloc10203/mail-system-api/tree/main/src/configs"
    click O "https://github.com/tanloc10203/mail-system-api/tree/main/src/database/config"
    click P "https://github.com/tanloc10203/mail-system-api/blob/main/src/database/mongoose-config.service.ts"
```
