How kevinsrb/inventory-management-api works
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: adjust-inventory.use-case.ts Inventory use cases · app.module.ts App module · create-purchase-order.use-case.ts PO use cases · create-product.use-case.ts Product use cases
Components 27
Runtime
NestJS entrypoint and request delivery
- Main · Starts Nest, configures validation, logging, global errors, and API prefix
src/main.ts - App module · Wires feature modules and shared infrastructure together
src/app.module.ts - Health · Shared readiness/health controller
src/shared/interfaces/health.controller.ts
Business modules
Feature modules with clean architecture layers
- Products API · HTTP interface for product creation and listing
src/modules/products/interfaces/products.controller.ts - Product use cases · Create and list products
src/modules/products/application/create-product.use-case.ts - Product domain · Product entity and repository port
src/modules/products/domain/product.ts - Product repo · Prisma-backed product repository
src/modules/products/infrastructure/prisma-product.repository.ts - Inventory API · HTTP interface for stock adjustment, lookup, and movements
src/modules/inventory/interfaces/inventory.controller.ts - Inventory use cases · Adjust inventory, read current stock, and list movements
src/modules/inventory/application/adjust-inventory.use-case.ts - Inventory domain · Inventory model and movement repository port
src/modules/inventory/domain/inventory.ts - Movement repo · Prisma-backed inventory movement repository
src/modules/inventory/infrastructure/prisma-inventory-movement.repository.ts - Alerts API · HTTP interface for stock alerts
src/modules/alerts/interfaces/alerts.controller.ts - Alert use cases · List alerts and close active alerts
src/modules/alerts/application/close-stock-alert.use-case.ts - Alert domain · Stock alert model and repository port
src/modules/alerts/domain/stock-alert.ts - Alert repo · Prisma-backed alert repository
src/modules/alerts/infrastructure/prisma-alert.repository.ts - PO API · HTTP interface for purchase orders
src/modules/purchase-orders/interfaces/purchase-orders.controller.ts - PO use cases · Create, approve, reject, receive, and list purchase orders
src/modules/purchase-orders/application/create-purchase-order.use-case.ts - PO domain · Purchase order model and repository port
src/modules/purchase-orders/domain/purchase-order.ts - PO repo · Prisma-backed purchase-order repository
src/modules/purchase-orders/infrastructure/prisma-purchase-order.repository.ts
Shared core
Cross-cutting domain and infrastructure
- Value objects · Common value objects and domain error types
src/shared/domain/value-objects.ts - Unit of work · Prisma unit of work for serializable multi-aggregate changes
src/shared/database/prisma-unit-of-work.ts - Validation config · Environment config, validation pipe, exception filter, and logging interceptor
src/shared/config/environment.ts
Database
Prisma and PostgreSQL persistence boundary
- Prisma service · Shared Prisma client service
src/shared/database/prisma.service.ts - Schema · Prisma schema defining database structure and constraints
prisma/schema.prisma
Operations
Migration, seeding, and deployment artifacts
- Migrations & seed · Database migrations and idempotent seed data
prisma/migrations/20260702000000_initial/migration.sql
Tests
Unit-style and integration coverage
- Unit tests · Application use-case tests with in-memory repositories
test/application/use-cases.spec.ts - Integration tests · PostgreSQL-specific persistence tests
test/integration/inventory-movement-immutability.integration-spec.ts
Connections 35
- 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