# matheusguermandi/advanced-node architecture

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

- Interactive diagram: https://gitdiagram.com/matheusguermandi/advanced-node
- Repository: https://github.com/matheusguermandi/advanced-node

## Overview

##### Project type and purpose
- This is a backend API project written in TypeScript, following Clean Architecture and DDD principles.
- It exposes HTTP endpoints for Facebook authentication and profile‐picture changes, persisting user data in Postgres and files in S3.

##### File structure
- Top‐level layers under `src/` mirror Clean Architecture:
- `src/main` – Composition Root, Express adapters, route wiring, middleware, configuration
- `src/application` – Use‐case controllers, request validation, transaction decorators, HTTP helpers
- `src/domain` – Core business rules: entities, use‐case interfaces (contracts), domain errors
- `src/infra` – Implementations of domain contracts: Postgres repos, AWS S3 gateway, Facebook API, JWT/UUID handlers
- Factories in `src/main/factories` assemble controllers, use cases and infra implementations via the Factory pattern.
- Config files (`ormconfig.json`, `.env`), build & test configs (Jest, Travis, tsconfig) and documentation use‐cases in `/documentation`.

##### README for additional insights
- Emphasis on SOLID, Clean Architecture, TDD, and patterns (Factory, Adapter, Decorator, Composite, etc.).
- Two primary use cases illustrated: Facebook authentication and change‐profile‐picture.

