# gehmac/desafio-backend-wallet architecture

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

- Interactive diagram: https://gitdiagram.com/gehmac/desafio-backend-wallet
- Repository: https://github.com/gehmac/desafio-backend-wallet

## Overview

##### Project Type and Purpose
- The project is a backend API, built with NestJS, for managing users, wallets, and transfers. It handles user authentication using JWT tokens (access and refresh) and supports transactional operations such as transfers and reverts.
- The README outlines a clear RESTful API with versioned endpoints (under /v1/) for authentication, wallet operations, and transaction management. It also notes the use of Docker containers, database migrations via TypeORM, and Node.js (version 22).

##### Analyzing the File Structure
- Top-Level Files & Config: Files like .env, docker-compose.yml, nest-cli.json, and TypeScript configuration files (tsconfig.json) indicate that this is a Node.js project using NestJS, with environment configuration and containerized deployment.
- Core Application Entry Points: The src/main.ts and src/app.ts suggest the starting point of the NestJS application.
- Modular Organization: The project is divided into several modules:
- “auth”: Contains its own controllers, DTOs, services, and strategies for handling token-based authentication.
- “users”: Manages user-related operations with controllers, DTOs for creating/updating users, entities, repositories, and a dedicated service.
- “wallets”: Handles wallet operations, including querying balances and transfers.
- “transactions”: Manages transfer transactions and reversion of transfers.
- “_shared”: Provides shared modules for configuration, database connectivity (migrations and TypeORM configuration), decorators, and guards.
- Each module has its own controller, service, entities, and repository directories. This suggests a modular style (often aligning with Domain Driven Design), and possibly indicating separation of concerns via NestJS modules.

a. Main Components to Include:
- Client / Consumer: Represents external users or clients making HTTP requests to the API.
- API Gateway/Controller: Denotes the entry point of the application (using NestJS), where requests are routed based on endpoints.
- Authentication Module: Handles registration and login endpoints with token issuance and token refreshing. Also includes the strategies and guards to protect routes.
- Users Module: Manages the CRUD operations for user entities.
- Wallets Module: Manages wallet-related functionalities, such as balance checks and wallet-specific endpoints.
- Transactions Module: Manages transfer operations; includes processes for initiating, monitoring, and reverting transfers.
- Shared Components:
– Config Module: Reads environment variables and configuration settings.
– Database Module: Manages the connection to the database and the migration processes (using TypeORM).
– Shared Decorators & Guards: Handles cross-cutting concerns like obtaining the current user and authentication validations.
- Infrastructure Components:
– Docker Containers: Representing the containerized environment (as per docker-compose.yml).
– Relational Database: Represents persistent storage for users, wallets, transactions, etc.

b. Relationships and Interactions:
- Data flow between controllers and their respective service layers.
- Interactions between services – for example, the Wallets Module and Transactions Module may both rely on common validations or shared fundamental business logic.
- Database communication: Indicate that migrations and runtime queries (handled by repositories using TypeORM) connect to a relational database.

c. Architectural Patterns & Design Principles:
- Modular architecture that separates concerns into distinct modules.
- Clean layering (Controller → Service → Repository) which aligns well with Domain-Driven Architecture.
- Authentication is token-based with clear strategies (access and refresh tokens) and utilizes guards to protect secured routes.
- Containerization via Docker highlights a microservice-friendly or at least an isolated, reproducible environment approach.

d. Relevant Technologies and Tools:
- NestJS as the framework for building the API.
- TypeORM for managing database operations and migrations.
- Docker for container orchestration.
- Node.js (version 22) as the runtime.
- JWT (implied by access and refresh tokens) for authentication.

## Diagram (Mermaid)

