# kevinsrb/inventory-management-api architecture

> Architecture diagram of the GitHub repository kevinsrb/inventory-management-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-07). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/kevinsrb/inventory-management-api
- Repository: https://github.com/kevinsrb/inventory-management-api

## Overview

1. **Overall shape**
   - This is a NestJS-based REST API for inventory management, but the core business logic is intentionally framework-agnostic.
   - The repo is organized as a Clean Architecture / Hexagonal system: HTTP and persistence are outer layers, while `domain` and `application` are plain TypeScript.

2. **Top-level module boundaries**
   - Business capabilities are split into four feature modules under `src/modules/`:
     - `products`
     - `inventory`
     - `alerts`
     - `purchase-orders`
   - Each module has the same internal layering: `domain`, `application`, `infrastructure`, and `interfaces`.
   - `src/app.module.ts` is the composition root that wires the modules together.

3. **HTTP delivery layer**
   - Controllers in `interfaces/` expose the REST API and translate HTTP requests into DTOs.
   - The main entrypoint `src/main.ts` bootstraps Nest, applies validation, global exception handling, logging, and the `/api` prefix.
   - A shared health endpoint lives in `src/shared/interfaces/health.controller.ts`.

4. **Application layer as orchestration**
   - Each business action is modeled as a use case class in `application/`:
     - create/list products
     - adjust/get inventory
     - list/close alerts
     - create/approve/reject/receive/list purchase orders
   - Use cases coordinate repositories, policies, and transactions; they do not know about HTTP or Prisma directly.

5. **Domain layer and business rules**
   - The `domain/` folders hold entities, repository ports, and business policies.
   - Core concepts include `Product`, `Inventory`, `StockAlert`, and `PurchaseOrder`, plus shared value objects like `Sku`, `Money`, `Quantity`, and `Stock`.
   - Domain policies centralize rules such as stock validation, alert activation/closure, and purchase-order state transitions.

6. **Persistence boundary**
   - Prisma adapters in `infrastructure/` implement the repository ports defined in domain.
   - `src/shared/database/prisma.service.ts` and `prisma/schema.prisma` define the database access layer.
   - PostgreSQL is the backing store; Prisma is the only persistence technology visible to the outer layers.

7. **Transactional consistency**
   - Multi-aggregate operations use a Prisma unit of work (`src/shared/database/prisma-unit-of-work.ts`).
   - The repo’s README states these transactions run with `Serializable` isolation, which matters for inventory adjustments, alert updates, and purchase-order receipt flows.
   - This is the main boundary for keeping stock, movements, alerts, and orders consistent together.

8. **Main business flows**
   - **Products:** create a product with unique SKU and list products.
   - **Inventory:** adjust stock up/down, retrieve current inventory, and list movement history.
   - **Alerts:** create stock-low alerts automatically, list alerts, and close them manually.
   - **Purchase orders:** create orders, approve/reject pending ones, and receive orders, which also updates inventory.
   - These flows are connected: inventory changes can trigger alert changes, and receiving a purchase order affects stock.

9. **Shared infrastructure and cross-cutting concerns**
   - `src/shared/validation/app-validation.pipe.ts` handles request validation.
   - `src/shared/filters/global-exception.filter.ts` maps domain errors to HTTP responses.
   - `src/shared/interceptors/logging.interceptor.ts` provides request logging.
   - `src/shared/config/environment.ts` centralizes validated environment configuration instead of reading `process.env` throughout the codebase.

10. **Database-level invariants**
   - The architecture relies on both application rules and PostgreSQL constraints to enforce invariants.
   - The README highlights important guarantees: unique SKU, at most one active alert per product, immutable inventory movements, and valid state/quantity constraints.
   - This means the diagram should show not just code paths, but also database-enforced consistency boundaries.

11. **Testing strategy**
   - Unit-style tests in `test/application/` exercise use cases with in-memory repositories.
   - Integration tests in `test/integration/` verify PostgreSQL-specific behavior such as immutability of inventory movements.
   - Tests focus on the application and persistence boundaries, not on the Nest controllers.

