# xerberuz/backend_faworki architecture

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

- Interactive diagram: https://gitdiagram.com/xerberuz/backend_faworki
- Repository: https://github.com/xerberuz/backend_faworki

## Overview

##### Project Type and Purpose
- This is a backend-only API service for the “FaWorKi” freelance-work platform. It follows a classic MVC (Model-View-Controller)-style layering but without a “View” layer, since it exposes JSON endpoints consumed by a frontend or mobile client.
- Its main responsibility is to authenticate users, manage user roles (SuperAdmin, Admin, Cliente, Trabajador), and provide CRUD operations on business entities via REST endpoints.

##### File Structure Analysis
- Top-level components:
- server.js & start-production.js – application entry points (development vs. production)
- config/ – database and connection-pool setup for Prisma
- prisma/ – schema.prisma and migration history for PostgreSQL
- models/ – business‐entity ORM definitions (Certificado, Cliente, Trabajador, SuperAdmin, Usuario)
- controllers/ – per‐role and per‐resource controller logic
- routes/ – route definitions tying URL paths to controllers
- middleware/ – request validation logic
- scripts/ – setup scripts (e.g., for Render deployment)
- env.example & render.yaml – environment configuration & deployment descriptors
- Pattern: incoming HTTP → router layer → validation middleware → controller → Prisma client (models) → PostgreSQL.

##### Key Architectural Elements from README
- Express.js web server
- Prisma ORM over PostgreSQL
- Environment variables handled via dotenv
- Nodemon for hot reload in dev
- CLI scripts for Prisma client generation and DB synchronization

a. Main Components to represent:
- API Gateway / HTTP Server (Express.js + Router module)
- Middleware layer (validation, auth if added)
- Controller layer (Admin, Auth, Cliente, SuperAdmin, Trabajador controllers)
- Service / ORM layer (Prisma Client configured via config/database.js & connection-pool.js)
- PostgreSQL Database (deployed e.g. on Render or another host)
- Auxiliary Scripts & Deployment (render.yaml, db setup scripts, start-production.js)

b. Relationships & Data Flows:
- Clients (Web/Mobile) → HTTP(S) → Express.js Router
- Router → Validation Middleware → Controller
- Controller → Prisma Client API → PostgreSQL
- Prisma migrations (prisma/schema.prisma + migration SQL) update DB schema
- Scripts (e.g., setup-render-db.js) invoked at deploy time to bootstrap DB

c. Architectural Patterns & Principles:
- Layered architecture (Presentation/API → Business logic → Data Access)
- Separation of concerns (routes vs controllers vs models)
- Configuration abstraction (config folder for environment-specific DB pools)
- Infrastructure as code (prisma migrations, render.yaml)

d. Key Technologies & Frameworks:
- Node.js + Express.js (server, routing)
- Prisma ORM (models, migrations, generated client)
- PostgreSQL (relational store)
- dotenv (environment management)
- pnpm / npm scripts (build, dev, production entry points)

## Diagram (Mermaid)

