Skip to content

Stock reservation

A shop holds stock for a customer while they check out. reserve checks that there is enough stock, saves the reservation, then takes the units out of stock. The check runs before the await and the units are taken after it, so two reservations that start together both pass the check against the same stock. The check also lets a quantity of 0, a negative one or a fraction through.

src/inventory.ts
async reserve(sku: string, quantity: number): Promise<string | null> {
if (quantity > (this.stock[sku] ?? 0)) {
return null;
}
const id = await this.deps.saveReservation({ sku, quantity });
this.stock[sku] = (this.stock[sku] ?? 0) - quantity;
return id;
}

See how Hegel finds and fixes it.

Last updated: