# ingluispalacio/api_with_nest architecture

> Architecture diagram of the GitHub repository ingluispalacio/api_with_nest, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-02-14). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ingluispalacio/api_with_nest
- Repository: https://github.com/ingluispalacio/api_with_nest

## Overview

Based on our review of the file tree and README, this project is an API built with the NestJS framework. Its primary purpose is to provide a robust backend service featuring JWT-based authentication, custom authorization logic, and modular handling for user management, task management, audits, and permissions. Additionally, it uses Prisma ORM for database interactions and leverages Docker Compose for setting up the database container.

##### Main Components Identification
- API Gateway / Entry Point: Represented by the main application file (src/main.ts) and the central module (src/app.module.ts).
- Modules & Features: Break down individual modules such as:
- Authentication (src/modules/auth): Handles JWT authentication, login endpoints, and uses custom decorators.
- Authorization & Permissions (src/modules/permissions and src/modules/roles): Manages resource-specific permissions and roles.
- User Management (src/modules/users): Provides endpoints for user-related actions.
- Task Management (src/modules/tasks): Handles tasks operations, including middleware and logging.
- Auditing (src/modules/audits): Captures event or audit trails.
- Shared/Common Services: Under src/common, include elements like:
- Database Abstraction: prisma.service.ts under src/common/database, which encapsulates Prisma ORM logic.
- Global Components: Exception filters, response interceptors, guards (for custom authorization), and logging helpers.
- Database: Refer to the Prisma directory that contains the schema, migrations, and seed script, as well as the docker-compose.yml that is used to provision the database.
- External Dependencies: Include third-party services and tools such as NestJS itself, Docker (for the database), and any libraries involved in JWT management.

##### Relationships and Interactions
- How HTTP requests enter through the NestJS controller layer (each module’s controller) and are processed by the corresponding service.
- How the authentication flow works: Clients provide a JWT that passes through guards and interceptors before reaching business logic.

##### Architectural Patterns and Design Principles
- Module Separation: Emphasize the modular design, with each domain (auth, users, tasks, audits, permissions, roles) encapsulated by its own module.
- Layered Architecture: Illustrate clear separations between controllers (presentation), services (business logic), and the database layer.
- Dependency Injection: Note how NestJS utilizes dependency injection to integrate shared components (like logging or database services) across modules.
- Middleware and Interceptors: Include any elements that manipulate request/response flows, such as the logger middleware in the tasks module or global HTTP error filters.

##### Relevant Technologies and Frameworks
- NestJS Framework for the overall application structure.
- Prisma ORM, shown under the prisma folder, essential for relational data management.
- Docker: The docker-compose.yml file indicates the containerized database setup.
- JWT Authentication: Explain the role of tokens in securing API endpoints.
- Testing: Mention that there are test files (in both src/* and test directories) to ensure each component’s functionality.

##### Detailing and Capturing Essential Elements
- That dependency flows (for example, dependency injection relationships) are clear to emphasize the architectural pattern NestJS promotes.
- Configuration files (nest-cli.json, docker-compose.yml, etc.) to remind the viewer that some components (like the database) are managed via separate services.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Entities
    Client["API Client"]:::client
    ApiServer["NestJS API Server"]:::server

    %% Modules Subgraph
    subgraph "Modules"
        AuthModule["Auth Module (JWT & Custom Authorization)"]:::module
        UsersModule["Users Module"]:::module
        TasksModule["Tasks Module"]:::module
        RolesModule["Roles Module"]:::module
        PermissionsModule["Permissions Module"]:::module
        AuditsModule["Audits Module"]:::module
    end

    CommonComp["Common Components (Guards, Filters, Interceptors, Helpers)"]:::common
    Prisma["Prisma ORM Service (with Migrations)"]:::database
    DockerDB["Dockerized Database"]:::database

    %% Connections
    Client -->|"sends request"| ApiServer
    ApiServer -->|"routes to"| AuthModule
    ApiServer -->|"routes to"| UsersModule
    ApiServer -->|"routes to"| TasksModule
    ApiServer -->|"routes to"| RolesModule
    ApiServer -->|"routes to"| PermissionsModule
    ApiServer -->|"routes to"| AuditsModule

    %% Each Module interacts with Common Components (for auth, guards, etc.)
    AuthModule -->|"uses"| CommonComp
    UsersModule -->|"uses"| CommonComp
    TasksModule -->|"uses"| CommonComp
    RolesModule -->|"uses"| CommonComp
    PermissionsModule -->|"uses"| CommonComp
    AuditsModule -->|"uses"| CommonComp

    %% Modules call the Prisma service for data operations
    AuthModule -->|"calls data layer"| Prisma
    UsersModule -->|"calls data layer"| Prisma
    TasksModule -->|"calls data layer"| Prisma
    RolesModule -->|"calls data layer"| Prisma
    PermissionsModule -->|"calls data layer"| Prisma
    AuditsModule -->|"calls data layer"| Prisma

    %% Prisma connects to the Dockerized database
    Prisma -->|"connects to"| DockerDB

    %% Click Events
    click ApiServer "https://github.com/ingluispalacio/api_with_nest/blob/main/src/main.ts"
    click ApiServer "https://github.com/ingluispalacio/api_with_nest/blob/main/src/app.module.ts"
    click AuthModule "https://github.com/ingluispalacio/api_with_nest/tree/main/src/modules/auth"
    click UsersModule "https://github.com/ingluispalacio/api_with_nest/tree/main/src/modules/users"
    click TasksModule "https://github.com/ingluispalacio/api_with_nest/tree/main/src/modules/tasks"
    click RolesModule "https://github.com/ingluispalacio/api_with_nest/tree/main/src/modules/roles"
    click PermissionsModule "https://github.com/ingluispalacio/api_with_nest/tree/main/src/modules/permissions"
    click AuditsModule "https://github.com/ingluispalacio/api_with_nest/tree/main/src/modules/audits"
    click CommonComp "https://github.com/ingluispalacio/api_with_nest/tree/main/src/common"
    click Prisma "https://github.com/ingluispalacio/api_with_nest/blob/main/src/common/database/prisma.service.ts"
    click DockerDB "https://github.com/ingluispalacio/api_with_nest/blob/main/docker-compose.yml"

    %% Styles
    classDef client fill:#7dcfb6,stroke:#333,stroke-width:2px;
    classDef server fill:#8ecae6,stroke:#333,stroke-width:2px;
    classDef module fill:#219ebc,stroke:#333,stroke-width:2px;
    classDef common fill:#ffb703,stroke:#333,stroke-width:2px;
    classDef database fill:#fb8500,stroke:#333,stroke-width:2px;
```