a. Main components
1. HTTP Layer – Express server, routers, middleware, multer adapter
2. Application Layer – Controllers, decorators (DB transaction), validators
3. Domain Layer – Use‐case interfaces, business logic, entities, domain errors
4. Infrastructure Layer –
- Postgres DB (via TypeORM)
- AWS S3 for file storage
- External Facebook API (via Axios)
- JWT token service, UUID generator
b. Relationships / data flows
- Client HTTP → Express Router → Controller
- Controller invokes Use‐Case interface → Domain logic → Domain entities
- Use‐Case calls Gateway / Repository interfaces → Infra implementations
- Infra returns data → Domain → Controller → HTTP Response
c. Architectural patterns / principles
- Clean Architecture (layered, dependency inversion)
- Factories & Dependency Injection (in `src/main/factories`)
- Adapter Pattern (Express middleware, Axios‐based Facebook API client)
- Decorator Pattern (DB transaction wrapper around controllers)
- Composite & Builder Pattern (validation rules composition)
d. Key technologies & libraries
- Typescript, Express, TypeORM, AWS‐SDK (S3), Axios, Multer, JWT, UUID, Postgres, Jest

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph "HTTP Layer"
        Server["Express Server"]:::http
        RouterAdapter["Express Router Adapter"]:::http
        MiddlewareAdapter["Express Middleware Adapter"]:::http
        MulterAdapter["Multer Adapter"]:::http
        AuthMiddleware["Authentication Middleware"]:::http
        RoutesLogin["Routes: login.ts"]:::http
        RoutesUser["Routes: user.ts"]:::http
    end

    subgraph "Application Layer"
        FBLoginCtrl["Controller: FacebookLogin"]:::app
        SavePicCtrl["Controller: SavePicture"]:::app
        BaseController["Interface: Controller"]:::app
        DBTransactionDec["Decorator: DBTransaction"]:::app
        ValidatorBuilder["Validator Builder"]:::app
        CompositeValidator["Composite Validator"]:::app
        MimeType["Allowed MIME Types"]:::app
        FileSize["Max File Size"]:::app
        RequiredField["Required Field"]:::app
        AppAuthMw["Application Authentication Middleware"]:::app
    end

    subgraph "Domain Layer"
        FBAuthUC["UseCase: FacebookAuthentication"]:::domain
        ChangePicUC["UseCase: ChangeProfilePicture"]:::domain
        FBAccountEnt["Entity: FacebookAccount"]:::domain
        UserProfileEnt["Entity: UserProfile"]:::domain
        AccessTokenEnt["Entity: AccessToken"]:::domain
        DomainErrors["Domain Errors"]:::domain
        ContractFB["Contract: Facebook API"]:::domain
        ContractFS["Contract: FileStorage"]:::domain
        ContractToken["Contract: Token"]:::domain
        ContractUUID["Contract: UUID"]:::domain
        RepoUserAcc["Repo Contract: UserAccount"]:::domain
        RepoUserProf["Repo Contract: UserProfile"]:::domain
    end

    subgraph "Infrastructure Layer"
        S3Storage["AWS S3 FileStorage"]:::infra
        FBApiClient["Facebook API Client"]:::infra
        AxiosClient["Axios HTTP Client"]:::infra
        JWTHandler["JWT Token Handler"]:::infra
        UUIDHandler["UUID Handler"]:::infra
        PostgresUserAcc["Repo: UserAccount"]:::infra
        PostgresUserProf["Repo: UserProfile"]:::infra
        TypeORMConn["TypeORM Connection Helper"]:::infra
    end

    subgraph "Composition / Factories"
        FactoryIndex["Factory Modules"]:::factory
    end

    Server -->|"uses"| RouterAdapter
    Server -->|"uses"| MiddlewareAdapter
    Server -->|"uses"| MulterAdapter
    RouterAdapter --> RoutesLogin
    RouterAdapter --> RoutesUser
    MiddlewareAdapter --> AuthMiddleware
    RoutesLogin --> FBLoginCtrl
    RoutesUser --> SavePicCtrl

    FBLoginCtrl --> DBTransactionDec
    SavePicCtrl --> DBTransactionDec
    FBLoginCtrl --> ValidatorBuilder
    SavePicCtrl --> ValidatorBuilder
    ValidatorBuilder --> CompositeValidator
    CompositeValidator --> MimeType
    CompositeValidator --> FileSize
    CompositeValidator --> RequiredField
    FBLoginCtrl --> AppAuthMw

    DBTransactionDec --> FBAuthUC
    DBTransactionDec --> ChangePicUC

    FBAuthUC --> FBAccountEnt
    ChangePicUC --> UserProfileEnt

    FBAuthUC --> ContractFB
    ChangePicUC --> ContractFS
    FBAuthUC --> ContractToken
    FBAuthUC --> ContractUUID
    ChangePicUC --> RepoUserAcc
    ChangePicUC --> RepoUserProf

    ContractFB --> FBApiClient
    ContractFS --> S3Storage
    ContractToken --> JWTHandler
    ContractUUID --> UUIDHandler
    RepoUserAcc --> PostgresUserAcc
    RepoUserProf --> PostgresUserProf
    PostgresUserAcc --> TypeORMConn
    PostgresUserProf --> TypeORMConn
    FBApiClient --> AxiosClient

    FactoryIndex --> Server
    FactoryIndex --> RouterAdapter
    FactoryIndex --> MiddlewareAdapter
    FactoryIndex --> FBLoginCtrl
    FactoryIndex --> SavePicCtrl
    FactoryIndex --> FBAuthUC
    FactoryIndex --> ChangePicUC
    FactoryIndex --> FBApiClient
    FactoryIndex --> S3Storage
    FactoryIndex --> JWTHandler
    FactoryIndex --> UUIDHandler
    FactoryIndex --> PostgresUserAcc
    FactoryIndex --> PostgresUserProf

    click Server "https://github.com/matheusguermandi/advanced-node/blob/master/src/main/index.ts"
    click RoutesLogin "https://github.com/matheusguermandi/advanced-node/blob/master/src/main/routes/login.ts"
    click RoutesUser "https://github.com/matheusguermandi/advanced-node/blob/master/src/main/routes/user.ts"
    click RouterAdapter "https://github.com/matheusguermandi/advanced-node/blob/master/src/main/adapters/express-router.ts"
    click MiddlewareAdapter "https://github.com/matheusguermandi/advanced-node/blob/master/src/main/adapters/express-middleware.ts"
    click MulterAdapter "https://github.com/matheusguermandi/advanced-node/blob/master/src/main/adapters/multer.ts"
    click AuthMiddleware "https://github.com/matheusguermandi/advanced-node/blob/master/src/main/middlewares/authentication.ts"
    click FBLoginCtrl "https://github.com/matheusguermandi/advanced-node/blob/master/src/application/controllers/facebook-login.ts"
    click SavePicCtrl "https://github.com/matheusguermandi/advanced-node/blob/master/src/application/controllers/save-picture.ts"
    click BaseController "https://github.com/matheusguermandi/advanced-node/blob/master/src/application/controllers/controller.ts"
    click DBTransactionDec "https://github.com/matheusguermandi/advanced-node/blob/master/src/application/decorators/db-transaction-controller.ts"
    click ValidatorBuilder "https://github.com/matheusguermandi/advanced-node/blob/master/src/application/validation/builder.ts"
    click CompositeValidator "https://github.com/matheusguermandi/advanced-node/blob/master/src/application/validation/composite.ts"
    click MimeType "https://github.com/matheusguermandi/advanced-node/blob/master/src/application/validation/allowed-mime-types.ts"
    click FileSize "https://github.com/matheusguermandi/advanced-node/blob/master/src/application/validation/max-file-size.ts"
    click RequiredField "https://github.com/matheusguermandi/advanced-node/blob/master/src/application/validation/required.ts"
    click AppAuthMw "https://github.com/matheusguermandi/advanced-node/blob/master/src/application/middlewares/authentication.ts"
    click FBAuthUC "https://github.com/matheusguermandi/advanced-node/blob/master/src/domain/use-cases/facebook-authentication.ts"
    click ChangePicUC "https://github.com/matheusguermandi/advanced-node/blob/master/src/domain/use-cases/change-profile-picture.ts"
    click FBAccountEnt "https://github.com/matheusguermandi/advanced-node/blob/master/src/domain/entities/facebook-account.ts"
    click UserProfileEnt "https://github.com/matheusguermandi/advanced-node/blob/master/src/domain/entities/user-profile.ts"
    click AccessTokenEnt "https://github.com/matheusguermandi/advanced-node/blob/master/src/domain/entities/access-token.ts"
    click DomainErrors "https://github.com/matheusguermandi/advanced-node/blob/master/src/domain/entities/errors/authentication.ts"
    click ContractFB "https://github.com/matheusguermandi/advanced-node/blob/master/src/domain/contracts/gateways/facebook.ts"
    click ContractFS "https://github.com/matheusguermandi/advanced-node/blob/master/src/domain/contracts/gateways/file-storage.ts"
    click ContractToken "https://github.com/matheusguermandi/advanced-node/blob/master/src/domain/contracts/gateways/token.ts"
    click ContractUUID "https://github.com/matheusguermandi/advanced-node/blob/master/src/domain/contracts/gateways/uuid.ts"
    click RepoUserAcc "https://github.com/matheusguermandi/advanced-node/blob/master/src/domain/contracts/repos/user-account.ts"
    click RepoUserProf "https://github.com/matheusguermandi/advanced-node/blob/master/src/domain/contracts/repos/user-profile.ts"
    click S3Storage "https://github.com/matheusguermandi/advanced-node/blob/master/src/infra/gateways/aws-s3-file-storage.ts"
    click FBApiClient "https://github.com/matheusguermandi/advanced-node/blob/master/src/infra/gateways/facebook-api.ts"
    click AxiosClient "https://github.com/matheusguermandi/advanced-node/blob/master/src/infra/gateways/axios-client.ts"
    click JWTHandler "https://github.com/matheusguermandi/advanced-node/blob/master/src/infra/gateways/jwt-token-handler.ts"
    click UUIDHandler "https://github.com/matheusguermandi/advanced-node/blob/master/src/infra/gateways/uuid-handler.ts"
    click PostgresUserAcc "https://github.com/matheusguermandi/advanced-node/blob/master/src/infra/repos/postgres/user-account.ts"
    click PostgresUserProf "https://github.com/matheusguermandi/advanced-node/blob/master/src/infra/repos/postgres/user-profile.ts"
    click TypeORMConn "https://github.com/matheusguermandi/advanced-node/blob/master/src/infra/repos/postgres/helpers/connection.ts"

    classDef http fill:#cfe2f3,stroke:#036;
    classDef app fill:#d9ead3,stroke:#090;
    classDef domain fill:#fff2cc,stroke:#e69138;
    classDef infra fill:#fce5cd,stroke:#dd7e6b;
    classDef factory fill:#d3d3d3,stroke:#888;
```
