# josherxd/gestion-compras-inventario architecture

> Architecture diagram of the GitHub repository josherxd/gestion-compras-inventario, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/josherxd/gestion-compras-inventario
- Repository: https://github.com/josherxd/gestion-compras-inventario

## Overview

##### Project type and purpose
- This is a full-stack “Gestión Compras e Inventario” application.
- Frontend: React single-page application (SPA) for managing products and orders.
- Backend: Node.js + TypeScript + Express REST API, backed by MongoDB, following a clean-architecture layering (controllers → use cases → domain services → repositories).

##### File structure
- Top-level directories:
- backend/
– src/ (TypeScript source)
- application/use_case (business use-cases)
- domain/models, interfaces, services (core entities, domain logic)
- infrastructure (Express controllers, routes, repository implementations, config)
– dist/ (compiled JS)
– test/ (Jest tests for each layer)
– schema.sql (DB schema reference)
– package.json, tsconfig.json, .env
- frontend/
– public/ (static HTML, manifest)
– src/ (React code)
- views (UI pages)
- context (React Context API)
- services/api.js (HTTP client)
- styles

##### README for additional insights
- Stack: React + CSS on frontend; Node.js, TypeScript, Express, Mongoose on backend; Jest for tests.
- Clean-architecture layering explicitly described.
- Key API endpoints for products, orders, purchase orders.

##### Main components and their interactions
a. Frontend (React SPA)
- UI Views (RegistrarProducto, RegistrarOrdenDeCompra, Home)
- AppContext (global state)
- api.js (fetch wrapper calling /api endpoints)
- Runs on http://localhost:3000, served by npm start

b. Backend (Express REST API)
- HTTP layer: Express server (index.ts) → routing modules (/api/productos, /api/ordenes, /api/purchaseorders)
- Controller layer: order.controller, product.controller, purchaseorder routes
- Application layer: use cases (order.usecase.ts, product.usecase.ts) orchestrating domain logic
- Domain layer:
- Models (Order, Product, Price, Quantity, etc.)
- Interfaces (OrderRepository, ProductRepository, PdfService)
- Services (business rules, e.g. order.service.ts)
- Infrastructure layer:
- Mongoose repository implementations (order.repository.service.ts, product.repository.service.ts)
- Database configuration (MongoDB connection via mongoose)
- Error handling and configuration (common/config/statusCode.ts, envs.ts)
- Tests: exercise each layer with Jest

c. Database
- MongoDB (hosted locally or in the cloud)
- Collections: products, orders, purchaseorders
- schema.sql for reference

d. External services
- Placeholder PdfService (interface in domain, implemented or stubbed in infrastructure if used)

