# huypp03/server_nest architecture

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

## Overview

##### Main Components
- Core Application Module: Represent the main entry (main.ts) and the central AppModule that ties every other module together.
- Controllers: Illustrate separate controllers for different routes and features. For instance, group them into:
- Admin controllers (e.g., todo.controller.ts, user.controller.ts under the admin folder)
- User controllers (e.g., notification.controller.ts, todo.controller.ts, todo_user.controller.ts, user.controller.ts)
- Auth controller, which handles authentication.
- Services and Providers: Display the corresponding services (auth.service, user.service, todo.service, etc.) that encapsulate the business logic. Also include providers that help with notifications, todo handling, etc.
- Database Layer: Show the database module and provider which interact with models and migrations. Include models (user.model, todo.model, notification.model, etc.) along with migration scripts that set up the initial schema.
- Routers/Modules: Map out the routers (e.g., admin.module.ts, auth.module.ts, public.module.ts, user.module.ts) which route requests to the corresponding controllers.
- Utility Layer: Include utility modules (hashing, mail util, file upload util) that support various parts of the application.
- Configuration: Represent configuration files (app.config.ts, environment configuration, Nest CLI settings) that set up the application environment.

##### Relationships and Interactions
- Directional flow from the HTTP entry point (main.ts) to the routers, then forwarded to the appropriate controllers.
- Security aspects are handled via guards and decorators that are applied to controllers and service methods.
- The module layer (NestJS modules) acts as an orchestrator, wiring controllers, services, and providers together. Represent it as a centralized hub that connects various components.

##### Architectural Patterns and Design Principles
- Modular Design: Use modules to encapsulate functionality, making the system more maintainable and scalable.
- Dependency Injection: Illustrate how controllers and services are connected via injected dependencies.
- MVC-Like Structure: Controllers handle routes and API endpoints, services contain business logic, and models represent the data structure.
- Use of Decorators and Guards: Emphasize how custom decorators (e.g., auth.decorator.ts, verify.decorator.ts) and guards (auth.guard, role.guard, verify.guard) secure and manage access to the API.

