Multi-Branch Inventory System
A backend for a retail chain where every branch holds its own stock of every product variant, and one online store sells across all of them. The hard part isn't the CRUD — it's what happens when two customers buy the last unit in the same millisecond.
Stock adjustments run inside a transaction that takes a pessimistic write lock on the row, so the second request waits, re-reads the real quantity, and is rejected instead of overselling. I proved it rather than assumed it: a script fires two identical requests through Promise.all and exactly one succeeds. Three roles — admin, branch manager, staff — gate every endpoint that can move inventory.
View on GitHub ↗// two buyers, one unit left, one winner return this.dataSource.transaction(async (m) => { const record = await m.findOne(Inventory, { where: { id }, lock: { mode: 'pessimistic_write' }, }); if (record.quantity + change < 0) throw new BadRequestException( 'Insufficient stock in this branch'); record.quantity += change; return m.save(record); });