# nguyenchithannh/trello-apis architecture

> Architecture diagram of the GitHub repository nguyenchithannh/trello-apis, 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/nguyenchithannh/trello-apis
- Repository: https://github.com/nguyenchithannh/trello-apis

## Overview

The project is a RESTful API backend for a Trello-like project management application. It is built with Node.js, Express, and MongoDB, incorporating real-time features through Socket.IO, comprehensive authentication (including JWT and Google OAuth), and integration with external services like Cloudinary, Twilio, and Nodemailer.

##### Main Components
- API Layer: Representing the HTTP RESTful endpoints organized into versioned routes (v1 and v2) for different functionalities (authentication, boards, columns, cards, invites, OTP, users, and messages).
- Controller Layer: Each route forwards requests to its respective controller (e.g., authController, boardController, cardController). These serve as the interface between the routes and the core business logic.
- Service Layer: Controllers call services (e.g., authService, boardService, cardService) which contain business logic and coordinate with models.
- Data Models (Persistence Layer): The MongoDB models (e.g., boardModel, userModel, cardModel, etc.) and the configuration files (e.g., mongodb.js) that establish the connection to MongoDB.
- External Integrations & Providers: Separate modules to deal with external services such as OAuth (Passport.js), email services (Nodemailer, Gmail integration), Cloudinary (for image uploads), and Twilio (for SMS notifications).
- Real-time/Sockets: Socket configuration and handling modules for real-time updates integrated via Socket.IO.
- Utility and Validation Layers: Modules for algorithms, formatters, constants, sorting, and request validations that ensure data integrity.

##### Detail the Flow and Interactions
- Clients (web or mobile applications) interact with the API by making HTTP requests to various endpoints.
- API Routes (organized under src/routes/v1 and v2) map to Controllers where the incoming HTTP requests are initially handled.
- Controllers validate the input (utilizing validations from src/validations) and then delegate processing to appropriate services.
- Services execute business logic, interact with the database through models available in src/models, and engage with external providers as needed.
- For real-time interactions, the socket layer (src/sockets) handles events to update clients in real time (for example, when moving cards or sending messages).

##### Architectural Patterns and Design Principles
- Layered Architecture / Separation of Concerns: Distinct layers for API routing, controllers, services, and data models.
- Modularization: Each major feature (such as boards, cards, and columns) is split into its own controller, service, and model, making future extensions or maintenance easier.
- API Versioning: Multiple versions (v1, v2) of routes are maintained, likely to support API evolution.
- Integration with External Services: Clear boundaries between core logic and integrations like email, SMS, and image hosting.
- Error Handling: Use of custom utility classes for error management ensures centralized error processing.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    Client["Client Application"]:::client

    %% API Gateway / Express Server
    APIServer["API Gateway/Express Server"]:::gateway

    %% API Layer (Versioned Routes)
    subgraph "API Layer"
        RoutesV1["Routes v1"]:::api
        RoutesV2["Routes v2"]:::api
    end

    %% Controller Layer
    Controllers["Controllers"]:::controller

    %% Service Layer
    Services["Services"]:::service

    %% Persistence Layer
    subgraph "Persistence Layer"
        Models["MongoDB Models"]:::model
        MongoConfig["MongoDB Config"]:::model
    end

    %% External Integrations & Providers
    subgraph "External Integrations"
        Passport["OAuth (Passport.js)"]:::external
        Cloudinary["Image Service (Cloudinary)"]:::external
        Email["Email Service (Nodemailer/Gmail)"]:::external
        SMS["SMS Service (Twilio)"]:::external
        Providers["Additional Providers"]:::external
    end

    %% Real-time Communication
    SocketIO["Socket.IO"]:::realtime

    %% Utility and Validation Layers
    Utilities["Utilities"]:::utility
    Validations["Validations"]:::utility

    %% Flow Connections
    Client -->|"HTTP Request"| APIServer
    APIServer -->|"Routes Request"| RoutesV1
    APIServer -->|"Routes Request"| RoutesV2
    RoutesV1 -->|"calls"| Controllers
    RoutesV2 -->|"calls"| Controllers
    Controllers -->|"delegates"| Services
    Controllers -->|"validates"| Validations
    Controllers -->|"utilizes"| Utilities
    Services -->|"business logic"| Models
    Services -->|"uses"| MongoConfig
    Services -->|"integrates"| Passport
    Services -->|"integrates"| Cloudinary
    Services -->|"integrates"| Email
    Services -->|"integrates"| SMS
    Services -->|"integrates"| Providers
    APIServer -->|"real-time events"| SocketIO
    SocketIO -->|"pushes updates"| Client

    %% Click Events
    click RoutesV1 "https://github.com/nguyenchithannh/trello-apis/tree/master/src/routes/v1"
    click RoutesV2 "https://github.com/nguyenchithannh/trello-apis/tree/master/src/routes/v2"
    click Controllers "https://github.com/nguyenchithannh/trello-apis/tree/master/src/controllers"
    click Services "https://github.com/nguyenchithannh/trello-apis/tree/master/src/services"
    click Models "https://github.com/nguyenchithannh/trello-apis/tree/master/src/models"
    click MongoConfig "https://github.com/nguyenchithannh/trello-apis/blob/master/src/config/mongodb.js"
    click Passport "https://github.com/nguyenchithannh/trello-apis/blob/master/src/config/passport.js"
    click Cloudinary "https://github.com/nguyenchithannh/trello-apis/blob/master/src/config/cloudinary.js"
    click Email "https://github.com/nguyenchithannh/trello-apis/tree/master/src/mail"
    click SMS "https://github.com/nguyenchithannh/trello-apis/blob/master/src/mail/sendToTelephone.js"
    click Providers "https://github.com/nguyenchithannh/trello-apis/tree/master/src/providers"
    click SocketIO "https://github.com/nguyenchithannh/trello-apis/tree/master/src/sockets"
    click Utilities "https://github.com/nguyenchithannh/trello-apis/tree/master/src/utils"
    click Validations "https://github.com/nguyenchithannh/trello-apis/tree/master/src/validations"

    %% Styles
    classDef client fill:#B3E5FC,stroke:#039BE5,stroke-width:2px;
    classDef gateway fill:#C8E6C9,stroke:#2E7D32,stroke-width:2px;
    classDef api fill:#E1BEE7,stroke:#6A1B9A,stroke-width:2px;
    classDef controller fill:#FFF9C4,stroke:#F9A825,stroke-width:2px;
    classDef service fill:#FFCCBC,stroke:#D84315,stroke-width:2px;
    classDef model fill:#E0F7FA,stroke:#006064,stroke-width:2px;
    classDef external fill:#F0F4C3,stroke:#827717,stroke-width:2px;
    classDef realtime fill:#D1C4E9,stroke:#5E35B1,stroke-width:2px;
    classDef utility fill:#FFECB3,stroke:#FF8F00,stroke-width:2px;
```