```mermaid
graph TD
    %% External Clients
    Clients["Web/Mobile Clients"]:::external

    %% API Gateway / HTTP Server Layer
    subgraph "API Gateway / HTTP Server"
        direction TB
        ServerJS["server.js"]:::runtime
        ProdJS["start-production.js"]:::runtime
    end

    %% Express Router Layer
    subgraph "Router Layer"
        direction TB
        RouterIndex["routes/index.js"]:::runtime
        APIIndex["routes/api/index.js"]:::runtime
        AdminRoutes["routes/api/adminRoutes.js"]:::runtime
        AuthRoutes["routes/api/authRoutes.js"]:::runtime
        ClientRoutes["routes/api/clientRoutes.js"]:::runtime
        SuperAdminRoutes["routes/api/superAdminRoutes.js"]:::runtime
        WorkerRoutes["routes/api/workerRoutes.js"]:::runtime
        DiagnosticRoute["routes/api/diagnostic.js"]:::runtime
    end

    %% Middleware Layer
    Validation["middleware/validation.js"]:::runtime

    %% Controller Layer
    subgraph "Controllers"
        direction TB
        AuthController["controllers/Auth/authController.js"]:::runtime
        AdminController["controllers/Admin/adminController.js"]:::runtime
        ClienteController["controllers/Cliente/clienteController.js"]:::runtime
        SuperAdminController["controllers/SuperAdmin/superAdminController.js"]:::runtime
        TrabajadorController["controllers/Trabajador/trabajadorController.js"]:::runtime
    end

    %% Data Access / ORM Layer
    subgraph "ORM / Data Access"
        direction TB
        ConfigDB["config/database.js"]:::orm
        ConnectionPool["config/connection-pool.js"]:::orm
        PrismaSchema["prisma/schema.prisma"]:::orm
        Migrations["prisma/migrations/"]:::orm
        ModelsIndex["models/index.js"]:::orm
    end

    %% Business-Entity Models
    subgraph "Business-Entity Models"
        direction TB
        UsuarioModel["models/User/Usuario.js"]:::orm
        CertificadoModel["models/Certificado/Certificado.js"]:::orm
        ClienteModel["models/Cliente/Cliente.js"]:::orm
        TrabajadorModel["models/Trabajador/Trabajador.js"]:::orm
        SuperAdminModel["models/SuperAdmin/SuperAdmin.js"]:::orm
    end

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

    %% Infrastructure & Deployment
    subgraph "Infrastructure / Deployment"
        direction TB
        RenderYAML["render.yaml"]:::infra
        SetupScript["scripts/setup-render-db.js"]:::infra
        EnvExample["env.example"]:::infra
    end

    %% Environment Variables
    DOTENV["dotenv (.env)"]:::infra

    %% Flow Connections
    Clients -->|"HTTP(S) Request"| ServerJS
    Clients -->|"HTTP(S) Request"| ProdJS

    ServerJS --> RouterIndex
    ProdJS --> RouterIndex

    RouterIndex --> APIIndex
    APIIndex --> AdminRoutes
    APIIndex --> AuthRoutes
    APIIndex --> ClientRoutes
    APIIndex --> SuperAdminRoutes
    APIIndex --> WorkerRoutes
    APIIndex --> DiagnosticRoute

    AdminRoutes --> Validation
    AuthRoutes --> Validation
    ClientRoutes --> Validation
    SuperAdminRoutes --> Validation
    WorkerRoutes --> Validation
    DiagnosticRoute --> Validation

    Validation --> AuthController
    Validation --> AdminController
    Validation --> ClienteController
    Validation --> SuperAdminController
    Validation --> TrabajadorController

    AuthController --> ConfigDB
    AdminController --> ConfigDB
    ClienteController --> ConfigDB
    SuperAdminController --> ConfigDB
    TrabajadorController --> ConfigDB

    ConfigDB --> ConnectionPool
    ConnectionPool --> ModelsIndex
    ModelsIndex --> PrismaSchema
    ModelsIndex --> Migrations

    ModelsIndex --> UsuarioModel
    ModelsIndex --> CertificadoModel
    ModelsIndex --> ClienteModel
    ModelsIndex --> TrabajadorModel
    ModelsIndex --> SuperAdminModel

    ConnectionPool --> PostgreSQL
    PrismaSchema --> PostgreSQL
    Migrations --> PostgreSQL

    SetupScript --> PostgreSQL
    RenderYAML --> SetupScript

    EnvExample --> DOTENV
    DOTENV --> ServerJS
    DOTENV --> ProdJS

    %% Click Events
    click ServerJS "https://github.com/xerberuz/backend_faworki/blob/main/server.js"
    click ProdJS "https://github.com/xerberuz/backend_faworki/blob/main/start-production.js"

    click RouterIndex "https://github.com/xerberuz/backend_faworki/blob/main/routes/index.js"
    click APIIndex "https://github.com/xerberuz/backend_faworki/blob/main/routes/api/index.js"
    click AdminRoutes "https://github.com/xerberuz/backend_faworki/blob/main/routes/api/adminRoutes.js"
    click AuthRoutes "https://github.com/xerberuz/backend_faworki/blob/main/routes/api/authRoutes.js"
    click ClientRoutes "https://github.com/xerberuz/backend_faworki/blob/main/routes/api/clientRoutes.js"
    click SuperAdminRoutes "https://github.com/xerberuz/backend_faworki/blob/main/routes/api/superAdminRoutes.js"
    click WorkerRoutes "https://github.com/xerberuz/backend_faworki/blob/main/routes/api/workerRoutes.js"
    click DiagnosticRoute "https://github.com/xerberuz/backend_faworki/blob/main/routes/api/diagnostic.js"

    click Validation "https://github.com/xerberuz/backend_faworki/blob/main/middleware/validation.js"

    click AuthController "https://github.com/xerberuz/backend_faworki/blob/main/controllers/Auth/authController.js"
    click AdminController "https://github.com/xerberuz/backend_faworki/blob/main/controllers/Admin/adminController.js"
    click ClienteController "https://github.com/xerberuz/backend_faworki/blob/main/controllers/Cliente/clienteController.js"
    click SuperAdminController "https://github.com/xerberuz/backend_faworki/blob/main/controllers/SuperAdmin/superAdminController.js"
    click TrabajadorController "https://github.com/xerberuz/backend_faworki/blob/main/controllers/Trabajador/trabajadorController.js"

    click ConfigDB "https://github.com/xerberuz/backend_faworki/blob/main/config/database.js"
    click ConnectionPool "https://github.com/xerberuz/backend_faworki/blob/main/config/connection-pool.js"
    click PrismaSchema "https://github.com/xerberuz/backend_faworki/blob/main/prisma/schema.prisma"
    click Migrations "https://github.com/xerberuz/backend_faworki/tree/main/prisma/migrations/"
    click ModelsIndex "https://github.com/xerberuz/backend_faworki/blob/main/models/index.js"

    click UsuarioModel "https://github.com/xerberuz/backend_faworki/blob/main/models/User/Usuario.js"
    click CertificadoModel "https://github.com/xerberuz/backend_faworki/blob/main/models/Certificado/Certificado.js"
    click ClienteModel "https://github.com/xerberuz/backend_faworki/blob/main/models/Cliente/Cliente.js"
    click TrabajadorModel "https://github.com/xerberuz/backend_faworki/blob/main/models/Trabajador/Trabajador.js"
    click SuperAdminModel "https://github.com/xerberuz/backend_faworki/blob/main/models/SuperAdmin/SuperAdmin.js"

    click RenderYAML "https://github.com/xerberuz/backend_faworki/blob/main/render.yaml"
    click SetupScript "https://github.com/xerberuz/backend_faworki/blob/main/scripts/setup-render-db.js"
    click EnvExample "https://github.com/xerberuz/backend_faworki/blob/main/env.example"

    %% Styles
    classDef runtime fill:#cce5ff,stroke:#007bff,stroke-width:1px
    classDef orm fill:#ffe5b4,stroke:#ff8c00,stroke-width:1px
    classDef database fill:#d4edda,stroke:#28a745,stroke-width:1px
    classDef infra fill:#e2e3e5,stroke:#6c757d,stroke-width:1px
    classDef external fill:#f8d7da,stroke:#dc3545,stroke-width:1px
```
