# yilbertluna/backend_gestionsocial architecture

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

- Interactive diagram: https://gitdiagram.com/yilbertluna/backend_gestionsocial
- Repository: https://github.com/yilbertluna/backend_gestionsocial

## Overview

This project is a Node.js-based web server designed to manage social or administrative requests (“Gestion Social Web Server”). Its purpose involves handling user registration and login, managing locations (likely administrative divisions such as states, municipalities, and parishes), and processing various “requirements” or requests coming from users. The application leverages PostgreSQL (run via Docker compose) for data persistence and uses a migration system (Sequelize-based, as indicated by the .sequelizerc and migration scripts) to manage database schema changes.

##### Main Components Identification
- API Entry Point & Server: Represented by the entry point (src/index.js) and server setup (src/server/server.js). This is where incoming HTTP requests are accepted.
- Controllers: Organized under src/controllers (with subfolders for features like user management, localization, and requirements). These handle client requests, process data, and coordinate service calls.
- Services: Found in src/services, they encapsulate business logic (e.g., new register logic, password comparison, user data retrieval), acting as an intermediary between controllers and repositories.
- Repositories: Under src/repository, these handle direct database interactions (e.g., new registration, user-related operations, and location fetches).
- Middleware: Includes layers for error handling, JWT token verification, and request validation (located under src/middleware). These are crucial for authentication, authorization, and input validation.
- Libraries: Custom utility modules (like jwt handling in src/libs/jwt.js and payload creation in src/libs/createPayload.js).
- Database: Represent the PostgreSQL database (configured and brought up using Docker Compose as noted in the README and compose.yaml). Also include migration management as a separate process.
- Connection: The module in src/connection deals with establishing database connections and configurations.
- Docker & Deployment Artifacts: Dockerfile, compose.yaml, and the .env.example files signify containerization and environment configuration.

##### Relationships and Interactions
- API router (src/router/router.js) dispatching requests to various Controllers based on the endpoint.
- Each Controller then passes requests to its respective Service layer, which in turn calls Repositories to perform database operations.
- Middleware (error handling, authentication, request validation) should be shown as intercepting requests before they reach controllers and processing responses after controller processing.
- Docker-managed PostgreSQL container as a persistent storage element that interacts with the Repository layer.
- That migrations serve to update the schema in the PostgreSQL database and can be triggered on server startup.

##### Architectural Patterns & Design Principles
- This architecture employs a layered (or n-tier) design separating concerns:
- Controller layer for HTTP interactions.
- Service layer for business logic.
- Repository layer for data access.
- Use of middleware for cross-cutting concerns (auth, error handling).
- Clear separation of configuration (connection files), utilities (libs), and request schemas ensures maintainability.
- The project also leverages containerization (Docker) enabling isolation and easier deployment.

