# triellis/clash-of-codes-api architecture

> Architecture diagram of the GitHub repository triellis/clash-of-codes-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/triellis/clash-of-codes-api
- Repository: https://github.com/triellis/clash-of-codes-api

## Overview

##### Main Components Identification

a) API Layer
- Routes under /src/routes
- Separate Admin routes (/admin) from regular user routes
- Authentication routes (/login)
- Clan-related endpoints (/myClan, /clansRanks)
- Scoring system routes (/pastScores)

b) Utility Layer
- Database connection (db.ts)
- Redis cache (redis.ts)
- Authentication/Authorization middleware (middlewares.ts)
- Helper functions (functions.ts)
- Type definitions (types.ts)

##### Architecture Pattern Representation

**┌─────────────────────────────────────────┐**
- Client Layer               │
- ┘
- ↓
**┌─────────────────────────────────────────┐**
- API Gateway Layer           │
- (Express.js Routes + Middleware)      │
- ┘
- ↓
**┌─────────────────────────────────────────┐**
- Service Layer                │
- (Business Logic + Utilities)        │
- ┘
- ↓
**┌───────────────┐  ┌───────────────────┐**
- Database     │  │    Redis Cache    │
- ┘  └───────────────────┘

##### Component Relationships

- Request flow from client to API
- Data flow between services
- Cache interactions
- Database operations

##### Detailed Elements

a) Route Groups:
**/admin**
- /users
- /config
- /cfSecretConfig
**/clansRanks**
**/login**
**/myClan**
**/pastScores**

b) Utility Services:
**Utilities**
- Database Connection
- Redis Cache
- Middleware
- Helper Functions
- Type Definitions

##### Additional Notes

- Version information (TypeScript/Node.js)
- Error handling paths
- Scalable components

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph Client
        CL[Client Application]
    end

    subgraph "API Gateway Layer"
        direction TB
        API[API Gateway]
        AUTH["Authentication"]:::security
        MIDDLE["Middleware Layer"]:::security
    end

    subgraph "Route Layer"
        direction TB
        ADMIN["Admin Routes"]:::admin
        CLAN["Clan Routes"]:::api
        AUTH_ROUTES["Auth Routes"]:::security
        SCORE["Scoring Routes"]:::api

        subgraph "Admin Endpoints"
            direction TB
            USERS["User Management"]:::admin
            CONFIG["Configuration"]:::admin
            SECRET["Secret Config"]:::admin
        end
    end

    subgraph "Service Layer"
        direction TB
        DB_CONN["Database Connection"]:::util
        REDIS_CONN["Redis Cache"]:::util
        HELPERS["Helper Functions"]:::util
        TYPES["Type Definitions"]:::util
    end

    subgraph "Storage Layer"
        direction TB
        DB[(Database)]:::storage
        REDIS[(Redis Cache)]:::storage

        subgraph "Database Collections"
            direction TB
            USERS_COL[("Users")]:::collection
            CLANS_COL[("Clans")]:::collection
            SCORES_COL[("Scores")]:::collection
            CONFIG_COL[("Configs")]:::collection
        end

        subgraph "Redis Data"
            direction TB
            USER_CACHE["User Sessions"]:::redis
            CLAN_CACHE["Clan Data"]:::redis
            SCORE_CACHE["Recent Scores"]:::redis
            CONFIG_CACHE["Config Cache"]:::redis
        end
    end

    %% Connections
    CL --> API
    API --> AUTH
    AUTH --> MIDDLE
    MIDDLE --> ADMIN & CLAN & AUTH_ROUTES & SCORE

    ADMIN --> USERS & CONFIG & SECRET
    
    USERS & CONFIG & SECRET & CLAN & AUTH_ROUTES & SCORE --> DB_CONN
    DB_CONN --> DB
    DB --> USERS_COL & CLANS_COL & SCORES_COL & CONFIG_COL
    
    USERS & CONFIG & SECRET & CLAN & AUTH_ROUTES & SCORE --> REDIS_CONN
    REDIS_CONN --> REDIS
    REDIS --> USER_CACHE & CLAN_CACHE & SCORE_CACHE & CONFIG_CACHE
    
    %% Data Relations
    USERS_COL -. "Cached As" .-> USER_CACHE
    CLANS_COL -. "Cached As" .-> CLAN_CACHE
    SCORES_COL -. "Cached As" .-> SCORE_CACHE
    CONFIG_COL -. "Cached As" .-> CONFIG_CACHE
    
    USERS & CONFIG & SECRET & CLAN & AUTH_ROUTES & SCORE --> HELPERS
    USERS & CONFIG & SECRET & CLAN & AUTH_ROUTES & SCORE --> TYPES

    %% Styles
    classDef admin fill:#ff6b6b
    classDef api fill:#4ecdc4
    classDef security fill:#ffe66d
    classDef util fill:#95a5a6
    classDef storage fill:#34495e,color:#fff
    classDef collection fill:#2ecc71,color:#fff
    classDef redis fill:#dc3545,color:#fff
    
    %% Click Events
    click ADMIN "https://github.com/Triellis/clash-of-codes-api/tree/main/src/routes/admin/"
    click AUTH_ROUTES "https://github.com/Triellis/clash-of-codes-api/blob/main/src/routes/login.ts"
    click CLAN "https://github.com/Triellis/clash-of-codes-api/blob/main/src/routes/clansRanks.ts"
    click SCORE "https://github.com/Triellis/clash-of-codes-api/blob/main/src/routes/pastScores.ts"
    click DB_CONN "https://github.com/Triellis/clash-of-codes-api/blob/main/src/util/db.ts"
    click REDIS_CONN "https://github.com/Triellis/clash-of-codes-api/blob/main/src/util/redis.ts"
    click AUTH "https://github.com/Triellis/clash-of-codes-api/blob/main/src/util/middlewares.ts"
    click HELPERS "https://github.com/Triellis/clash-of-codes-api/blob/main/src/util/functions.ts"
    click TYPES "https://github.com/Triellis/clash-of-codes-api/blob/main/src/util/types.ts"
    click CONFIG "https://github.com/Triellis/clash-of-codes-api/blob/main/src/routes/admin/config.ts"
    click SECRET "https://github.com/Triellis/clash-of-codes-api/blob/main/src/routes/admin/cfSecretConfig.ts"
    click USERS "https://github.com/Triellis/clash-of-codes-api/blob/main/src/routes/admin/users.ts"
```
