# eduardosouzv/pokefight-api architecture

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

## Overview

Key Components to Include:

##### Client Layer
- External clients making API requests
- Webhook endpoints receiving battle results

##### API Layer
- Express.js server (src/app.js, src/server.js)
- Routes (src/routes/)
- Controllers (src/controllers/PokemonController.js)

##### Service Layer
- PokeAPI Integration (src/services/PokeApiService.js)
- Webhook Service (src/services/WebhookService.js)

##### Queue System
- Bull Queue implementation (src/lib/Queue.js)
- Battle Creation Job (src/jobs/BattleCreation.js)

##### Data Layer
- PostgreSQL Database (src/database/)
- Redis for queue management (src/config/redis.js)

##### Connection Flows
a. Client → API Routes → Controllers
b. Controllers → Services
c. Services → External APIs (PokeAPI)
d. Controllers → Queue System
e. Queue System → Battle Processing → Webhook Notifications

##### Component Details

**[External Clients] → [Swagger UI (Port 7777)]**
- ↓
**[API Gateway (Port 3333)]**
- ↓
**[Routes Layer]**
- Pokemon Routes
- Queue Monitoring Routes
- ↓
**[Controllers]**
- Pokemon Controller
- ↓
**[Services Layer]**
- PokeAPI Service → [External PokeAPI]
- Webhook Service
- ↓
**[Queue System (Bull)]**
- Battle Creation Jobs
- Queue Monitoring (Bull Board)
- ↓
**[Data Storage]**
- PostgreSQL (Battles)
- Redis (Queue Management)
- ↓
**[Webhook Notifications] → [External Webhook Endpoints]**

##### Additional Elements
- A container boundary showing Docker components
- Environment configurations
- Database schema for Battles table
- Monitoring points (Bull Board)

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Clients
        EC[External Clients]:::client
        WH[Webhook Endpoints]:::client
    end

    subgraph API
        SV[Server]:::api
        click SV "https://github.com/eduardosouzv/pokefight-api/blob/main/src/server.js"
        APP[Application]:::api
        click APP "https://github.com/eduardosouzv/pokefight-api/blob/main/src/app.js"
        
        subgraph Routes
            PR["Pokemon Routes"]:::route
            click PR "https://github.com/eduardosouzv/pokefight-api/blob/main/src/routes/PokemonRouter.js"
            QR["Queue Monitor Routes"]:::route
            click QR "https://github.com/eduardosouzv/pokefight-api/blob/main/src/routes/QueueMonitoringRouter.js"
        end
        
        subgraph Controllers
            PC[Pokemon Controller]:::controller
            click PC "https://github.com/eduardosouzv/pokefight-api/blob/main/src/controllers/PokemonController.js"
        end
    end

    subgraph Services
        PS[PokeAPI Service]:::service
        click PS "https://github.com/eduardosouzv/pokefight-api/blob/main/src/services/PokeApiService.js"
        WS[Webhook Service]:::service
        click WS "https://github.com/eduardosouzv/pokefight-api/blob/main/src/services/WebhookService.js"
    end

    subgraph Clients Internal
        PAC[PokeAPI Client]:::client
        click PAC "https://github.com/eduardosouzv/pokefight-api/blob/main/src/clients/PokeApiClient.js"
        WC[Webhook Client]:::client
        click WC "https://github.com/eduardosouzv/pokefight-api/blob/main/src/clients/WebhookClient.js"
    end

    subgraph Queue System
        Q[Queue Implementation]:::queue
        click Q "https://github.com/eduardosouzv/pokefight-api/blob/main/src/lib/Queue.js"
        BJ[Battle Creation Job]:::queue
        click BJ "https://github.com/eduardosouzv/pokefight-api/blob/main/src/jobs/BattleCreation.js"
        QP[Queue Processing]:::queue
        click QP "https://github.com/eduardosouzv/pokefight-api/blob/main/src/queue.js"
    end

    subgraph Data Layer
        DB[(PostgreSQL)]:::database
        click DB "https://github.com/eduardosouzv/pokefight-api/blob/main/src/database/index.js"
        RD[(Redis)]:::database
        click RD "https://github.com/eduardosouzv/pokefight-api/blob/main/src/config/redis.js"
        BM[Battle Model]:::model
        click BM "https://github.com/eduardosouzv/pokefight-api/blob/main/src/models/Battle.js"
    end

    subgraph External
        PAPI[PokeAPI]:::external
    end

    EC --> SV
    SV --> APP
    APP --> PR & QR
    PR & QR --> PC
    PC --> PS & WS
    PS --> PAC
    WS --> WC
    PAC --> PAPI
    WC --> WH
    PC --> Q
    Q --> BJ
    BJ --> QP
    Q --> RD
    BJ --> DB
    DB --> BM

    classDef client fill:#6495ED
    classDef api fill:#98FB98
    classDef route fill:#90EE90
    classDef controller fill:#98FB98
    classDef service fill:#DDA0DD
    classDef queue fill:#FFD700
    classDef database fill:#DDA0DD
    classDef external fill:#A9A9A9
    classDef model fill:#20B2AA

    %% Legend
    subgraph Legend
        L1[Client]:::client
        L2[API Component]:::api
        L3[Service]:::service
        L4[Queue]:::queue
        L5[Database]:::database
        L6[External]:::external
        L7[Model]:::model
    end
```
