Here is a stock bug that throws no error, passes every single-device test, and still loses inventory.
The counter model
The obvious model is a number on the product:
product . stock = 12 ; // on each sale: product . stock -= quantitySold ;
Enter fullscreen mode
Exit fullscreen mode
On one device it works. Now add a second till, and let both work offline.
Both tills start with stock = 12 .
Till A sells 2, so it stores 10 .
Till B sells 3, so it stores 9 .
Both reconnect and sync their new value.
The server keeps 10 or 9 , depending on which arrives last. Five units left the shop. The system recorded two or three. Nobody sees an error.
The ledger model
Instead of storing the current number, store every change as a movement with a signed delta:
class StockMove { const StockMove ( this . id , this . productId , this . delta , this .β¦