- tags
- #shop
shop-orders¶
Order placed. When an order is created, the shop issues its invoice, reserves the stock for each line (from the first warehouse that has enough), checks whether that left any stock low, and thanks the customer.
$when shop.orderPlaced (id: String) {
o = dom.find(cls = "Order", key = id)
dom.upsert(cls = "Invoice", entity = {number: "INV-${id}", order: id, amount: o.total, issued: now()})
reserved = o.lines map (l => shop.reserve(sku = l.sku, qty = l.qty))
who = dom.find(cls = "Customer", key = o.customer)
shop.notify(to = who.email, text = "Thanks, ${who.name}: order ${id} is placed.")
payload = {invoice: "INV-${id}", total: o.total, reserved: reserved}
}
Reserving takes the stock out of one warehouse. A flow's own changes to objects don't set off other flows (so one order can't start a chain of them), which is why this rule asks for the stock check itself.
$when shop.reserve (sku: String, qty) {
levels = dom.list(cls = "StockLevel").data filter (s => s.sku is sku and s.qty >= qty)
if (levels is empty) {
payload = {sku: sku, reserved: false}
} else {
s = levels[0]
dom.upsert(cls = "StockLevel", entity = s + {qty: s.qty - qty})
low = shop.checkStock(id = s.id)
payload = {sku: sku, reserved: true, warehouse: s.warehouse, stock: low}
}
}