# raulzamoraperez/backend_bv architecture

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

- Interactive diagram: https://gitdiagram.com/raulzamoraperez/backend_bv
- Repository: https://github.com/raulzamoraperez/backend_bv

## Overview

##### Project Type and Purpose
- This is a Node.js/TypeScript–based backend REST API for a “Biblioteca Virtual UDEA” (virtual library) system.
- It follows an MVC‐style, layered architecture, using Express for HTTP handling and Sequelize (MySQL) for persistence.

##### High-Level Architectural Layers
a. Client Layer
- External consumer (e.g., front-end SPA, mobile app, Postman) issues HTTP requests to the API.
b. Presentation Layer (Express Server)
- src/server.ts / src/app.ts bootstrap the Express application.
- Middleware stack (CORS, body parsing, validation, file upload with Multer, JWT authentication, error handling) processes each incoming request.
c. Routing Layer
- src/routes defines routers for each domain (Administrador, Alumno, Book, Area, Materia, Matricula, Semestre, AuthAlumno).
- Requests are dispatched to the appropriate Controller based on HTTP method + URL.
d. Controller Layer
- src/controllers/*Controller.ts implement endpoint handlers, performing request→DTO mapping, basic input checks, and orchestrating calls to services or ORM models.
e. Service / Business Logic Layer (optional in some controllers)
- For “Administrador”, there’s a dedicated Service class (Administrador.Service.ts). Other controllers can be refactored similarly.
f. Data Access Layer
- src/data/mysql/udea.database.ts sets up a Sequelize connection to the MySQL database.
- src/data/mysql/models/*.ts define Sequelize models (Administrador, Alumno, Area, Libro, Materia, Matricula, Semestre).
- Migrations (migrations/*.js) manage schema evolution via sequelize-cli.
g. Utilities & Helpers
- src/config holds environment loading (envs.ts), CORS setup, JWT adapter, Bcrypt adapter.
- src/helpers handles email notifications and token generation.
- src/errors and src/middleware provide structured error and exception handling.

##### Database & Persistence
- MySQL instance named “biblioteca_virtual_udea.”
- Sequelize ORM interfaces with tables corresponding to domain models.
- Migration scripts (migrations folder) create/alter tables on deploy or schema change.

##### External Dependencies & Tools
- JWT for authentication tokens.
- Bcrypt for password hashing.
- Multer for file (book/upload, cover) uploads.
- dependency-check-bin folder contains OWASP dependency-check CLI (for local security scans), not part of request flow but part of CI.

##### Diagramming the System
- Clients (Browser/Mobile/API Consumer)
- Express HTTP Server
- Middleware Layer (CORS, Validation, Auth, Upload, Error Handler)
- Controllers
- Services (if any)
- Sequelize ORM / Models
- MySQL Database
- Helpers (Email Service, Token Generator)
- Configuration Module (env loader, JWT adapter, Bcrypt adapter)
- Migration Tool (sequelize-cli) & dependency-check (side tool)

##### Interactions & Data Flow
1) Client → HTTP Server: Incoming request
2) HTTP Server → Middleware: CORS → Body Parser → Validation → File Upload → JWT Auth → Error Handler
3) Middleware → Router: Route lookup (e.g., /books, /auth/admin)
4) Router → Controller: Controller.method(req, res)
5) Controller → Service or directly to Model:
- Service calls Helpers (e.g., token generation, email)
- Model (via Sequelize) executes SQL queries
6) Model → Database: SQL SELECT/INSERT/UPDATE/DELETE
7) Database → Model → Controller → HTTP Server → Client: Response payload

##### Architectural Patterns & Principles
- Layered architecture (presentation, routing, controllers, services, data).
- Separation of concerns: each folder (config, controllers, data, helpers, middleware) has a clear responsibility.
- Use of DTOs (src/controllers/…/dtos) to validate and type-safely map incoming payloads.
- Configuration and adapters abstract environment variables, JWT, Bcrypt.
- Migrations ensure schema versioning and reproducibility.

##### Final Advice
- End-to-end path of a request through middleware, routing, business logic, and persistence.
- Don’t forget auxiliary flows: file upload via Multer, email sending via helper, token generation via JWT adapter.
- Important interactions (e.g., “Generates JWT token,” “Validates input DTO,” “Executes SQL query”).

## Diagram (Mermaid)

```mermaid
flowchart TD
    Client["API Consumer (Browser/Mobile/Postman)"]:::runtime

    subgraph "Express Server"
        Server["Express Server bootstrap"]:::runtime
    end

    Client --> Server

    subgraph "Middleware Layer"
        CORS["CORS Middleware"]:::runtime
        Body["Body Parser & JSON"]:::runtime
        Validation["Validation Middleware"]:::runtime
        Upload["File Upload Middleware"]:::runtime
        Auth["JWT Auth Middleware"]:::runtime
        ErrorH["Error Handler"]:::runtime
    end

    Server --> CORS --> Body --> Validation --> Upload --> Auth --> ErrorH

    subgraph "Routing Layer"
        AdminR["Administrador Router"]:::runtime
        AlumnoR["AlumnoUser Router"]:::runtime
        AreaR["Area Router"]:::runtime
        BookR["Book Router"]:::runtime
        MateriaR["Materia Router"]:::runtime
        MatriculaR["Matricula Router"]:::runtime
        SemestreR["Semestre Router"]:::runtime
        AuthAlumnoR["AuthAlumno Router"]:::runtime
    end

    ErrorH --> AdminR
    ErrorH --> AlumnoR
    ErrorH --> AreaR
    ErrorH --> BookR
    ErrorH --> MateriaR
    ErrorH --> MatriculaR
    ErrorH --> SemestreR
    ErrorH --> AuthAlumnoR

    subgraph "Controller Layer"
        AdminC["AdministradorController"]:::runtime
        AlumnoC["AlumnoController"]:::runtime
        AreaC["AreaController"]:::runtime
        AuthAlumnoC["AuthAlumnController"]:::runtime
        BookC["Book Controllers"]:::runtime
        MateriaC["MateriaController"]:::runtime
        MatriculaC["MatriculaController"]:::runtime
        SemestreC["SemestreController"]:::runtime
    end

    AdminR --> AdminC
    AlumnoR --> AlumnoC
    AreaR --> AreaC
    AuthAlumnoR --> AuthAlumnoC
    BookR --> BookC
    MateriaR --> MateriaC
    MatriculaR --> MatriculaC
    SemestreR --> SemestreC

    subgraph "Service Layer"
        AdminS["AdministradorService"]:::runtime
    end

    AdminC --> AdminS

    subgraph "Data Access Layer"
        DBConn["Sequelize Connection"]:::data
        subgraph Models
            AdmM["Administrador Model"]:::data
            AlumM["Alumno Model"]:::data
            AreaM["Area Model"]:::data
            LibroM["Libro Model"]:::data
            MateriaM2["Materia Model"]:::data
            MatriculaM["Matricula Model"]:::data
            SemestreM["Semestre Model"]:::data
        end
        DB["MySQL Database"]:::data
    end

    AdminC --> DBConn
    AlumnoC --> DBConn
    AreaC --> DBConn
    AuthAlumnoC --> DBConn
    BookC --> DBConn
    MateriaC --> DBConn
    MatriculaC --> DBConn
    SemestreC --> DBConn

    DBConn --> AdmM
    DBConn --> AlumM
    DBConn --> AreaM
    DBConn --> LibroM
    DBConn --> MateriaM2
    DBConn --> MatriculaM
    DBConn --> SemestreM
    AdmM --> DB
    AlumM --> DB
    AreaM --> DB
    LibroM --> DB
    MateriaM2 --> DB
    MatriculaM --> DB
    SemestreM --> DB

    subgraph "Configuration & Helpers"
        Config["Config Module"]:::config
        EmailH["Email Notifications Helper"]:::config
        TokenH["Token Generation Helper"]:::config
    end

    Config --> Server
    Config --> DBConn
    AdminS --> EmailH
    AuthAlumnoC --> TokenH

    subgraph "CI/Security Tools"
        Migrations["Migrations"]:::tools
        DepCheck["OWASP Dep-Check CLI"]:::tools
    end

    classDef runtime fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef data fill:#90EE90,stroke:#333,stroke-width:1px
    classDef config fill:#FFA500,stroke:#333,stroke-width:1px
    classDef tools fill:#D3D3D3,stroke:#333,stroke-width:1px

    click Server "https://github.com/raulzamoraperez/backend_bv/blob/main/src/server.ts"
    click CORS "https://github.com/raulzamoraperez/backend_bv/blob/main/src/config/cors.ts"
    click Validation "https://github.com/raulzamoraperez/backend_bv/blob/main/src/middleware/validation.ts"
    click Upload "https://github.com/raulzamoraperez/backend_bv/blob/main/src/middleware/upload.ts"
    click Upload "https://github.com/raulzamoraperez/backend_bv/blob/main/src/middleware/uploadLibro.ts"
    click Upload "https://github.com/raulzamoraperez/backend_bv/blob/main/src/middleware/uploadPortada.ts"
    click Upload "https://github.com/raulzamoraperez/backend_bv/blob/main/src/middleware/handleMulterErrors.ts"
    click Auth "https://github.com/raulzamoraperez/backend_bv/blob/main/src/middleware/Admin.middleware.ts"
    click ErrorH "https://github.com/raulzamoraperez/backend_bv/blob/main/src/middleware/errorHandler.ts"
    click AdminR "https://github.com/raulzamoraperez/backend_bv/blob/main/src/routes/Administrador/AdminstradorRouter.ts"
    click AlumnoR "https://github.com/raulzamoraperez/backend_bv/blob/main/src/routes/AlumnoUser/AlumnoUserRoutes.ts"
    click AreaR "https://github.com/raulzamoraperez/backend_bv/blob/main/src/routes/Area/AreaRouter.ts"
    click BookR "https://github.com/raulzamoraperez/backend_bv/blob/main/src/routes/Book/BookRoutes.ts"
    click MateriaR "https://github.com/raulzamoraperez/backend_bv/blob/main/src/routes/Materia/MateriaRoutes.ts"
    click MatriculaR "https://github.com/raulzamoraperez/backend_bv/blob/main/src/routes/Matricula/MatriculaRouter.ts"
    click SemestreR "https://github.com/raulzamoraperez/backend_bv/blob/main/src/routes/Semestre/SemestreRouter.ts"
    click AuthAlumnoR "https://github.com/raulzamoraperez/backend_bv/blob/main/src/routes/AuthAlumno/AuthAlumnoRoutes.ts"
    click AdminC "https://github.com/raulzamoraperez/backend_bv/blob/main/src/controllers/Administrador/AdministradorController.ts"
    click AlumnoC "https://github.com/raulzamoraperez/backend_bv/blob/main/src/controllers/Alumno/AlumnoController.ts"
    click AreaC "https://github.com/raulzamoraperez/backend_bv/blob/main/src/controllers/Area/AreaController.ts"
    click AuthAlumnoC "https://github.com/raulzamoraperez/backend_bv/blob/main/src/controllers/AuthAlumn/AuthAlumnController.ts"
    click BookC "https://github.com/raulzamoraperez/backend_bv/blob/main/src/controllers/Books/createBook.ts"
    click BookC "https://github.com/raulzamoraperez/backend_bv/blob/main/src/controllers/Books/getBooks.ts"
    click BookC "https://github.com/raulzamoraperez/backend_bv/blob/main/src/controllers/Books/getBookById.ts"
    click BookC "https://github.com/raulzamoraperez/backend_bv/blob/main/src/controllers/Books/updateBook.ts"
    click BookC "https://github.com/raulzamoraperez/backend_bv/blob/main/src/controllers/Books/deleteBook.ts"
    click BookC "https://github.com/raulzamoraperez/backend_bv/blob/main/src/controllers/Books/searchBooks.ts"
    click MateriaC "https://github.com/raulzamoraperez/backend_bv/blob/main/src/controllers/Materia/MateriaController.ts"
    click MatriculaC "https://github.com/raulzamoraperez/backend_bv/blob/main/src/controllers/Matricula/MatriculaController.ts"
    click SemestreC "https://github.com/raulzamoraperez/backend_bv/blob/main/src/controllers/Semestre/SemestreController.ts"
    click AdminS "https://github.com/raulzamoraperez/backend_bv/blob/main/src/controllers/Administrador/Administrador.Service.ts"
    click DBConn "https://github.com/raulzamoraperez/backend_bv/blob/main/src/data/mysql/udea.database.ts"
    click AdmM "https://github.com/raulzamoraperez/backend_bv/blob/main/src/data/mysql/models/Administrador.ts"
    click AlumM "https://github.com/raulzamoraperez/backend_bv/blob/main/src/data/mysql/models/Alumno.ts"
    click AreaM "https://github.com/raulzamoraperez/backend_bv/blob/main/src/data/mysql/models/Area.ts"
    click LibroM "https://github.com/raulzamoraperez/backend_bv/blob/main/src/data/mysql/models/Libro.ts"
    click MateriaM2 "https://github.com/raulzamoraperez/backend_bv/blob/main/src/data/mysql/models/Materia.ts"
    click MatriculaM "https://github.com/raulzamoraperez/backend_bv/blob/main/src/data/mysql/models/Matricula.ts"
    click SemestreM "https://github.com/raulzamoraperez/backend_bv/blob/main/src/data/mysql/models/Semestre.ts"
    click Migrations "https://github.com/raulzamoraperez/backend_bv/blob/main/migrations/20250325200436-uno.js"
    click Migrations "https://github.com/raulzamoraperez/backend_bv/blob/main/migrations/20250331025513-eliminacion-campo-permisos-administrador.js"
    click Migrations "https://github.com/raulzamoraperez/backend_bv/blob/main/migrations/20250402034658-deleteRegisterDateField.js"
    click Migrations "https://github.com/raulzamoraperez/backend_bv/blob/main/migrations/20250405235436-eliminadoCampoCodigoVerificacionAdminitradores.js"
    click Migrations "https://github.com/raulzamoraperez/backend_bv/blob/main/migrations/20250407175440-eliminadoPropiedadVerificadoAdministrador.js"
    click Config "https://github.com/raulzamoraperez/backend_bv/blob/main/src/config/envs.ts"
    click Config "https://github.com/raulzamoraperez/backend_bv/blob/main/src/config/index.ts"
    click Config "https://github.com/raulzamoraperez/backend_bv/blob/main/src/config/jwt.adapte.ts"
    click Config "https://github.com/raulzamoraperez/backend_bv/blob/main/src/config/bcryp.adapter.ts"
    click EmailH "https://github.com/raulzamoraperez/backend_bv/blob/main/src/helpers/emails.ts"
    click TokenH "https://github.com/raulzamoraperez/backend_bv/blob/main/src/helpers/tokens.ts"
    click DepCheck "https://github.com/raulzamoraperez/backend_bv/tree/main/dependency-check-bin/"
```
