# alvseven/url-shortener architecture

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

- Interactive diagram: https://gitdiagram.com/alvseven/url-shortener
- Repository: https://github.com/alvseven/url-shortener

## Overview

##### Main System Components

a) Client Layer
- External clients/applications that consume the API
- Connection through HTTP/HTTPS

b) API Layer (NestJS Application)
- Main modules:
- Auth Module
- Users Module
- Links Module
- Middleware Layer (Auth Guard, Validation)

c) Database Layer
- PostgreSQL Database
- Prisma ORM as the data access layer

##### Detailed Component Breakdown

For each major component, represent the following:

a) Auth Module:
- AuthController: handling authentication endpoints
- AuthService: business logic for authentication
- AuthGuard: JWT validation
- UsersRepository: user data access

b) Users Module:
- CreateUserController: user registration
- CreateUserService: user creation logic
- UsersRepository: user management

c) Links Module:
- Generate Link
- Get User Links
- Redirect
- Soft Delete Link
- Update Link
- Each with its respective Controller/Service/Repository pattern

##### Data Flow Representation

- Client requests → API endpoints
- Inter-module communication
- Database interactions through Prisma
- Authentication flow
- URL redirection flow

##### Specific Design Elements

Components with distinct visual representations:

a) Infrastructure Components:
- Docker containers (one for API, one for PostgreSQL)
- Environment configuration

b) Security Layer:
- JWT authentication flow
- Password hashing (bcryptjs)

c) Database Schema:
- Users table
- Links table with relationships

##### Key Interactions to Highlight

- URL shortening flow
- URL redirection flow
- User authentication process
- Click tracking mechanism

##### Technical Details

- API_URL for redirection
- JWT authentication flow
- Prisma ORM layer
- Docker containerization
- Environment configuration

##### Infrastructure Layer (Docker, Environment)

This structure reflects the modular nature of the NestJS application while showing the clear separation of concerns and the interaction between different components of the system.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph CL["Client Layer"]
        Client["External Clients"]:::client
    end

    subgraph AL["API Layer"]
        API["API Gateway"]:::api
        
        subgraph Modules["Application Modules"]
            AuthMod["Auth Module"]:::module
            UsersMod["Users Module"]:::module
            LinksMod["Links Module"]:::module
        end
        
        subgraph AuthComp["Auth Components"]
            AuthCtrl["Auth Controller"]:::controller
            AuthSvc["Auth Service"]:::service
            AuthGuard["Auth Guard"]:::middleware
            AuthRepo["Users Repository"]:::repository
        end
        
        subgraph UsersComp["Users Components"]
            CreateUserCtrl["Create User Controller"]:::controller
            CreateUserSvc["Create User Service"]:::service
            UsersRepo["Users Repository"]:::repository
        end
        
        subgraph LinksComp["Links Components"]
            GenLink["Generate Link"]:::component
            GetLinks["Get User Links"]:::component
            Redirect["Redirect"]:::component
            DeleteLink["Soft Delete Link"]:::component
            UpdateLink["Update Link"]:::component
            LinksRepo["Links Repository"]:::repository
        end
    end
    
    subgraph DL["Database Layer"]
        Prisma["Prisma ORM"]:::orm
        DB[(PostgreSQL)]:::database
    end
    
    subgraph IL["Infrastructure Layer"]
        Docker["Docker Containers"]:::infra
        Env["Environment Config"]:::config
    end

    %% Connections
    Client --> API
    API --> AuthMod & UsersMod & LinksMod
    
    AuthMod --> AuthCtrl
    AuthCtrl --> AuthSvc
    AuthSvc --> AuthRepo
    AuthGuard --> AuthSvc
    
    UsersMod --> CreateUserCtrl
    CreateUserCtrl --> CreateUserSvc
    CreateUserSvc --> UsersRepo
    
    LinksMod --> GenLink & GetLinks & Redirect & DeleteLink & UpdateLink
    GenLink & GetLinks & DeleteLink & UpdateLink --> LinksRepo
    
    AuthRepo & UsersRepo & LinksRepo --> Prisma
    Prisma --> DB
    
    %% Click Events
    click AuthMod "https://github.com/alvseven/url-shortener/tree/main/src/modules/auth"
    click AuthCtrl "https://github.com/alvseven/url-shortener/blob/main/src/modules/auth/auth.controller.ts"
    click AuthSvc "https://github.com/alvseven/url-shortener/blob/main/src/modules/auth/auth.service.ts"
    click AuthGuard "https://github.com/alvseven/url-shortener/blob/main/src/modules/auth/auth.guard.ts"
    click AuthRepo "https://github.com/alvseven/url-shortener/blob/main/src/modules/auth/repositories/users.repository.ts"
    
    click UsersMod "https://github.com/alvseven/url-shortener/tree/main/src/modules/users"
    click CreateUserCtrl "https://github.com/alvseven/url-shortener/blob/main/src/modules/users/create-user/create-user.controller.ts"
    click CreateUserSvc "https://github.com/alvseven/url-shortener/blob/main/src/modules/users/create-user/create-user.service.ts"
    click UsersRepo "https://github.com/alvseven/url-shortener/blob/main/src/modules/users/repositories/users.repository.ts"
    
    click LinksMod "https://github.com/alvseven/url-shortener/tree/main/src/modules/links"
    click GenLink "https://github.com/alvseven/url-shortener/tree/main/src/modules/links/generate-link"
    click GetLinks "https://github.com/alvseven/url-shortener/tree/main/src/modules/links/get-user-links"
    click Redirect "https://github.com/alvseven/url-shortener/tree/main/src/modules/links/redirect"
    click DeleteLink "https://github.com/alvseven/url-shortener/tree/main/src/modules/links/soft-delete-link"
    click UpdateLink "https://github.com/alvseven/url-shortener/tree/main/src/modules/links/update-link"
    click LinksRepo "https://github.com/alvseven/url-shortener/blob/main/src/modules/links/repositories/links.repository.ts"
    
    click Prisma "https://github.com/alvseven/url-shortener/blob/main/src/shared/database/prisma-client.ts"
    click DB "https://github.com/alvseven/url-shortener/blob/main/prisma/schema.prisma"
    click Docker "https://github.com/alvseven/url-shortener/tree/main/Dockerfile"
    click Env "https://github.com/alvseven/url-shortener/blob/main/src/shared/config/env.ts"

    %% Styling
    classDef client fill:#f9f,stroke:#333,stroke-width:2px
    classDef api fill:#aaf,stroke:#333,stroke-width:2px
    classDef module fill:#ffa,stroke:#333,stroke-width:2px
    classDef controller fill:#afa,stroke:#333,stroke-width:2px
    classDef service fill:#aff,stroke:#333,stroke-width:2px
    classDef middleware fill:#faa,stroke:#333,stroke-width:2px
    classDef repository fill:#ddd,stroke:#333,stroke-width:2px
    classDef component fill:#daf,stroke:#333,stroke-width:2px
    classDef orm fill:#fda,stroke:#333,stroke-width:2px
    classDef database fill:#afd,stroke:#333,stroke-width:2px
    classDef infra fill:#dfa,stroke:#333,stroke-width:2px
    classDef config fill:#fad,stroke:#333,stroke-width:2px
```
