# guille-nat/api_compras architecture

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

- Interactive diagram: https://gitdiagram.com/guille-nat/api_compras
- Repository: https://github.com/guille-nat/api_compras

## Overview

##### Project Type & Purpose
- This is a Django-based REST API backend for a purchasing system (compras) that handles products/inventory, purchases, payments (including installment/cuota logic), and user management.
- The README emphasizes key business rules (stock validation, installment calculations, discounts/recargos, and automated email notifications) and critical technical details like JWT authentication.

##### File Structure
- The root-level files include standard Django project files (manage.py, SistemaCompras as the main project folder with settings, URLs, and WSGI/ASGI configurations).
- The “api” folder holds application-specific logic. Within “api” you have submodules for:
- “payments” (contains models, serializers, views)
- “products” (models, serializers, tests, views)
- “purchases” (models, serializers, views)
- “users” (models, serializers, views)
- There is a “management/commands” folder for custom Django commands (e.g., actualizar_cuotas and prueba_email), indicating scheduled tasks or administrative operations.
- A separate “migrations” folder exists at the root to consolidate migration files, which reflects a decision to centralize schema changes.

##### README for More Insights
- The README details the API’s capabilities, endpoints (with links to Swagger and Redoc documentation), and how JWT is used for authentication via Bearer tokens.
- It also highlights business rules such as validation of stock, application of discounts or recargos on payments, and dynamic calculation of installment due dates.
- There is a clear emphasis on automated tasks (cron jobs) for updating installments and sending HTML email notifications, which are unique architectural aspects.

a. Client/Consumer:
- External clients, mobile apps, or web frontends making REST API calls.
- A block labeled “Client Application” (or multiple consumers) connecting to the system.
b. API Gateway / Security Layer:
- JWT Authentication mechanism (a block showing the “/api/token/” endpoint for token issuance).
c. Core Django Application Server:
- Main Django project (SistemaCompras) as the backbone.
- Users Module – managing user registration and login (with modifications for storing lowercase fields and custom user logic).
- Products Module – handling inventory control and product listings.
- Purchases Module – handling order creation, validating stock, applying business rules for purchase processing.
- Payments Module – managing installments, applying discounts and surcharges for early/late payments.
d. Database:
e. External Email Service:
- SMTP service that handles email notifications (using Django’s email backend and the email configuration from the README).
f. Scheduled/Management Commands:
- A “Scheduled Task Executor” (or “Cron Job Scheduler”) that triggers management commands (actualizar_cuotas, prueba_email) to ensure periodic operations.

##### Architectural Patterns & Design Principles
- Separation of concerns:
- Different “api” submodules for different business domains.
- Clear separation between authentication (JWT) and business logic.
- RESTful nature of the design:
- Clearly labeled API endpoints.
- Use of automation (management commands) and modular design (application division in apps like payments, products, purchases, users).

## Diagram (Mermaid)

```mermaid
graph TD
    %% Global Components
    CA["Client Application"]:::client
    JWT["JWT Authentication(/api/token/)"]:::auth
    DAPI["Django API (SistemaCompras)"]:::api
    DB["Database(PostgreSQL)"]:::db
    SCHED["Scheduled Tasks\n(Management Commands)"]:::scheduled
    EMAIL["Email Service(SMTP Server)"]:::external

    %% Connections from external client to API and JWT
    CA -->|"REST_call"| JWT
    CA -->|"REST_call"| DAPI
    JWT -->|"validateToken"| DAPI

    %% Django API Submodules
    subgraph "Django API (SistemaCompras)"
        USERS["Users Module"]:::module
        PRODUCTS["Products Module"]:::module
        PURCHASES["Purchases Module"]:::module
        PAYMENTS["Payments Module"]:::module
    end

    %% Inter-module communication inside Django API
    PURCHASES -->|"checkStock"| PRODUCTS
    PURCHASES -->|"processPayment"| PAYMENTS

    %% API to Database connection
    DAPI -->|"dataPersistence"| DB

    %% Scheduled tasks triggering payments update and email notifications
    SCHED -->|"update_installments"| PAYMENTS
    SCHED -->|"send_notification"| EMAIL

    %% Styles
    classDef client fill:#FFA07A,stroke:#333,stroke-width:2px;
    classDef auth fill:#87CEFA,stroke:#333,stroke-width:2px;
    classDef api fill:#90EE90,stroke:#333,stroke-width:2px;
    classDef module fill:#ADFF2F,stroke:#333,stroke-width:2px;
    classDef db fill:#FFDEAD,stroke:#333,stroke-width:2px;
    classDef scheduled fill:#FFD700,stroke:#333,stroke-width:2px;
    classDef external fill:#DDA0DD,stroke:#333,stroke-width:2px;

    %% Click Events for Project Components
    click DAPI "https://github.com/guille-nat/api_compras/tree/main/SistemaCompras"
    click PAYMENTS "https://github.com/guille-nat/api_compras/tree/main/api/payments"
    click PRODUCTS "https://github.com/guille-nat/api_compras/tree/main/api/products"
    click PURCHASES "https://github.com/guille-nat/api_compras/tree/main/api/purchases"
    click USERS "https://github.com/guille-nat/api_compras/tree/main/api/users"
    click SCHED "https://github.com/guille-nat/api_compras/tree/main/api/management/commands"
```
