# kartuludus/poraclejs architecture

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

- Interactive diagram: https://gitdiagram.com/kartuludus/poraclejs
- Repository: https://github.com/kartuludus/poraclejs

## Overview

##### Project Purpose and Type
- PoracleJS is a full-stack NodeJS application focused on real-time alerts driven by Discord and Telegram bots.
- The README highlights its role as a configurable alarm system, and the file tree shows clear separations among configuration files, controllers, libraries, and API routes.

##### Analyzing the File Structure
- Top-Level Directories:
- “config”: Contains default configurations, custom environment variables, and default setting files (e.g., for broadcasts, geofence, testdata) that guide how the application behaves.
- “src”: Core application source code. It includes high-level application files, workers (weatherWorker, statsWorker), and multiple subdirectories such as controllers, lib, routes, and init.
- “src/controllers”: Contains many modules that handle various alarm types (monster, raid, gym, quest, etc.) and shared logic.
- “src/lib”: A rich collection of libraries organized by their role:
∗ “discord” and “telegram” subdirectories that house integration code for real-time messaging commands, event workers, utilities, and message handling.
∗ “db” migrations for setting up and updating the database schema.
∗ Other utility modules (e.g., geocoding, URL shorteners, Promise queues, and Handlebars helpers).
- “src/routes”: Defines API endpoints for various functionalities such as configuration, geofencing, posting messages, and tracking different event types. This suggests an HTTP API layer.
- “examples” and “test”: Provide sample configuration and test cases, indicating support for customization and robustness in the code.

##### Key Components and Their Relationships
a. Main Components:
- Bot Integrations: Two core entry points for user interactions via Discord and Telegram. These are managed in the “src/lib/discord” and “src/lib/telegram” directories. They include command parsing, event handling, and message formatting.
- Core Controllers: Located in “src/controllers”, these modules are responsible for processing specific alarm types (e.g., monster, raid, quest, nest) and likely serve as intermediaries between incoming commands and business logic.
- API Layer: Found in “src/routes”, where HTTP endpoints allow external clients or admin interfaces to check health, update configurations, or trigger webhooks.
- Configuration Management: The “config” folder holds customizable settings and environment-specific variables. This is central to how the system adapts to different deployment needs.
- Worker Processes: Separate background workers (e.g., controllerWorker.js, statsWorker.js, weatherWorker.js) process tasks asynchronously. They work behind the scenes to generate alerts or process scheduled tasks.
- Database and Data Migrations: The “src/lib/db/migrations” folder indicates that database migrations are a controlled part of the system architecture, maintaining data integrity and versioning.
- Utility Services and External Integrations: Libraries for geocoding, mapping (custom maps), URL shorteners, and other helper functions assist in data processing and external API communication.

b. Relationships and Data Flow:
- User commands sent from Discord or Telegram enter the system through the respective bot integrations.
- Incoming messages are parsed by the command handlers (located under “src/lib/discord/commando/commands” and “src/lib/telegram/commands”), which route requests to the appropriate controllers in “src/controllers”.
- Controllers interact with shared libraries (from “src/lib”) for tasks like geocoding, data caching, and database operations.
- Worker processes run in parallel to handle tasks like stats generation, weather updates, and periodic scanning.
- The API layer (via “src/routes”) manages administrative or configuration requests which may in turn update configurations, trigger alerts, or interact with the underlying controllers.
- Data persistence and retrieval are managed through the database and its migrations.

##### Architectural Patterns and Design Principles
- Modularity: The clear separation between controllers, libraries, and integration modules (Discord/Telegram) exemplifies modular design.
- Separation of Concerns: Command parsing, business logic, utility functions, and configuration management are all isolated in their respective directories.
- Asynchronous Processing: Use of workers and Promise queues (e.g., FairPromiseQueue.js) reflects an emphasis on efficient asynchronous processing.
- Extensibility and Customizability: Given the high configurability through discord commands, telegram commands, and customizable configuration files, the design emphasizes flexibility.

##### Detailing and Clarity
- Every component is clearly identified with a label.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Subgraph: User Interfaces
    subgraph "User Interfaces"
        discord["Discord Integration Module"]:::ui
        telegram["Telegram Integration Module"]:::ui
        api["API Server/Endpoints"]:::ui
    end

    %% Subgraph: Core Processing
    subgraph "Core Processing"
        controllers["Core Controllers"]:::processing
        utilities["Utility Services & External Integrations"]:::utility
    end

    %% Subgraph: Configuration & Persistence
    subgraph "Configuration & Persistence"
        configM["Configuration Management"]:::config
        database["Database & Data Migrations"]:::database
    end

    %% Subgraph: Worker Processes
    subgraph "Worker Processes"
        controllerWorker["Controller Worker"]:::worker
        stats["Stats Worker"]:::worker
        weather["Weather Worker"]:::worker
    end

    %% Connections from User Interfaces to Core Processing
    discord -->|"commands"| controllers
    telegram -->|"commands"| controllers
    api -->|"adminCalls"| controllers

    %% Connection from Configuration to Core Processing
    configM -->|"configUpdates"| controllers

    %% Controllers using Utility Services
    controllers -->|"uses"| utilities

    %% Controllers trigger Worker Processes
    controllers -->|"triggers"| controllerWorker
    controllers -->|"triggers"| stats
    controllers -->|"triggers"| weather

    %% Controllers interact with Database
    controllers -->|"dataOps"| database

    %% Optionally, Utility Services can interact with Database
    utilities -->|"dataSupport"| database

    %% Click Events
    click discord "https://github.com/kartuludus/poraclejs/tree/master/src/lib/discord"
    click telegram "https://github.com/kartuludus/poraclejs/tree/master/src/lib/telegram"
    click controllers "https://github.com/kartuludus/poraclejs/tree/master/src/controllers"
    click api "https://github.com/kartuludus/poraclejs/tree/master/src/routes"
    click configM "https://github.com/kartuludus/poraclejs/tree/master/config"
    click controllerWorker "https://github.com/kartuludus/poraclejs/blob/master/src/controllerWorker.js"
    click stats "https://github.com/kartuludus/poraclejs/blob/master/src/statsWorker.js"
    click weather "https://github.com/kartuludus/poraclejs/blob/master/src/weatherWorker.js"
    click database "https://github.com/kartuludus/poraclejs/tree/master/src/lib/db/migrations"
    click utilities "https://github.com/kartuludus/poraclejs/tree/master/src/lib"

    %% Styles
    classDef ui fill:#C0E7FF,stroke:#007EC2,stroke-width:2px;
    classDef processing fill:#FFE4B5,stroke:#FF8C00,stroke-width:2px;
    classDef config fill:#C8E6C9,stroke:#388E3C,stroke-width:2px;
    classDef database fill:#FFCDD2,stroke:#F44336,stroke-width:2px;
    classDef worker fill:#FFF9C4,stroke:#FBC02D,stroke-width:2px;
    classDef utility fill:#D1C4E9,stroke:#7E57C2,stroke-width:2px;
```