12. **Operational/deployment context**
   - The app is designed to run in Docker and deploy on Railway with PostgreSQL.
   - `prisma/migrations/` and `prisma/seed.ts` are part of the operational model: schema migration and idempotent seeding are first-class architecture elements.
   - The system exposes `/api/health` for platform health checks and `/docs` for Swagger/OpenAPI documentation.

Main entry points: [`src/modules/inventory/application/adjust-inventory.use-case.ts`](https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/inventory/application/adjust-inventory.use-case.ts) (Inventory use cases), [`src/app.module.ts`](https://github.com/kevinsrb/inventory-management-api/blob/main/src/app.module.ts) (App module), [`src/modules/purchase-orders/application/create-purchase-order.use-case.ts`](https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/purchase-orders/application/create-purchase-order.use-case.ts) (PO use cases), [`src/modules/products/application/create-product.use-case.ts`](https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/products/application/create-product.use-case.ts) (Product use cases)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime"]
  node_main["Main<br/>bootstrap<br/>[main.ts]"]
  node_app_module["App module<br/>composition root<br/>[app.module.ts]"]
  node_health["Health<br/>http endpoint"]
end

subgraph group_modules["Business modules"]
  node_products_api["Products API<br/>controller"]
  node_products_use_cases["Product use cases"]
  node_products_domain["Product domain<br/>[product.ts]"]
  node_products_repo["Product repo<br/>adapter"]
  node_inventory_api["Inventory API<br/>controller"]
  node_inventory_use_cases["Inventory use cases"]
  node_inventory_domain["Inventory domain<br/>[inventory.ts]"]
  node_inventory_repo["Movement repo<br/>adapter"]
  node_alerts_api["Alerts API<br/>controller"]
  node_alerts_use_cases["Alert use cases"]
  node_alerts_domain["Alert domain<br/>[stock-alert.ts]"]
  node_alerts_repo["Alert repo<br/>adapter"]
  node_po_api["PO API<br/>controller"]
  node_po_use_cases["PO use cases"]
  node_po_domain["PO domain<br/>[purchase-order.ts]"]
  node_po_repo["PO repo<br/>adapter"]
end

subgraph group_shared["Shared core"]
  node_shared_domain["Value objects<br/>shared domain<br/>[value-objects.ts]"]
  node_shared_uow["Unit of work<br/>transaction"]
  node_validation_config["Validation config<br/>cross-cutting<br/>[environment.ts]"]
end

subgraph group_database["Database"]
  node_prisma_service["Prisma service<br/>database access<br/>[prisma.service.ts]"]
  node_prisma_schema["Schema<br/>[schema.prisma]"]
end

subgraph group_ops["Operations"]
  node_migrations_seed["Migrations & seed<br/>ops<br/>[migration.sql]"]
end

subgraph group_tests["Tests"]
  node_unit_tests["Unit tests<br/>[use-cases.spec.ts]"]
  node_integration_tests["Integration tests"]
end

node_main -->|"bootstraps"| node_app_module
node_main -->|"applies"| node_validation_config
node_app_module -->|"loads"| node_products_api
node_app_module -->|"loads"| node_inventory_api
node_app_module -->|"loads"| node_alerts_api
node_app_module -->|"loads"| node_po_api
node_app_module -->|"exposes"| node_health
node_products_api -->|"calls"| node_products_use_cases
node_products_use_cases -->|"uses"| node_products_domain
node_products_use_cases -->|"persists via"| node_products_repo
node_products_repo -->|"uses"| node_prisma_service
node_inventory_api -->|"calls"| node_inventory_use_cases
node_inventory_use_cases -->|"uses"| node_inventory_domain
node_inventory_use_cases -->|"persists via"| node_inventory_repo
node_inventory_use_cases -->|"coordinates"| node_shared_uow
node_inventory_use_cases -.->|"triggers"| node_alerts_use_cases
node_alerts_api -->|"calls"| node_alerts_use_cases
node_alerts_use_cases -->|"uses"| node_alerts_domain
node_alerts_use_cases -->|"persists via"| node_alerts_repo
node_po_api -->|"calls"| node_po_use_cases
node_po_use_cases -->|"uses"| node_po_domain
node_po_use_cases -->|"persists via"| node_po_repo
node_po_use_cases -->|"coordinates"| node_shared_uow
node_po_use_cases -.->|"affects"| node_inventory_use_cases
node_products_domain -->|"reuses"| node_shared_domain
node_inventory_domain -->|"reuses"| node_shared_domain
node_alerts_domain -->|"reuses"| node_shared_domain
node_po_domain -->|"reuses"| node_shared_domain
node_shared_uow -->|"wraps"| node_prisma_service
node_prisma_service -->|"maps to"| node_prisma_schema
node_prisma_schema -->|"evolves through"| node_migrations_seed
node_unit_tests -.->|"covers"| node_products_use_cases
node_unit_tests -.->|"covers"| node_inventory_use_cases
node_integration_tests -.->|"verifies"| node_prisma_schema
node_integration_tests -.->|"exercises"| node_shared_uow

click node_main "https://github.com/kevinsrb/inventory-management-api/blob/main/src/main.ts"
click node_app_module "https://github.com/kevinsrb/inventory-management-api/blob/main/src/app.module.ts"
click node_health "https://github.com/kevinsrb/inventory-management-api/blob/main/src/shared/interfaces/health.controller.ts"
click node_products_api "https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/products/interfaces/products.controller.ts"
click node_products_use_cases "https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/products/application/create-product.use-case.ts"
click node_products_domain "https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/products/domain/product.ts"
click node_products_repo "https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/products/infrastructure/prisma-product.repository.ts"
click node_inventory_api "https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/inventory/interfaces/inventory.controller.ts"
click node_inventory_use_cases "https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/inventory/application/adjust-inventory.use-case.ts"
click node_inventory_domain "https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/inventory/domain/inventory.ts"
click node_inventory_repo "https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/inventory/infrastructure/prisma-inventory-movement.repository.ts"
click node_alerts_api "https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/alerts/interfaces/alerts.controller.ts"
click node_alerts_use_cases "https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/alerts/application/close-stock-alert.use-case.ts"
click node_alerts_domain "https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/alerts/domain/stock-alert.ts"
click node_alerts_repo "https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/alerts/infrastructure/prisma-alert.repository.ts"
click node_po_api "https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/purchase-orders/interfaces/purchase-orders.controller.ts"
click node_po_use_cases "https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/purchase-orders/application/create-purchase-order.use-case.ts"
click node_po_domain "https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/purchase-orders/domain/purchase-order.ts"
click node_po_repo "https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/purchase-orders/infrastructure/prisma-purchase-order.repository.ts"
click node_shared_domain "https://github.com/kevinsrb/inventory-management-api/blob/main/src/shared/domain/value-objects.ts"
click node_shared_uow "https://github.com/kevinsrb/inventory-management-api/blob/main/src/shared/database/prisma-unit-of-work.ts"
click node_prisma_service "https://github.com/kevinsrb/inventory-management-api/blob/main/src/shared/database/prisma.service.ts"
click node_prisma_schema "https://github.com/kevinsrb/inventory-management-api/blob/main/prisma/schema.prisma"
click node_migrations_seed "https://github.com/kevinsrb/inventory-management-api/blob/main/prisma/migrations/20260702000000_initial/migration.sql"
click node_validation_config "https://github.com/kevinsrb/inventory-management-api/blob/main/src/shared/config/environment.ts"
click node_unit_tests "https://github.com/kevinsrb/inventory-management-api/blob/main/test/application/use-cases.spec.ts"
click node_integration_tests "https://github.com/kevinsrb/inventory-management-api/blob/main/test/integration/inventory-movement-immutability.integration-spec.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_main,node_app_module,node_health toneBlue
class node_products_api,node_products_use_cases,node_products_domain,node_products_repo,node_inventory_api,node_inventory_use_cases,node_inventory_domain,node_inventory_repo,node_alerts_api,node_alerts_use_cases,node_alerts_domain,node_alerts_repo,node_po_api,node_po_use_cases,node_po_domain,node_po_repo toneAmber
class node_shared_domain,node_shared_uow,node_validation_config toneMint
class node_prisma_service,node_prisma_schema toneRose
class node_migrations_seed toneIndigo
class node_unit_tests,node_integration_tests toneTeal
```

## Components

### Runtime

NestJS entrypoint and request delivery

- [Main](https://github.com/kevinsrb/inventory-management-api/blob/main/src/main.ts): bootstrap · Starts Nest, configures validation, logging, global errors, and API prefix · `src/main.ts`
- [App module](https://github.com/kevinsrb/inventory-management-api/blob/main/src/app.module.ts): composition root · Wires feature modules and shared infrastructure together · `src/app.module.ts`
- [Health](https://github.com/kevinsrb/inventory-management-api/blob/main/src/shared/interfaces/health.controller.ts): http endpoint · Shared readiness/health controller · `src/shared/interfaces/health.controller.ts`

### Business modules

Feature modules with clean architecture layers

- [Products API](https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/products/interfaces/products.controller.ts): controller · HTTP interface for product creation and listing · `src/modules/products/interfaces/products.controller.ts`
- [Product use cases](https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/products/application/create-product.use-case.ts): application · Create and list products · `src/modules/products/application/create-product.use-case.ts`
- [Product domain](https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/products/domain/product.ts): domain · Product entity and repository port · `src/modules/products/domain/product.ts`
- [Product repo](https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/products/infrastructure/prisma-product.repository.ts): adapter · Prisma-backed product repository · `src/modules/products/infrastructure/prisma-product.repository.ts`
- [Inventory API](https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/inventory/interfaces/inventory.controller.ts): controller · HTTP interface for stock adjustment, lookup, and movements · `src/modules/inventory/interfaces/inventory.controller.ts`
- [Inventory use cases](https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/inventory/application/adjust-inventory.use-case.ts): application · Adjust inventory, read current stock, and list movements · `src/modules/inventory/application/adjust-inventory.use-case.ts`
- [Inventory domain](https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/inventory/domain/inventory.ts): domain · Inventory model and movement repository port · `src/modules/inventory/domain/inventory.ts`
- [Movement repo](https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/inventory/infrastructure/prisma-inventory-movement.repository.ts): adapter · Prisma-backed inventory movement repository · `src/modules/inventory/infrastructure/prisma-inventory-movement.repository.ts`
- [Alerts API](https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/alerts/interfaces/alerts.controller.ts): controller · HTTP interface for stock alerts · `src/modules/alerts/interfaces/alerts.controller.ts`
- [Alert use cases](https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/alerts/application/close-stock-alert.use-case.ts): application · List alerts and close active alerts · `src/modules/alerts/application/close-stock-alert.use-case.ts`
- [Alert domain](https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/alerts/domain/stock-alert.ts): domain · Stock alert model and repository port · `src/modules/alerts/domain/stock-alert.ts`
- [Alert repo](https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/alerts/infrastructure/prisma-alert.repository.ts): adapter · Prisma-backed alert repository · `src/modules/alerts/infrastructure/prisma-alert.repository.ts`
- [PO API](https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/purchase-orders/interfaces/purchase-orders.controller.ts): controller · HTTP interface for purchase orders · `src/modules/purchase-orders/interfaces/purchase-orders.controller.ts`
- [PO use cases](https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/purchase-orders/application/create-purchase-order.use-case.ts): application · Create, approve, reject, receive, and list purchase orders · `src/modules/purchase-orders/application/create-purchase-order.use-case.ts`
- [PO domain](https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/purchase-orders/domain/purchase-order.ts): domain · Purchase order model and repository port · `src/modules/purchase-orders/domain/purchase-order.ts`
- [PO repo](https://github.com/kevinsrb/inventory-management-api/blob/main/src/modules/purchase-orders/infrastructure/prisma-purchase-order.repository.ts): adapter · Prisma-backed purchase-order repository · `src/modules/purchase-orders/infrastructure/prisma-purchase-order.repository.ts`

### Shared core

Cross-cutting domain and infrastructure

- [Value objects](https://github.com/kevinsrb/inventory-management-api/blob/main/src/shared/domain/value-objects.ts): shared domain · Common value objects and domain error types · `src/shared/domain/value-objects.ts`
- [Unit of work](https://github.com/kevinsrb/inventory-management-api/blob/main/src/shared/database/prisma-unit-of-work.ts): transaction · Prisma unit of work for serializable multi-aggregate changes · `src/shared/database/prisma-unit-of-work.ts`
- [Validation config](https://github.com/kevinsrb/inventory-management-api/blob/main/src/shared/config/environment.ts): cross-cutting · Environment config, validation pipe, exception filter, and logging interceptor · `src/shared/config/environment.ts`

### Database

Prisma and PostgreSQL persistence boundary

- [Prisma service](https://github.com/kevinsrb/inventory-management-api/blob/main/src/shared/database/prisma.service.ts): database access · Shared Prisma client service · `src/shared/database/prisma.service.ts`
- [Schema](https://github.com/kevinsrb/inventory-management-api/blob/main/prisma/schema.prisma): schema · Prisma schema defining database structure and constraints · `prisma/schema.prisma`

### Operations

Migration, seeding, and deployment artifacts

- [Migrations & seed](https://github.com/kevinsrb/inventory-management-api/blob/main/prisma/migrations/20260702000000_initial/migration.sql): ops · Database migrations and idempotent seed data · `prisma/migrations/20260702000000_initial/migration.sql`

### Tests

Unit-style and integration coverage

- [Unit tests](https://github.com/kevinsrb/inventory-management-api/blob/main/test/application/use-cases.spec.ts): tests · Application use-case tests with in-memory repositories · `test/application/use-cases.spec.ts`
- [Integration tests](https://github.com/kevinsrb/inventory-management-api/blob/main/test/integration/inventory-movement-immutability.integration-spec.ts): tests · PostgreSQL-specific persistence tests · `test/integration/inventory-movement-immutability.integration-spec.ts`

## Connections

- Main → App module: bootstraps · Application startup delegates to the Nest composition root
- Main → Validation config: applies · Main process installs validation, logging, and exception handling
- App module → Products API: loads · Registers the products feature module
- App module → Inventory API: loads · Registers the inventory feature module
- App module → Alerts API: loads · Registers the alerts feature module
- App module → PO API: loads · Registers the purchase-order feature module
- App module → Health: exposes · Shared health endpoint is available through the app
- Products API → Product use cases: calls · Controllers translate HTTP requests into application use cases
- Product use cases → Product domain: uses · Use cases depend on domain entities and repository ports
- Product use cases → Product repo: persists via · Product workflows use the repository adapter at the edge
- Product repo → Prisma service: uses · Repository adapter uses shared Prisma access
- Inventory API → Inventory use cases: calls · HTTP inventory actions are routed into use cases
- Inventory use cases → Inventory domain: uses · Inventory orchestration depends on domain rules and ports
- Inventory use cases → Movement repo: persists via · Movement history is written through the Prisma adapter
- Inventory use cases → Unit of work: coordinates · Adjustments and related updates run inside a unit of work
- Inventory use cases → Alert use cases: triggers · Inventory changes can activate or close stock alerts
- Alerts API → Alert use cases: calls · Alerts controller delegates to application use cases
- Alert use cases → Alert domain: uses · Alert state transitions are enforced in domain logic
- Alert use cases → Alert repo: persists via · Alert workflows store through the Prisma adapter
- PO API → PO use cases: calls · Purchase-order endpoints dispatch to use cases
- PO use cases → PO domain: uses · Purchase-order lifecycle is modeled in the domain
- PO use cases → PO repo: persists via · Order workflows store and retrieve through the repository adapter
- PO use cases → Unit of work: coordinates · Receiving orders participates in transactional consistency
- PO use cases → Inventory use cases: affects · Receiving a purchase order updates inventory
- Product domain → Value objects: reuses · Business entities share common value objects and errors
- Inventory domain → Value objects: reuses · Inventory logic relies on shared quantity and stock types
- Alert domain → Value objects: reuses · Alerts use shared value objects and domain error handling
- PO domain → Value objects: reuses · Purchase-order state and quantities build on shared types
- Unit of work → Prisma service: wraps · Unit of work is implemented on top of Prisma transactions
- Prisma service → Schema: maps to · Prisma client is generated from the schema
- Schema → Migrations & seed: evolves through · Schema changes are applied by migrations and seed scripts
- Unit tests → Product use cases: covers · Application tests exercise use cases with in-memory dependencies
- Unit tests → Inventory use cases: covers · Application tests validate orchestration behavior
- Integration tests → Schema: verifies · Integration tests check database-level invariants and behavior
- Integration tests → Unit of work: exercises · Integration coverage includes transactional behavior
