5 pain points identified before writing a single line of code
The store relied on manual stock checking inside the storage room
Employees had to physically verify product availability multiple times per day
Frequent sales errors (selling unavailable products)
Slow checkout process due to lack of real-time visibility
No clear financial tracking or stock accuracy
Real cost — not hypothetical risk
Lost sales due to unavailable products going undetected until checkout
Wasted time and physical effort for employees on repetitive stock verification
Poor customer experience caused by long wait times inside the store
Inaccurate understanding of inventory levels and actual revenue
Operational chaos affecting daily performance and decision-making
7 deliberate trade-offs
Connected inventory directly to the sales process (real-time sync)
Introduced multi-product order system instead of single-product checkout
Added stock threshold alerts for better anticipation
Implemented flexible pricing (min/max) to support real negotiation
Designed invoice system supporting both individuals and companies
Made VAT (TVA) optional based on real business scenarios
Integrated automatic warranty logic per product
The blueprint that holds everything together
Separated product entity from stock entity to avoid data duplication
Each stock batch has its own purchase price for accurate accounting
Implemented FIFO logic for precise profit calculation
Used status-based operations for cancel/return with automatic stock recovery
Designed system with data consistency and transactional integrity in mind
5 measurable improvements
Stock availability became visible during checkout instead of after a manual trip to storage
Cashiers could process multi-product orders inside one consistent flow
Profit tracking moved from approximation to batch-based calculation
Returns and cancellations restored stock through defined system rules
The owner gained a clearer daily view of sales, stock movement, and invoicing
How the system is designed under the hood — architecture, data model, and guarantees.
Catalog entry with cached stock (synced from FIFO)
FIFO batch — single source of truth for stock & cost
Single-product workflow document (UI entry point)
Multi-product sale with embedded OrderItems
Canonical financial record — immutable after creation
Legal document (FACTURE / BON DE VENTE) with embedded items
Warranty document with lifecycle tracking
Immutable daily aggregates for drift detection
3-layer defense: cached stock → FIFO aggregate → atomic batch guard ($gte). Concurrent conflicts return 409.
Cancellation atomically updates Sale + STI + Invoice + Guarantee + FIFO restore in one transaction.
Cost comes from FIFO batches (not product-level). Each unit sold carries its exact purchase price.
STI financial fields and daily snapshots cannot be modified. Corrections create new records.
Every critical operation uses MongoDB transactions. Partial state is impossible — commit or full rollback.
Point-in-time snapshots on every sale so later product edits do not rewrite past invoices or reports.
InventoryLog.remainingQty ← SINGLE SOURCE OF TRUTHProduct.stock = Σ(InventoryLog.remainingQty) // cachedSale → FIFO consumption (oldest batch first)Cancel → FIFO restoration (new entry at original cost)Product (1) ────── (N) InventoryLogSale (1) ────────── (1) SalesTransactionItemOrder (1) ───────── (N) SalesTransactionItemInvoice (1) ─────── (0..1) Guaranteesession = mongoose.startSession()session.startTransaction() ... all ops with { session } ...session.commitTransaction() || abortTransaction()Exact profit per unit — critical for a store where purchase prices fluctuate between batches
Separates mutable workflow state from immutable financial truth — analytics stay clean
Historical accuracy without heavy join queries - past invoices stay tied to stored sale snapshots
Fast reads for UI/POS — FIFO aggregation remains authoritative for all write operations