Skip to content
achacon.dev
All work

2026

Rule-driven order tagging and carrier routing

A Cloudflare Worker that tags incoming orders and switches shipping method by rule, and records why every time.

Role
Sole engineer: worker, rules, and audit model
Stack
TypeScriptCloudflare WorkersWorkers KVCron TriggersSupabase PostgresShipStation APIVitest
  1. Trigger: 01Order webhookCron sweep catches the misses
  2. Process: 02Rules evaluatedOne file, one test, per rule
  3. Process: 03Carrier resolvedAccount cached, billing checked
  4. Store: 04Decision auditedEvery call recorded, dry run or not
  5. Output: 05Tag appliedThe tag is the processed marker
The same rules judge an order whether it arrives live or on the sweep.

The problem

Orders got tagged by hand for priority, promotions and local delivery, and whoever opened an order picked the shipping service. The rules lived in people's heads, which meant they were applied unevenly and nobody could reconstruct why a given order shipped the way it did.

What I built

  • Each rule is its own file with its own test. Changing what gets tagged means editing one file and one test, which matters because the rules are the part non-engineers ask about.
  • Order webhooks drive the live path and a queued cron scan sweeps up whatever the webhook missed. Both run the same pipeline, so the two cannot drift apart.
  • Every evaluation writes an audit row to Postgres whether or not it acted, dry runs included. Dry run is the default, so you watch a new rule before you trust it.
  • The local-delivery rule switches qualifying orders to ground, since we ship from in-state and ground arrives next day anyway, then applies its tag as the permanent already-processed marker. It leaves PO Box and military addresses alone, and it will not downgrade an order already on a paid expedited service to something slower than the customer bought.
  • It resolves the carrier account from the API and caches it, preferring the directly connected account over the reseller one so labels bill to the right place. A missing carrier, service or tag suppresses the switch and fails closed while the other rules carry on.
  • One-time eligibility gets claimed atomically before the external call, then finalised or released after, which stops a retry applying the same promotion twice.