# wiliam-l/tiendaonline-backend architecture

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

- Interactive diagram: https://gitdiagram.com/wiliam-l/tiendaonline-backend
- Repository: https://github.com/wiliam-l/tiendaonline-backend

## Overview

##### Project type and purpose
- This is a monolithic RESTful backend for an online store (“TiendaOnline Backend”) built with Node.js and Express.
- It exposes API endpoints for authentication, user management, roles, products, categories, states and orders, persisting data in a SQL Server database via stored procedures.

##### File structure
- app/config: database connection, environment config
- app/routers: defines Express routes (one per resource)
- app/middleware: authentication (JWT), error handling
- app/controller: map HTTP request → business logic
- app/services: core operations, orchestrating models and utilities
- app/model: ORM-like representations of tables (category, product, order, user, etc.)
- app/utils: common helpers (file upload, password hashing, token creation, query/proc wrappers)
- index.js / .env_example / package.json at the root: entry point, env setup, dependencies

##### README for additional insights
- SQL script to create DB/schema and stored procedures
- Startup flow: on dev start, auto-create roles, states and an administrator operator
- API endpoints grouped by resource (/auth, /user, /api/order, /api/product, etc.)

##### Main components and their relationships
a. External Clients (Web, Mobile, Admin UI, Postman)
b. Load Balancer or reverse proxy (optional)
c. Node.js / Express API Server
- Configuration module
- Router layer
- Middleware layer (auth, error handling)
- Controller layer
- Service layer
- Data Access layer (models + stored-procedure wrappers)
- Utility modules (JWT, bcrypt, multer, file utils)
d. SQL Server Database (tables + stored procedures)

Data flow:
Client → HTTP(S) → Express Routers
└─> Middleware (JWT validation, error catching)
└─> Controllers (validate request, call service)
└─> Services (business rules, orchestrate utils)
└─> Data Access (invoke stored procedures via mssql)
└─> SQL Server DB
And reverse on the response path.

##### Architectural patterns and design principles
- Layered (Separation of Concerns): routers → middleware → controllers → services → data access
- Dependency inversion: controllers depend on abstracted services, not raw DB calls
- Single Responsibility: each directory/module has a distinct purpose
- Stateless authentication with JWTs
- File upload handling via multer utility

