Back to the Operations Ledger journey

DecidedStop 6 of 8

Saving a purchase and its retry answer together

Operations Ledger saves a purchase, its history entry and the answer a retry will get together, or not at all. A separate invoice check catches the same purchase sent again under a new key.

The question

Operations Ledger records purchases from invoices. Sometimes the same request arrives a second time. The caller should then get the first answer back, not a second purchase. So the ledger saves two things. One is the purchase. The other is the answer a retry will get, called the Operations Ledger · The ledgerPurchase writeLocks the database, then saves the purchase, its lines and its stock records. The history entry and the retry answer are saved with them, all or nothing.See it in the Operations Ledger build. When should each be saved?

What made it hard

If the retry answer is saved after the purchase, a purchase could exist with nothing to give back. If it is saved first, it could claim success for a purchase that was never saved. There is a second problem. A caller sends a label with each request, called a Operations Ledger · The ledgerRetry checkMatches the key, the action's name and a short code made from the request. An exact repeat gets the first answer back, and a changed one is refused.See it in the Operations Ledger build. A new retry key doesn't always mean a new purchase.

What I chose

Before anything is saved, a Operations Ledger · The ledgerPurchase calculatorWorks out the discounts, the invoice's separate amounts and the split again. It stops if anything is off at all.See it in the Operations Ledger build works out the invoice's lines and totals again. If they don't add up exactly, or the supporting document is missing, it stops.

The rest is saved in one A technical worddatabase transactionA group of database changes saved together, so either all of them are saved or none are., so either every change is saved or none is. This step locks the database for writing as soon as it starts. First the ledger checks the retry key. It matches the key against the action's name and a fingerprint of the request. A fingerprint is a short code made from the request's content, and it changes when the content changes. If both match, this is an exact retry, and the caller gets the saved retry answer. If either one changed, the request fails.

A retry key names a request, not a purchase. So a separate Operations Ledger · The ledgerInvoice checkFinds a purchase that is already saved, even when it comes with a new request key.See it in the Operations Ledger build refuses an invoice the ledger already holds, whatever key it comes with.

Then the purchase is saved with its lines. Its stock records and a history entry are saved with it. The retry answer is saved with them too, and an error undoes them all. A purchase never exists without its retry answer, and a retry answer never exists without its purchase.

How I know it works

The tests cover four cases. Two are an exact retry and a saved invoice sent with a new key. The other two are a changed request under a used key and an attempt to edit a history entry. A retry answer confirms a record in this ledger only. If an outside account also has to be updated, that needs its own check that reads the account back.