##### Technologies, Frameworks, and Tools
- Node.js as the runtime environment.
- Express or similar framework for the HTTP server (inferred from controllers, middleware, and router directories).
- Sequelize ORM for migration and database operations.
- PostgreSQL as the database.
- Docker and Docker Compose for container management and environment standardization.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Client
    Client["Client"]:::external

    %% API Layer
    subgraph "API Layer"
        AEP["API Entry Point"]:::api
        SS["Server Setup"]:::api
        AR["API Router"]:::api
    end

    %% Middleware Layer
    subgraph "Middleware Layer"
        MW1["Error Handler Middleware"]:::middleware
        MW2["Validate Body Request"]:::middleware
        MW3["Validate Error Handler"]:::middleware
        MW4["Verify Token"]:::middleware
    end

    %% Controllers Layer
    subgraph "Controllers"
        C1["Register Controller"]:::controller
        C2["User Controller"]:::controller
        C3["Localidad Controller"]:::controller
        C4["Requirements Controller"]:::controller
    end

    %% Services Layer
    subgraph "Services"
        S1["Compare Password Service"]:::service
        S2["Get Data User Service"]:::service
        S3["New Register Service"]:::service
    end

    %% Repositories Layer
    subgraph "Repositories"
        R1["New Register Repository"]:::repository
        R2["User Repository"]:::repository
        R3["Get Location Repository"]:::repository
        R4["Get Requirements Repository"]:::repository
    end

    %% Database
    DB["PostgreSQL Database"]:::database

    %% Utility Libraries
    subgraph "Utility Libraries"
        UL1["Create Payload"]:::utility
        UL2["JWT Library"]:::utility
    end

    %% Connection Setup
    CS["DB Connection Setup"]:::connection

    %% Schemas
    SCH["Request Validation Schema"]:::schema

    %% Migrations & ORM
    subgraph "Migrations & ORM"
        MO1["Sequelize Config"]:::orm
        MO2["Migration: 20250304205930-unnamed-migration.js"]:::orm
        MO3["Migration Config"]:::orm
    end

    %% Docker & Deployment Artifacts
    subgraph "Deployment Artifacts"
        DA1["Dockerfile"]:::deployment
        DA2["compose.yaml"]:::deployment
        DA3[".env.example"]:::deployment
    end

    %% Connections
    Client -->|"request"| AEP
    AEP -->|"initializes"| SS
    SS -->|"routes via"| AR

    %% API Routing through Middleware to Controllers
    AR -->|"passes"| MW1
    AR -->|"passes"| MW2
    AR -->|"passes"| MW3
    AR -->|"passes"| MW4
    MW1 -->|"forwards"| C1
    MW2 -->|"forwards"| C2
    MW3 -->|"forwards"| C3
    MW4 -->|"forwards"| C4

    %% Controllers to Services
    C1 -->|"calls"| S3
    C2 -->|"calls"| S2
    C3 -->|"calls"| S2
    C4 -->|"calls"| S2

    %% Services to Repositories and Utility Libraries
    S1 -->|"uses"| UL1
    S1 -->|"uses"| UL2
    S3 -->|"invokes"| R1
    S2 -->|"invokes"| R2
    S2 -->|"requests"| R3
    S2 -->|"requests"| R4

    %% Repositories to Database
    R1 -->|"queries"| DB
    R2 -->|"queries"| DB
    R3 -->|"queries"| DB
    R4 -->|"queries"| DB

    %% Connection Setup to Database
    CS -->|"connects to"| DB

    %% Controllers use Schemas
    C1 -->|"validates with"| SCH
    C2 -->|"validates with"| SCH
    C3 -->|"validates with"| SCH
    C4 -->|"validates with"| SCH

    %% Migrations & ORM updates Database
    MO2 -->|"updates"| DB
    MO3 -->|"configures"| DB

    %% Interconnections within Deployment Artifacts and Migrations
    MO1 --- MO2
    MO1 --- MO3
    DA1 --- DA2
    DA2 --- DA3
    SS --- DA1

    %% Styles
    classDef external fill:#F9E79F,stroke:#333,stroke-width:2px;
    classDef api fill:#AED6F1,stroke:#333,stroke-width:2px;
    classDef controller fill:#D2B4DE,stroke:#333,stroke-width:2px;
    classDef service fill:#A9DFBF,stroke:#333,stroke-width:2px;
    classDef repository fill:#FAD7A0,stroke:#333,stroke-width:2px;
    classDef middleware fill:#F5B7B1,stroke:#333,stroke-width:2px;
    classDef utility fill:#ABEBC6,stroke:#333,stroke-width:2px;
    classDef connection fill:#F9E79F,stroke:#333,stroke-width:2px;
    classDef schema fill:#FDEBD0,stroke:#333,stroke-width:2px;
    classDef orm fill:#D6EAF8,stroke:#333,stroke-width:2px;
    classDef deployment fill:#F5CBA7,stroke:#333,stroke-width:2px;
    classDef database fill:#FCF3CF,stroke:#333,stroke-width:2px;

    %% Click Events
    click AEP "https://github.com/yilbertluna/backend_gestionsocial/blob/master/src/index.js"
    click SS "https://github.com/yilbertluna/backend_gestionsocial/blob/master/src/server/server.js"
    click AR "https://github.com/yilbertluna/backend_gestionsocial/blob/master/src/router/router.js"

    click C1 "https://github.com/yilbertluna/backend_gestionsocial/tree/master/src/controllers/controllerRegister"
    click C2 "https://github.com/yilbertluna/backend_gestionsocial/tree/master/src/controllers/controllerUser"
    click C3 "https://github.com/yilbertluna/backend_gestionsocial/tree/master/src/controllers/localidad"
    click C4 "https://github.com/yilbertluna/backend_gestionsocial/tree/master/src/controllers/requeriments"

    click S1 "https://github.com/yilbertluna/backend_gestionsocial/blob/master/src/services/comparePassword.middeware.js"
    click S2 "https://github.com/yilbertluna/backend_gestionsocial/blob/master/src/services/getDataUser.services.js"
    click S3 "https://github.com/yilbertluna/backend_gestionsocial/blob/master/src/services/newRegister.services.js"

    click R1 "https://github.com/yilbertluna/backend_gestionsocial/blob/master/src/repository/newRegisterRepository.js"
    click R2 "https://github.com/yilbertluna/backend_gestionsocial/blob/master/src/repository/userRepository.js"
    click R3 "https://github.com/yilbertluna/backend_gestionsocial/blob/master/src/repository/getLocation.Repository.js"
    click R4 "https://github.com/yilbertluna/backend_gestionsocial/blob/master/src/repository/getRqueriments.Repository.js"

    click MW1 "https://github.com/yilbertluna/backend_gestionsocial/blob/master/src/middleware/errorHandler.middleware.js"
    click MW2 "https://github.com/yilbertluna/backend_gestionsocial/blob/master/src/middleware/validateBodyRequest.js"
    click MW3 "https://github.com/yilbertluna/backend_gestionsocial/blob/master/src/middleware/validateErrorHanlder.middleware.js"
    click MW4 "https://github.com/yilbertluna/backend_gestionsocial/blob/master/src/middleware/verifyToken.middleware.js"

    click UL1 "https://github.com/yilbertluna/backend_gestionsocial/blob/master/src/libs/createPayload.js"
    click UL2 "https://github.com/yilbertluna/backend_gestionsocial/blob/master/src/libs/jwt.js"

    click CS "https://github.com/yilbertluna/backend_gestionsocial/blob/master/src/connection/connection.js"

    click SCH "https://github.com/yilbertluna/backend_gestionsocial/blob/master/src/schemas/validateSchema.js"

    click MO1 "https://github.com/yilbertluna/backend_gestionsocial/blob/master/.sequelizerc"
    click MO2 "https://github.com/yilbertluna/backend_gestionsocial/blob/master/migrations/20250304205930-unnamed-migration.js"
    click MO3 "https://github.com/yilbertluna/backend_gestionsocial/blob/master/migrations/config/config.js"

    click DA1 "https://github.com/yilbertluna/backend_gestionsocial/tree/master/Dockerfile"
    click DA2 "https://github.com/yilbertluna/backend_gestionsocial/blob/master/compose.yaml"
    click DA3 "https://github.com/yilbertluna/backend_gestionsocial/blob/master/.env.example"
```