- Component labels (e.g., “ProductService”, “AuthMiddleware”)
- Call out important interactions:
- JWT token issuance/validation in auth middleware and utils
- File upload path via multer in utils
- Stored procedure calls in data access layer

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Clients
    subgraph "External Clients"
        direction TB
        Web(("Web Client")):::client
        Mobile(("Mobile Client")):::client
        Admin(("Admin UI")):::client
        Postman(("Postman")):::client
    end

    %% API Server
    subgraph "TiendaOnline Backend (API Server)"
        direction TB

        %% Configuration Layer
        subgraph "Config (app/config)"
            direction TB
            DBConn["db.js"]:::config
            TestConn["testConnection.js"]:::config
            CfgTxt["config.txt"]:::config
        end

        %% Router Layer
        subgraph "Routers (app/routers)"
            direction TB
            AuthR["authRoutes.js"]:::router
            UserR["userRoutes.js"]:::router
            RolR["rolRoutes.js"]:::router
            CatR["categoryRouter.js"]:::router
            ProdR["productRoutes.js"]:::router
            StateR["stateRoutes.js"]:::router
            OrderR["orderRouter.js"]:::router
        end

        %% Middleware Layer
        subgraph "Middleware (app/middleware)"
            direction TB
            AuthMw["auth.js"]:::middleware
            ErrMw["errorHandle.js"]:::middleware
        end

        %% Controller Layer
        subgraph "Controllers (app/controller)"
            direction TB
            LoginC["loginController.js"]:::controller
            UserC["userController.js"]:::controller
            RolC["rolController.js"]:::controller
            CatProdC["categoryProductsController.js"]:::controller
            ProdC["productsController.js"]:::controller
            StateC["stateController.js"]:::controller
            OrdenC["ordenController.js"]:::controller
            OpC["operatorController.js"]:::controller
            AutoC["autoCreationController.js"]:::controller
        end

        %% Service Layer
        subgraph "Services (app/services)"
            direction TB
            UserS["userService.js"]:::service
            RolS["rolService.js"]:::service
            CatS["categoryService.js"]:::service
            ProdS["productsService.js"]:::service
            StateS["stateService.js"]:::service
            OrdenS["ordenService.js"]:::service
            CustS["customerService.js"]:::service
            OpS["operatorService.js"]:::service
        end

        %% Data Access Layer
        subgraph "Data Access / Models (app/model)"
            direction TB
            UserM["user.js"]:::data
            RolM["rol.js"]:::data
            CatM["category.js"]:::data
            ProdM["product.js"]:::data
            StateM["states.js"]:::data
            OrderM["order.js"]:::data
            CustM["customer.js"]:::data
        end

        %% Utility Layer
        subgraph "Utils (app/utils)"
            direction TB
            TokenU["token.js"]:::util
            PassU["passwordUtils.js"]:::util
            MulterU["multer.js"]:::util
            FileU["fileUtils.js"]:::util
            QueryU["queryProcedureUtils.js"]:::util
            RoleU["role.js"]:::util
        end

    end

    %% Database
    SQLServer[(SQL Server DB)]:::db

    %% Data Flow
    Web -->|"HTTP(S)"| AuthR
    Mobile -->|"HTTP(S)"| AuthR
    Admin -->|"HTTP(S)"| AuthR
    Postman -->|"HTTP(S)"| AuthR

    AuthR -->|"next() / route()"| AuthMw
    AuthMw -->|"calls"| LoginC
    LoginC -->|"calls"| UserS
    UserS -->|"uses"| QueryU
    QueryU -->|"maps to"| UserM
    UserM -->|"mssql driver"| SQLServer

    SQLServer -->|"results"| UserM
    UserM -->|"returns"| QueryU
    QueryU -->|"returns"| UserS
    UserS -->|"returns"| LoginC
    LoginC -->|"res.json()"| AuthMw
    AuthMw -->|"res.end()"| AuthR

    %% Click Events
    click DBConn "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/config/db.js"
    click TestConn "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/config/testConnection.js"
    click CfgTxt "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/config/config.txt"
    click AuthR "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/routers/authRoutes.js"
    click UserR "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/routers/userRoutes.js"
    click RolR "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/routers/rolRoutes.js"
    click CatR "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/routers/categoryRouter.js"
    click ProdR "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/routers/productRoutes.js"
    click StateR "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/routers/stateRoutes.js"
    click OrderR "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/routers/orderRouter.js"
    click AuthMw "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/middleware/auth.js"
    click ErrMw "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/middleware/errorHandle.js"
    click LoginC "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/controller/loginController.js"
    click UserC "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/controller/userController.js"
    click RolC "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/controller/rolController.js"
    click CatProdC "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/controller/categoryProductsController.js"
    click ProdC "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/controller/productsController.js"
    click StateC "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/controller/stateController.js"
    click OrdenC "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/controller/ordenController.js"
    click OpC "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/controller/operatorController.js"
    click AutoC "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/controller/autoCreationController.js"
    click UserS "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/services/userService.js"
    click RolS "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/services/rolService.js"
    click CatS "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/services/categoryService.js"
    click ProdS "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/services/productsService.js"
    click StateS "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/services/stateService.js"
    click OrdenS "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/services/ordenService.js"
    click CustS "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/services/customerService.js"
    click OpS "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/services/operatorService.js"
    click UserM "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/model/user.js"
    click RolM "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/model/rol.js"
    click CatM "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/model/category.js"
    click ProdM "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/model/product.js"
    click StateM "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/model/states.js"
    click OrderM "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/model/order.js"
    click CustM "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/model/customer.js"
    click TokenU "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/utils/token.js"
    click PassU "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/utils/passwordUtils.js"
    click MulterU "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/utils/multer.js"
    click FileU "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/utils/fileUtils.js"
    click QueryU "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/utils/queryProcedureUtils.js"
    click RoleU "https://github.com/wiliam-l/tiendaonline-backend/blob/main/app/utils/role.js"

    %% Styles
    classDef client fill:#ADD8E6,stroke:#333,stroke-width:1px;
    classDef config fill:#D3D3D3,stroke:#333,stroke-width:1px;
    classDef router fill:#87CEFA,stroke:#333,stroke-width:1px;
    classDef middleware fill:#FFC0CB,stroke:#333,stroke-width:1px;
    classDef controller fill:#008080,stroke:#333,stroke-width:1px;
    classDef service fill:#90EE90,stroke:#333,stroke-width:1px;
    classDef data fill:#FFFFE0,stroke:#333,stroke-width:1px;
    classDef util fill:#FFFFFF,stroke:#333,stroke-width:1px;
    classDef db fill:#FFA500,stroke:#333,stroke-width:1px;
```
