# ssv96/sales-admin-panel architecture

> Architecture diagram of the GitHub repository ssv96/sales-admin-panel, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ssv96/sales-admin-panel
- Repository: https://github.com/ssv96/sales-admin-panel

## Overview

##### Project Type & Purpose
- This is a backend API system built with Node.js and Nest.js.
- Its purpose is to manage wholesalers and retailers via RESTful API endpoints.
- The project follows the MVC paradigm and uses Sequelize ORM with a PostgreSQL (or MySQL) database.

##### Analyzing the File Structure
- Top-level files like nest-cli.json, package.json, tsconfig.json, and configuration files (.eslintrc.js, .prettierrc, etc.) indicate a Nest.js project setup.
- The “src” directory contains the main source code:
– “app.module.ts”, “app.controller.ts”, and “app.service.ts” form the core of the application.
– The “common” directory holds shared modules (e.g., configuration and PostgreSQL settings) used across the project.
– The “core” directory is further divided into modules for “retailer”, “wholesaler”, and “stock” with their controllers, services, DTOs, and schemas.
- The “migrations” directory contains database migration scripts, which set up tables for Wholesalers, Retailers, their pivot table, and Stock.
- There are also separate configuration files for environment setup and deployment (e.g., “src/config”).

##### Key Project Components
a. API Layer:
– Represent Nest.js Controllers (e.g., retailer.controller.ts, stock.controller.ts, wholesaler.controller.ts)
– Highlight API endpoints mentioned in the README (e.g., Get Wholesaler with Retailers, Get Retailers with a Single Wholesaler, etc.)
b. Business Logic Layer:
– Include services (e.g., retailer.service.ts, stock.service.ts, wholesaler.service.ts) handling the core business logic.
c. Data Access Layer:
– Mention models and configuration in “models” and “src/common/postgres” modules.
d. Database:
– Include the PostgreSQL database with the identified tables:
▪ Wholesalers
▪ Retailers
▪ WholesalerRetailers (pivot table)
▪ Stock
– Indicate relationships (e.g., many-to-many between Wholesalers and Retailers, and a related Stock table).
e. Supporting Modules:
– Show the “common/config” and “common/postgres” modules representing configuration management and database connection details.
– Include the “core” modules (retailer, wholesaler, stock) each encapsulating their respective business logic.
f. External Interaction:

##### Relationships & Interactions
- Clients (e.g., Swagger UI or any API consumer) send HTTP requests to the Nest.js API (controllers).
- Controllers route requests to the corresponding service layers.
- Services interact with database models through Sequelize ORM to perform CRUD operations.
- The configuration modules manage environment settings and connection parameters.
- Clearly demarcate layers (API, Business Logic, Data Access, and Database) so that interactions and data flow are obvious.

##### Architectural Patterns & Design Principles
- MVC Pattern: the controllers (handling HTTP requests), services (business logic), and models (data) are clearly separated.
- Modules: The use of Nest.js modules indicates a modular design where functionalities (retailer, wholesaler, stock) are self-contained.
- ORM Abstraction: Sequelize ORM abstracts database access, aligning with clean separation between business logic and data persistence.

##### Relevant Technologies, Frameworks & Libraries
- Node.js and Nest.js for runtime and API framework.
- Sequelize ORM for database operations.
- PostgreSQL/MySQL as the relational database.
- Tools like ESLint, Prettier, and tsconfig files for code styling and TypeScript configuration.
- Migration scripts and seed files indicating database schema initialization and seeding.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Global Entities
    Client["Client (Swagger UI/API Consumer)"]:::client
    API_Split["API Gateway"]:::api

    Client -->|"HTTP_Request"| API_Split

    %% API Layer
    subgraph "API Layer"
        retailerController["Retailer Controller"]:::api
        wholesalerController["Wholesaler Controller"]:::api
        stockController["Stock Controller"]:::api
    end

    API_Split --> retailerController
    API_Split --> wholesalerController
    API_Split --> stockController

    %% Business Logic Layer
    subgraph "Business Logic Layer"
        retailerService["Retailer Service"]:::service
        wholesalerService["Wholesaler Service"]:::service
        stockService["Stock Service"]:::service
    end

    retailerController -->|"calls"| retailerService
    wholesalerController -->|"calls"| wholesalerService
    stockController -->|"calls"| stockService

    %% Data Access Layer
    subgraph "Data Access Layer"
        ormModels["Sequelize ORM & Models"]:::orm
        pgConfig["PostgreSQL Configuration"]:::orm
    end

    retailerService -->|"executes"| ormModels
    wholesalerService -->|"executes"| ormModels
    stockService -->|"executes"| ormModels

    ormModels -->|"uses"| pgConfig

    %% Database Layer
    database["Database (PostgreSQL)"]:::db
    pgConfig -->|"connects"| database

    migrations["Database Migrations"]:::migration
    migrations -->|"sets_up"| database

    %% Supporting Modules
    subgraph "Supporting Modules"
        commonConfig["Common Configuration Module"]:::support
        envConfig["Environment & Application Config"]:::support
        commonPgModule["Common PostgreSQL Module"]:::support
    end

    envConfig -->|"provides"| pgConfig
    commonPgModule -->|"supports"| pgConfig
    envConfig -->|"configures"| commonConfig

    %% Click Events
    click retailerController "https://github.com/ssv96/sales-admin-panel/blob/main/src/core/retailer/retailer.controller.ts"
    click wholesalerController "https://github.com/ssv96/sales-admin-panel/blob/main/src/core/wholesaler/wholesaler.controller.ts"
    click stockController "https://github.com/ssv96/sales-admin-panel/blob/main/src/core/stock/stock.controller.ts"

    click retailerService "https://github.com/ssv96/sales-admin-panel/blob/main/src/core/retailer/retailer.service.ts"
    click wholesalerService "https://github.com/ssv96/sales-admin-panel/blob/main/src/core/wholesaler/wholesaler.service.ts"
    click stockService "https://github.com/ssv96/sales-admin-panel/blob/main/src/core/stock/stock.service.ts"

    click ormModels "https://github.com/ssv96/sales-admin-panel/blob/main/models/index.js"
    click pgConfig "https://github.com/ssv96/sales-admin-panel/blob/main/src/common/postgres/postgres.config.ts"
    click migrations "https://github.com/ssv96/sales-admin-panel/blob/main/migrations/20250323181811-create-all-tables.js"

    click commonConfig "https://github.com/ssv96/sales-admin-panel/blob/main/src/common/config/config.module.ts"
    click envConfig "https://github.com/ssv96/sales-admin-panel/tree/main/src/config"
    click commonPgModule "https://github.com/ssv96/sales-admin-panel/blob/main/src/common/postgres/postgres.module.ts"

    %% Styles
    classDef client fill:#ffdddd,stroke:#cc0000,stroke-width:2px;
    classDef api fill:#cceeff,stroke:#0066cc,stroke-width:2px;
    classDef service fill:#ddffdd,stroke:#009900,stroke-width:2px;
    classDef orm fill:#ffffcc,stroke:#999900,stroke-width:2px;
    classDef db fill:#ccffcc,stroke:#339933,stroke-width:2px;
    classDef support fill:#ffeedd,stroke:#cc6600,stroke-width:2px;
    classDef migration fill:#ffddee,stroke:#cc3399,stroke-width:2px;
```
