This article was originally published on Jo4 Blog.

A publisher submits a bid. The brand counters. The publisher accepts. Then the brand tries to withdraw. Is that legal? What if two bids are active on the same campaign — which one "wins" for display?

If you're reaching for a bunch of if/else checks scattered across your codebase, stop. You need a state machine.

TL;DR

We built BidStateMachineService to manage the full lifecycle of bid negotiations: DRAFT → SUBMITTED → ACCEPTED/REJECTED/COUNTERED → WITHDRAWN/EXPIRED. Each transition is validated. Each state has a displayPriority so we can resolve multiple bids on the same campaign to a single canonical status. 1,782 lines of tests cover every transition path.