##### Architectural patterns and design principles
- Clean (hexagonal) architecture: clear separation of layers, dependency inversion via interfaces
- RESTful API design, resource-oriented routing
- React Context for state management
- Mongoose ODM for data persistence

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Frontend (React SPA)"
        direction TB
        Browser["Browser"]:::external
        Index["frontend/src/index.js"]:::frontend
        App["App.js"]:::frontend
        Context["AppContext"]:::frontend
        APIClient["api.js"]:::frontend
        subgraph Views
            direction TB
            Home["Home.js"]:::frontend
            RegistrarOrden["RegistrarOrdenDeCompra.js"]:::frontend
            RegistrarProducto["RegistrarProducto.js"]:::frontend
        end
        Styles["styles"]:::frontend
        Browser -->|Loads SPA| Index
        Index --> App
        App --> Context
        App --> Views
        App --> APIClient
        Views --> APIClient
        App --> Styles
    end

    subgraph "Backend (Express API)"
        direction TB
        Server["index.ts"]:::backend
        ConfigEnv["envs.ts"]:::backend
        StatusCode["statusCode.ts"]:::backend

        subgraph Routing
            direction TB
            ProductRoutes["product.routes.ts"]:::backend
            OrderRoutes["order.routes.ts"]:::backend
            PurchaseRoutes["purchaseorder.routes.ts"]:::backend
        end

        subgraph Controllers
            direction TB
            ProductController["product.controller.ts"]:::backend
            OrderController["order.controller.ts"]:::backend
        end

        subgraph "Application Use Cases"
            direction TB
            ProductUseCase["product.usecase.ts"]:::backend
            OrderUseCase["order.usecase.ts"]:::backend
        end

        subgraph Domain
            direction TB
            Models["models"]:::backend
            Interfaces["interfaces"]:::backend
            Services["services"]:::backend
        end

        subgraph Infrastructure
            direction TB
            DBConfig["database.ts"]:::backend
            ProductRepoImpl["product.repository.service.ts"]:::backend
            OrderRepoImpl["order.repository.service.ts"]:::backend
            PurchaseRepoImpl["purchaseorder.repository.service.ts"]:::backend
        end

        Server --> ConfigEnv
        Server --> StatusCode
        Server --> Routing
        Routing --> ProductRoutes
        Routing --> OrderRoutes
        Routing --> PurchaseRoutes
        ProductRoutes --> ProductController
        OrderRoutes --> OrderController
        PurchaseRoutes -->|calls| OrderController
        ProductController --> ProductUseCase
        OrderController --> OrderUseCase
        ProductUseCase --> Services
        OrderUseCase --> Services
        Services --> Interfaces
        Interfaces --> ProductRepoImpl
        Interfaces --> OrderRepoImpl
        Interfaces --> PurchaseRepoImpl
        ProductRepoImpl --> DBConfig
        OrderRepoImpl --> DBConfig
        PurchaseRepoImpl --> DBConfig
    end

    DB["MongoDB"]:::database
    DBConfig --> DB
    APIClient -->|HTTP /api/*| Server

    subgraph "External Services"
        direction TB
        PDFService["PDF Service"]:::external
    end
    Services --> PDFService

    classDef frontend fill:#ccf,stroke:#333,stroke-width:1px;
    classDef backend fill:#cfc,stroke:#333,stroke-width:1px;
    classDef database fill:#fcf,stroke:#333,stroke-width:1px;
    classDef external fill:#eee,stroke:#333,stroke-width:1px;

    click Index "https://github.com/josherxd/gestion-compras-inventario/blob/develop/frontend/src/index.js"
    click App "https://github.com/josherxd/gestion-compras-inventario/blob/develop/frontend/src/App.js"
    click Context "https://github.com/josherxd/gestion-compras-inventario/blob/develop/frontend/src/context/AppContext.js"
    click APIClient "https://github.com/josherxd/gestion-compras-inventario/blob/develop/frontend/src/services/api.js"
    click Home "https://github.com/josherxd/gestion-compras-inventario/blob/develop/frontend/src/views/Home.js"
    click RegistrarOrden "https://github.com/josherxd/gestion-compras-inventario/blob/develop/frontend/src/views/RegistrarOrdenDeCompra.js"
    click RegistrarProducto "https://github.com/josherxd/gestion-compras-inventario/blob/develop/frontend/src/views/RegistrarProducto.js"
    click Styles "https://github.com/josherxd/gestion-compras-inventario/tree/develop/frontend/src/styles"
    click Server "https://github.com/josherxd/gestion-compras-inventario/blob/develop/backend/src/index.ts"
    click ConfigEnv "https://github.com/josherxd/gestion-compras-inventario/blob/develop/backend/src/common/config/envs.ts"
    click StatusCode "https://github.com/josherxd/gestion-compras-inventario/blob/develop/backend/src/common/config/statusCode.ts"
    click ProductUseCase "https://github.com/josherxd/gestion-compras-inventario/blob/develop/backend/src/application/use_case/product.usecase.ts"
    click OrderUseCase "https://github.com/josherxd/gestion-compras-inventario/blob/develop/backend/src/application/use_case/order.usecase.ts"
    click Models "https://github.com/josherxd/gestion-compras-inventario/tree/develop/backend/src/domain/models"
    click Interfaces "https://github.com/josherxd/gestion-compras-inventario/tree/develop/backend/src/domain/interfaces"
    click Services "https://github.com/josherxd/gestion-compras-inventario/tree/develop/backend/src/domain/services"
    click DBConfig "https://github.com/josherxd/gestion-compras-inventario/blob/develop/backend/src/infrastructure/config/database.ts"
    click ProductController "https://github.com/josherxd/gestion-compras-inventario/blob/develop/backend/src/infrastructure/controller/product.controller.ts"
    click OrderController "https://github.com/josherxd/gestion-compras-inventario/blob/develop/backend/src/infrastructure/controller/order.controller.ts"
    click ProductRoutes "https://github.com/josherxd/gestion-compras-inventario/blob/develop/backend/src/infrastructure/routes/product.routes.ts"
    click OrderRoutes "https://github.com/josherxd/gestion-compras-inventario/blob/develop/backend/src/infrastructure/routes/order.routes.ts"
    click PurchaseRoutes "https://github.com/josherxd/gestion-compras-inventario/blob/develop/backend/src/infrastructure/routes/purchaseorder.routes.ts"
    click ProductRepoImpl "https://github.com/josherxd/gestion-compras-inventario/blob/develop/backend/src/infrastructure/services/product.repository.service.ts"
    click OrderRepoImpl "https://github.com/josherxd/gestion-compras-inventario/blob/develop/backend/src/infrastructure/services/order.repository.service.ts"
    click PurchaseRepoImpl "https://github.com/josherxd/gestion-compras-inventario/blob/develop/backend/src/infrastructure/services/purchaseorder.repository.service.ts"
```
