Overview
KAIU’s inventory management system provides real-time stock tracking, product catalog management, and an admin interface for managing the entire product database. It includes a sophisticated stock reservation system to prevent overselling.Product Data Model
Database Schema
Product structure in Prisma:Product Variants
Products with multiple sizes/formats are stored as separate records:- Same Product, Different Sizes
- Frontend Display
Example: Lavender Essential Oil
Each variant has independent stock levels and pricing.
Stock Management
Reservation System
To prevent overselling during checkout process, system uses two-tier stock tracking:Available Stock
stock fieldTotal units physically in warehouse.Updated when:- Receiving new inventory
- Completing sales (COD immediate, Online after payment)
- Cancelling orders (stock released)
Reserved Stock
reserved fieldUnits temporarily held for pending orders.Updated when:- Order created (reserves)
- Order confirmed (converts to sale)
- Order cancelled (releases)
Inventory Service
Backend service handles all stock operations (backend/services/inventory/InventoryService.js):
reserveStock()
reserveStock()
Reserve units for new orderCalled during order creation:
confirmSale()
confirmSale()
Convert reservation to actual saleCalled when:Effect:
- COD order created (immediate)
- Online payment confirmed (webhook)
stockdecreases (physical inventory reduced)reserveddecreases (no longer held)- Net effect:
available = stock - reservedstays same
releaseReserve()
releaseReserve()
Return reserved units to poolCalled when:
- Order cancelled
- Order creation fails (rollback)
- Online payment fails/expires
Uses
Math.max(0, ...) to prevent reserved from going negative in edge cases.Stock Flow Examples
- COD Order
- Online Payment
- Order Cancellation
Cash on Delivery FlowInitial state:Customer orders 2 units:
-
Reserve stock:
Available: 50 - 2 = 48
-
Confirm sale (immediate):
Available: 48 - 0 = 48
COD orders confirm immediately because payment is guaranteed at delivery.
Admin Interface
Inventory Manager Component
Admin dashboard includes dedicated inventory tab (src/components/admin/InventoryManager.tsx - referenced in AdminDashboard.tsx:470).
Features:
- View all products in sortable table
- Filter by category, status (active/inactive)
- Search by name or SKU
- Edit product details inline
- Adjust stock levels
- Upload/change images
- Create new products
- Bulk operations
API Endpoints
Inventory management API (backend/admin/inventory.js):
- GET /api/admin/inventory
- POST /api/admin/inventory
- PUT /api/admin/inventory
Fetch all productsReturns array of all products, sorted by name then variant.
Product Categories
Standard categories used in KAIU:Aceites Esenciales
Essential oils extracted from plants
Aceites Portadores
Carrier oils for diluting essentials
Hidrolatos
Floral waters from distillation
Kits
Product bundles and gift sets
Difusores
Aromatherapy diffusers
Otros
Miscellaneous products
Stock Alerts
Low Stock Detection
System can be configured to alert when stock falls below threshold:Out of Stock Handling
- Frontend
- Cart
- AI Chatbot
Product cards automatically show “Out of Stock” badge:
Image Management
Upload Flow
Multiple Images
Products support multiple images (array):- First image is primary (shown in catalog)
- Additional images in product detail page gallery
- AI chatbot uses first image when sending product photos
Data Integrity
SKU Uniqueness
SKU field has unique constraint:Stock Validation
Stock and reserved fields have checks:Reporting
Inventory Value
Calculate total inventory value:Stock Movement Report
Track stock changes over time (future enhancement):Integration with AI Chatbot
The AI chatbot queries inventory in real-time:Best Practices
Always Reserve First
Never deduct stock directly. Always:
- Reserve
- Process order
- Confirm or release
Atomic Operations
Use database transactions for multi-product operations to ensure consistency.
Audit Trail
Log all stock changes with timestamp, reason, and admin user for accountability.
Regular Reconciliation
Periodically verify:
reservedmatches pending orders- Physical count matches database
- No orphaned reservations
Related Features
Order Management
How orders consume inventory
Admin Dashboard
Inventory management interface
E-commerce
Product catalog and shopping