##### Technologies and Technologies
- NestJS Framework
- TypeScript
- Sequelize (for database migrations and models)
- Express (implicitly via Nest’s use of Express, as indicated by custom type definitions and decorators)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Application Entry
    subgraph "Application Layer"
        MAIN["main.ts"]:::app
        APPMOD["AppModule"]:::app
    end
    MAIN --> APPMOD

    %% Routers/Modules Layer
    subgraph "Routers/Modules"
        ADMINMOD["Admin Module"]:::router
        AUTHMOD["Auth Module"]:::router
        PUBLICMOD["Public Module"]:::router
        USERMOD["User Module"]:::router
    end
    APPMOD --> ADMINMOD
    APPMOD --> AUTHMOD
    APPMOD --> PUBLICMOD
    APPMOD --> USERMOD

    %% Admin Controllers
    subgraph "Admin Controllers"
        ADMINTODO["Admin Todo Controller"]:::controller
        ADMINUSER["Admin User Controller"]:::controller
    end
    ADMINMOD --> ADMINTODO
    ADMINMOD --> ADMINUSER

    %% Auth Controller
    subgraph "Auth Controllers"
        AUTHC["Auth Controller"]:::controller
    end
    AUTHMOD --> AUTHC

    %% User Controllers
    subgraph "User Controllers"
        USERNOTIF["User Notification Controller"]:::controller
        USERTODO["User Todo Controller"]:::controller
        USERTODOUSER["User Todo User Controller"]:::controller
        USERUSER["User Controller"]:::controller
    end
    USERMOD --> USERNOTIF
    USERMOD --> USERTODO
    USERMOD --> USERTODOUSER
    USERMOD --> USERUSER

    %% Service Layer
    subgraph "Service Layer"
        AUTHSVC["Auth Service"]:::service
        USERSVC["User Service"]:::service
        TODOSVC["Todo Service"]:::service
        NOTIFSVC["Notification Service"]:::service
        TODOUTSVC["Todo User Service"]:::service
    end
    %% Controller to Service Connections
    ADMINTODO -->|"calls"| TODOSVC
    ADMINUSER -->|"calls"| USERSVC
    AUTHC -->|"calls"| AUTHSVC
    USERNOTIF -->|"calls"| NOTIFSVC
    USERTODO -->|"calls"| TODOSVC
    USERTODOUSER -->|"calls"| TODOUTSVC
    USERUSER -->|"calls"| USERSVC

    %% Providers for Services
    subgraph "Providers Layer"
        USERPROV["User Provider"]:::provider
        TODOPROV["Todo Provider"]:::provider
        NOTIFPROV["Notification Provider"]:::provider
        TODOUTPROV["Todo User Provider"]:::provider
    end
    USERSVC -->|"uses"| USERPROV
    TODOSVC -->|"uses"| TODOPROV
    NOTIFSVC -->|"uses"| NOTIFPROV
    TODOUTSVC -->|"uses"| TODOUTPROV

    %% Database Layer
    subgraph "Database Layer"
        DBMOD["Database Module"]:::database
        DBPROV["Database Provider"]:::database
        DBMIG["Sequelize Migrations"]:::database
        DBMODELS["Database Models"]:::database
    end
    AUTHSVC -->|"db connection"| DBMOD
    USERSVC -->|"db connection"| DBMOD
    TODOSVC -->|"db connection"| DBMOD
    NOTIFSVC -->|"db connection"| DBMOD
    TODOUTSVC -->|"db connection"| DBMOD

    %% Utility Layer
    subgraph "Utility Layer"
        HASHUTIL["Hashing Utility"]:::utility
        MAILUTIL["Mail Utility"]:::utility
        FILEUPLOAD["File Upload Utility"]:::utility
        DATARESP["Data Response Utility"]:::utility
        STRINGUTIL["String Utility"]:::utility
    end

    %% Configuration Layer
    subgraph "Configuration"
        APPCONF["App Config"]:::configuration
        ENVCONF["Environment Config"]:::configuration
        NESTCLI["Nest CLI Config"]:::configuration
    end

    %% Security Layer
    subgraph "Security"
        AUTHGUARD["Auth Guard"]:::security
        ROLEGUARD["Role Guard"]:::security
        VERIFYGUARD["Verify Guard"]:::security
        AUTHDEC["Auth Decorator"]:::security
        VERIFYDEC["Verify Decorator"]:::security
    end
    %% Example Security connection from a controller (applied as middleware)
    AUTHC ---|"securedBy"| AUTHGUARD

    %% Click Events for Core Application Module
    click MAIN "https://github.com/huypp03/server_nest/blob/main/src/main.ts"
    click APPMOD "https://github.com/huypp03/server_nest/blob/main/src/app.module.ts"

    %% Click Events for Routers/Modules
    click ADMINMOD "https://github.com/huypp03/server_nest/blob/main/src/routers/admin.module.ts"
    click AUTHMOD "https://github.com/huypp03/server_nest/blob/main/src/routers/auth.module.ts"
    click PUBLICMOD "https://github.com/huypp03/server_nest/blob/main/src/routers/public.module.ts"
    click USERMOD "https://github.com/huypp03/server_nest/blob/main/src/routers/user.module.ts"

    %% Click Events for Admin Controllers
    click ADMINTODO "https://github.com/huypp03/server_nest/blob/main/src/controllers/admin/todo.controller.ts"
    click ADMINUSER "https://github.com/huypp03/server_nest/blob/main/src/controllers/admin/user.controller.ts"

    %% Click Event for Auth Controller
    click AUTHC "https://github.com/huypp03/server_nest/blob/main/src/controllers/auth.controller.ts"

    %% Click Events for User Controllers
    click USERNOTIF "https://github.com/huypp03/server_nest/blob/main/src/controllers/user/notification.controller.ts"
    click USERTODO "https://github.com/huypp03/server_nest/blob/main/src/controllers/user/todo.controller.ts"
    click USERTODOUSER "https://github.com/huypp03/server_nest/blob/main/src/controllers/user/todo_user.controller.ts"
    click USERUSER "https://github.com/huypp03/server_nest/blob/main/src/controllers/user/user.controller.ts"

    %% Click Events for Services and Providers
    click AUTHSVC "https://github.com/huypp03/server_nest/blob/main/src/services/auth.service.ts"
    click USERSVC "https://github.com/huypp03/server_nest/blob/main/src/services/user.service.ts"
    click TODOSVC "https://github.com/huypp03/server_nest/blob/main/src/services/todo.service.ts"
    click NOTIFSVC "https://github.com/huypp03/server_nest/blob/main/src/services/notification.service.ts"
    click TODOUTSVC "https://github.com/huypp03/server_nest/blob/main/src/services/todo_user.service.ts"
    click USERPROV "https://github.com/huypp03/server_nest/blob/main/src/providers/user.provider.ts"
    click TODOPROV "https://github.com/huypp03/server_nest/blob/main/src/providers/todo.provider.ts"
    click NOTIFPROV "https://github.com/huypp03/server_nest/blob/main/src/providers/notification.provider.ts"
    click TODOUTPROV "https://github.com/huypp03/server_nest/blob/main/src/providers/todo_user.provider.ts"

    %% Click Events for Database Layer
    click DBMOD "https://github.com/huypp03/server_nest/blob/main/src/database/database.module.ts"
    click DBPROV "https://github.com/huypp03/server_nest/blob/main/src/database/database.provider.ts"
    click DBMIG "https://github.com/huypp03/server_nest/tree/main/src/database/migrations/"
    click DBMODELS "https://github.com/huypp03/server_nest/tree/main/src/database/models/"

    %% Click Events for Utility Layer
    click HASHUTIL "https://github.com/huypp03/server_nest/blob/main/src/utility/hashing.ts"
    click MAILUTIL "https://github.com/huypp03/server_nest/blob/main/src/utility/mail.util.ts"
    click FILEUPLOAD "https://github.com/huypp03/server_nest/blob/main/src/utility/uploadFile.util.ts"
    click DATARESP "https://github.com/huypp03/server_nest/blob/main/src/utility/dataResponse.ts"
    click STRINGUTIL "https://github.com/huypp03/server_nest/blob/main/src/utility/string.util.ts"

    %% Click Events for Configuration
    click APPCONF "https://github.com/huypp03/server_nest/blob/main/src/app.config.ts"
    click ENVCONF "https://github.com/huypp03/server_nest/blob/main/src/env.ts"
    click NESTCLI "https://github.com/huypp03/server_nest/blob/main/nest-cli.json"

    %% Click Events for Security
    click AUTHGUARD "https://github.com/huypp03/server_nest/blob/main/src/guards/auth.guard.ts"
    click ROLEGUARD "https://github.com/huypp03/server_nest/blob/main/src/guards/role.guard.ts"
    click VERIFYGUARD "https://github.com/huypp03/server_nest/blob/main/src/guards/verify.guard.ts"
    click AUTHDEC "https://github.com/huypp03/server_nest/blob/main/src/decorators/auth.decorator.ts"
    click VERIFYDEC "https://github.com/huypp03/server_nest/blob/main/src/decorators/verify.decorator.ts"

    %% Styles
    classDef app fill:#AED6F1,stroke:#1B4F72,stroke-width:2px;
    classDef router fill:#A9DFBF,stroke:#145A32,stroke-width:2px;
    classDef controller fill:#FAD7A0,stroke:#935116,stroke-width:2px,rx:5,ry:5;
    classDef service fill:#F5B7B1,stroke:#78281F,stroke-width:2px;
    classDef provider fill:#D2B4DE,stroke:#6C3483,stroke-width:2px;
    classDef database fill:#AED6F1,stroke:#1B4F72,stroke-width:2px,stroke-dasharray: 5 5;
    classDef utility fill:#D5F5E3,stroke:#196F3D,stroke-width:2px;
    classDef configuration fill:#FCF3CF,stroke:#7D6608,stroke-width:2px;
    classDef security fill:#F9E79F,stroke:#7D6608,stroke-width:2px;
```