```mermaid
flowchart TD
    Client["Client/External"]:::external
    API_Gateway["API Gateway (NestJS)"]:::api

    Client -->|"HTTPRequest"| API_Gateway

    subgraph "Modules"
        subgraph "Authentication Module"
            Auth_Module["Auth Module"]:::module
            Auth_Controller["Controller"]:::controller
            Auth_Service["Service"]:::service
            Auth_Module --> Auth_Controller --> Auth_Service
        end

        subgraph "Users Module"
            Users_Module["Users Module"]:::module
            Users_Controller["Controller"]:::controller
            Users_Service["Service"]:::service
            Users_Repository["Repository"]:::repository
            Users_Module --> Users_Controller --> Users_Service --> Users_Repository
        end

        subgraph "Wallets Module"
            Wallets_Module["Wallets Module"]:::module
            Wallets_Controller["Controller"]:::controller
            Wallets_Service["Service"]:::service
            Wallets_Repository["Repository"]:::repository
            Wallets_Module --> Wallets_Controller --> Wallets_Service --> Wallets_Repository
        end

        subgraph "Transactions Module"
            Transactions_Module["Transactions Module"]:::module
            Transactions_Controller["Controller"]:::controller
            Transactions_Service["Service"]:::service
            Transactions_Repository["Repository"]:::repository
            Transactions_Module --> Transactions_Controller --> Transactions_Service --> Transactions_Repository
        end
    end

    API_Gateway -->|"routesTo"| Auth_Module
    API_Gateway -->|"routesTo"| Users_Module
    API_Gateway -->|"routesTo"| Wallets_Module
    API_Gateway -->|"routesTo"| Transactions_Module

    Users_Repository -->|"persists"| Relational_DB
    Wallets_Repository -->|"persists"| Relational_DB
    Transactions_Repository -->|"persists"| Relational_DB

    subgraph "Shared Components"
        Shared_Config["Config Module"]:::shared
        Database_Module["Database Module"]:::shared
        Shared_Decorators["Decorators"]:::shared
        Shared_Guards["Guards"]:::shared
    end

    API_Gateway ---|"loads"| Shared_Config
    API_Gateway ---|"DBConn"| Database_Module
    API_Gateway ---|"secures"| Shared_Guards
    API_Gateway ---|"decorates"| Shared_Decorators

    subgraph "Infrastructure"
        Docker_Container["Docker Container"]:::infra
        Relational_DB["Relational Database"]:::db
    end

    Docker_Container ---|"hosts"| API_Gateway
    Database_Module ---|"migrates"| Relational_DB

    click API_Gateway "https://github.com/gehmac/desafio-backend-wallet/blob/master/src/main.ts"
    click API_Gateway "https://github.com/gehmac/desafio-backend-wallet/blob/master/src/app.ts"
    click Auth_Module "https://github.com/gehmac/desafio-backend-wallet/tree/master/src/auth"
    click Users_Module "https://github.com/gehmac/desafio-backend-wallet/tree/master/src/users"
    click Wallets_Module "https://github.com/gehmac/desafio-backend-wallet/tree/master/src/wallets"
    click Transactions_Module "https://github.com/gehmac/desafio-backend-wallet/tree/master/src/transactions"
    click Shared_Config "https://github.com/gehmac/desafio-backend-wallet/tree/master/src/_shared/config"
    click Database_Module "https://github.com/gehmac/desafio-backend-wallet/tree/master/src/_shared/database"
    click Shared_Decorators "https://github.com/gehmac/desafio-backend-wallet/tree/master/src/_shared/decorators"
    click Shared_Guards "https://github.com/gehmac/desafio-backend-wallet/tree/master/src/_shared/guards"
    click Docker_Container "https://github.com/gehmac/desafio-backend-wallet/blob/master/docker-compose.yml"

    classDef api fill:#b3d9ff,stroke:#333,stroke-width:2px;
    classDef module fill:#ccffcc,stroke:#333,stroke-width:2px;
    classDef controller fill:#ffffcc,stroke:#333,stroke-width:2px;
    classDef service fill:#ffd9b3,stroke:#333,stroke-width:2px;
    classDef repository fill:#ffccff,stroke:#333,stroke-width:2px;
    classDef external fill:#ffe6e6,stroke:#333,stroke-width:2px;
    classDef shared fill:#e6e6ff,stroke:#333,stroke-width:2px;
    classDef infra fill:#ffd6cc,stroke:#333,stroke-width:2px;
    classDef db fill:#ffcccc,stroke:#333,stroke-width:2px;
```
