Rank 5: Hypervisor
I have a Collection with a list of items and our stock level for them. When someone makes an order, I need to decrement stock, and when new stock is added, I need to increment stock.
The simplest way would be to getDocument, then updateDocument with stock-1, but that would be an issue if there are two orders at the same time.
The only method I can think of to deal with this is:
Create a new record in a collection order_updates.
Check order_updates for any records created before the one I just created.
If there are, wait 0.5 seconds and check again.
Once there are no more records before my new one, get the current stock level and update it.
Delete the order_updates record.
This seems a bit roundabout, though - does anyone have any better ways to deal with this? TIA!