Version 1 of 1 · · Current version

tags
#shop

shop-shipping

Shipped. When a shipment's status becomes shipped, its order is marked shipped and the customer gets the tracking number.

$when shop.shipped (id: String) {
  sh = dom.find(cls = "Shipment", key = id)
  o = dom.find(cls = "Order", key = sh.order)
  dom.upsert(cls = "Order", entity = o + {status: "shipped"})
  who = dom.find(cls = "Customer", key = o.customer)
  payload = shop.notify(to = who.email, text = "On its way: order ${o.id}, ${sh.carrier} ${sh.tracking}")
}

Returns. When a return is approved, the customer is refunded for the lines sent back (at the price they paid), and the stock goes back on the shelf.

$when shop.returnApproved (id: String) {
  r = dom.find(cls = "Return", key = id)
  o = dom.find(cls = "Order", key = r.order)
  pays = dom.list(cls = "Payment").data filter (p => p.order is r.order)
  amount = r.lines map (l => (o.lines filter (x => x.sku is l.sku) map (x => x.unitPrice * l.qty)) fold (s = 0.0) (y => s + y)) fold (s = 0.0) (x => s + x)
  dom.upsert(cls = "Refund", entity = {id: "RF-${id}", payment: pays[0].id, amount: amount, reason: r.reason})
  back = r.lines map (l => shop.restock(sku = l.sku, qty = l.qty))
  who = dom.find(cls = "Customer", key = o.customer)
  shop.notify(to = who.email, text = "Your return ${id} is approved: ${amount} is on its way back to you.")
  payload = {refund: "RF-${id}", amount: amount, restocked: sizeOf(back)}
}