Skip to main content

Engineering Log · 20 September – 14 October 2024

Hardening the status tree

The simulator ran the status rules faster and more often than any person would, and the edge cases came out in a day. A refunded status, a command that re-derives every status, rules for cancelling a line, and rejected purchase orders refunded and sourced again.

An order's status in Turtle is never set by hand: it rolls up from its purchase order items, and at each level the least-progressed child wins. In mid-September the simulator started driving those rules, faster and more often than any supplier would, and it reached the edge cases quickly. Friday 20 September went on fixing them.

A rejection that let go too early

When rejections arrived a few days earlier, a rejected purchase order item stopped counting towards its line's quantity, the same as a cancelled one. The line went straight back to having quantity outstanding, with the rejected item still attached.

First I added a status after rejected: refunded. A rejected item now holds its quantity on the line until it is refunded, and refunded is the only place a rejection can go. Then the line's roll-up learned to skip refunded items, and to fall back to pending when every item on a line has been refunded.

A command that re-derives everything

Fixing a rule doesn't fix the orders already saved under the old one. So I added app:refresh-order-status. It walks the orders, oldest first, and re-derives every status from the bottom up: each purchase order from its items, each line from its purchase order items, the order from its lines. It prints every status it changes and dispatches the domain events, so each correction lands in the status log like any other change.

Running it found a real gap: a line on a cancelled order with nothing on a purchase order rolled up to pending, so re-deriving a cancelled order's statuses brought its lines back to life. Now a line on a cancelled order stays cancelled.

That fix had a cost of its own. The new check reads the line's order, and a line being created didn't have one yet, so placing an order failed. The fix was to attach the line to its order before building it, and orders flowed again.

The command stayed until the rebuild by bounded context.

Cancelling a line

Soon after, a single line could be cancelled, not just a whole order. The rule is narrow: a line can be cancelled only while it is pending or processing and nothing has gone on a purchase order. A cancelled line drops out of the order's totals, and an order whose lines are all cancelled drops its shipping charge too.

A week later order processing got two fixes. A line with quantity outstanding but nothing on a purchase order, including a fully refunded one, had shown as processing; it now stays pending until something is ordered from a supplier. And sending an order's purchase orders to the suppliers now moves only the items still pending, so an item a supplier has already answered is left alone.

Refunded and sourced again

In mid-October the refunded status got its command. app:refund-purchase-orders takes rejected purchase orders, refunds their items, and runs the sourcing again for the order. The refund frees the line's quantity, so the line goes to whichever supplier is cheapest with the stock now, which can be the supplier that rejected it.

Update, November 2026: ADR 002 records the allocation rules, which haven't changed in substance: the whole outstanding quantity on one supplier, or the line is skipped. The build command now also retries orders that still have quantity outstanding, after the new ones, so a skipped line no longer waits for someone to click Allocate.

← All posts