# soramyo/shortlinkmanagement_be architecture

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

- Interactive diagram: https://gitdiagram.com/soramyo/shortlinkmanagement_be
- Repository: https://github.com/soramyo/shortlinkmanagement_be

## Overview

##### Main Components
- Client/User Interface:
- Representing web or mobile clients (or even server-to-server communications) that send HTTP requests to the backend API.
- API Gateway / Routing Layer:
- All the routes (located in src/routes) are entry points that dispatch requests to the appropriate controllers.
- Controllers:
- Major controller (e.g., adminController, affiliateController, authController, dashboardController, linkController, referralController, etc.) that handles specific business logic for different endpoints.
- Middleware:
- Layers for authentication (authenticate and apiKeyAuth), authorization (adminAuth), input validation (validate), and error handling (errorHandler). These essentially wrap around the request-processing flow.
- Services:
- Service layer (such as userService and withdrawalService) that encapsulates business logic separate from controllers.
- Data Models:
- Data access layer, referencing models (User, ShortLink, Withdrawal, AffiliateConfig, etc.) that define the structure and relationships in the database.
- Database:
- Given the presence of a SQL query file (SQLQuery1.sql) and the connectDB.js file, include a relational database component.
- Utilities and Configurations:
- Utilities (e.g., apiProcessor, email, statsUpdater, withdrawalValidator) that assist in processing requests.
- Mention configuration files (connectDB.js, logger.js) that set up the database connection and logging.
- Logging:
- A logging component that writes to the logs directory.
- API Documentation:
- Swagger setup (swagger.js and swagger-output.json) indicating that the project supports auto-generated API documentation.
- Deployment/Environment:
- That configuration files like .env.example, package.json, and vercel.json dictate environment and deployment configurations (e.g., deployment to Vercel).

##### Relationships and Data Flow
- Have the Routes direct the request into the proper Controller.
- Controllers may call Services, which interact with the Data Models.
- Middleware intercepts requests before reaching the Controller to perform authentication, validation, or error handling.
- The Models then communicate with the Database.
- Utilities might be used by both Controllers and Services to process data (e.g., updating stats or sending emails).
- The Logging module intercepts significant events and errors at several levels.
- API Documentation (via Swagger) is generated based on the API's specifications and exposed for client developers.

##### Architectural Patterns and Design Principles
- Modularization: Each layer (routes, controllers, services, models) is clearly segregated.
- Separation of Concerns: By isolating middleware and utility functions, the design ensures that each component is responsible for a distinct function.
- RESTful API principles: Indicated by the broad set of routes and controllers that handle common CRUD operations.
- Configuration Management: The presence of configuration files ensures that infrastructure (database, logging, deployment) is kept separate from business logic.

##### Specific Notes for This Project
- Logging system distinctly, as it captures combined and error logs separately.

## Diagram (Mermaid)

```mermaid
graph TD
  %% Client Layer
  subgraph "Client Layer"
    CUI("Client/User Interface"):::client
  end

  %% API Gateway / Routing Layer
  subgraph "API Gateway"
    RT("API Gateway / Routes"):::route
  end

  %% Middleware and Controllers
  subgraph "Middleware & Controllers"
    MW("Middleware"):::middleware
    CT("Controllers"):::controller
  end

  %% Business Logic
  subgraph "Business Logic"
    SV("Services"):::service
  end

  %% Data Persistence
  subgraph "Data Persistence"
    DM("Data Models"):::model
    DB_CONN("DB Connection"):::database
    DB("Database"):::database
  end

  %% Utilities and Configurations
  subgraph "Utilities & Configurations"
    UT("Utilities"):::utility
    LC("Logger Config"):::log
    LD("Logging"):::log
  end

  %% API Documentation
  subgraph "API Documentation"
    API_DOC("API Documentation"):::doc
  end

  %% Deployment/Environment
  subgraph "Deployment/Environment"
    ENV("Env File"):::deploy
    DEP("Deploy Config"):::deploy
    PKG("Package Info"):::deploy
  end

  %% Relationships and Data Flow
  CUI -->|"HTTP_Request"| RT
  RT -->|"Route_Request"| MW
  MW -->|"Validation"| CT
  CT -->|"Call_Service"| SV
  SV -->|"Data_Operation"| DM
  DM -->|"DB_Query"| DB
  DB_CONN -->|"Init_DB"| DB
  CT -->|"Uses"| UT
  SV -->|"Uses"| UT
  MW -->|"Logs"| LC
  CT -->|"Logs"| LC
  SV -->|"Logs"| LC
  DM -->|"Logs"| LC
  LC -->|"Stores_Log"| LD
  RT -->|"Generates_Swagger"| API_DOC
  ENV -->|"Config_DB"| DB_CONN
  ENV -->|"Config_Logger"| LC
  DEP -->|"Deploys_To"| CUI
  PKG -->|"Provides_Info"| CUI

  %% Click Events
  click CUI "https://github.com/soramyo/shortlinkmanagement_be/tree/main/public/"
  click RT "https://github.com/soramyo/shortlinkmanagement_be/tree/main/src/routes/"
  click MW "https://github.com/soramyo/shortlinkmanagement_be/tree/main/src/middleware/"
  click CT "https://github.com/soramyo/shortlinkmanagement_be/tree/main/src/controllers/"
  click SV "https://github.com/soramyo/shortlinkmanagement_be/tree/main/src/services/"
  click DM "https://github.com/soramyo/shortlinkmanagement_be/tree/main/src/models/"
  click DB_CONN "https://github.com/soramyo/shortlinkmanagement_be/blob/main/src/config/connectDB.js"
  click UT "https://github.com/soramyo/shortlinkmanagement_be/tree/main/src/utils/"
  click LC "https://github.com/soramyo/shortlinkmanagement_be/blob/main/src/config/logger.js"
  click LD "https://github.com/soramyo/shortlinkmanagement_be/tree/main/logs/"
  click API_DOC "https://github.com/soramyo/shortlinkmanagement_be/blob/main/src/swagger.js"
  click ENV "https://github.com/soramyo/shortlinkmanagement_be/blob/main/.env.example"
  click DEP "https://github.com/soramyo/shortlinkmanagement_be/blob/main/vercel.json"
  click PKG "https://github.com/soramyo/shortlinkmanagement_be/blob/main/package.json"

  %% Styles
  classDef client fill:#FFEEAD,stroke:#333,stroke-width:2px;
  classDef route fill:#CDE7BE,stroke:#333,stroke-width:2px;
  classDef middleware fill:#FAD4D4,stroke:#333,stroke-width:2px;
  classDef controller fill:#AEDFF7,stroke:#333,stroke-width:2px;
  classDef service fill:#D3F8E2,stroke:#333,stroke-width:2px;
  classDef model fill:#FFF5BA,stroke:#333,stroke-width:2px;
  classDef database fill:#F8D7DA,stroke:#333,stroke-width:2px;
  classDef utility fill:#E2E8F0,stroke:#333,stroke-width:2px;
  classDef log fill:#ECECEC,stroke:#333,stroke-width:2px;
  classDef doc fill:#FFD8B1,stroke:#333,stroke-width:2px;
  classDef deploy fill:#D1C4E9,stroke:#333,stroke-width:2px;
```
