{"success":true,"name":"model-status-as-a-state-machine-name-the-states-and-the-allo","description":"An entity has a status field (order, booking, membership) with rules living nowhere","category":"ai-brimind","path":"ai-brimind/model-status-as-a-state-machine-name-the-states-and-the-allo/SKILL.md","file":"SKILL.md","content":"---\nname: model-status-as-a-state-machine-name-the-states-and-the-allo\ndescription: \"An entity has a status field (order, booking, membership) with rules living nowhere\"\n---\n\n# Model Status as a State Machine: Name the States and the Allowed Moves Between Them\n\n## When to use\nYou are designing or growing an entity whose life passes through statuses (pending → paid → shipped, active/suspended/expired), and the allowed transitions currently exist only as assumptions scattered across endpoints — any code can set any status to anything.\n\n## Example request\nAn order jumps from 'cancelled' straight to 'shipped' because an update endpoint just writes whatever status arrives → nothing ever said that move is illegal; a state machine is exactly that missing list of legal moves. Refund logic assumes 'delivered' always came after 'paid' → but a manual DB edit created a 'delivered' unpaid order and the refund math explodes; enforced transitions are what make such assumptions safe to write. Booking statuses grow ad hoc — 'confirmed', 'CONFIRMED', 'validated' all meaning the same thing → every new screen invents matching logic; one enum, defined once, is the vocabulary everyone must speak. A simple on/off flag like 'emailVerified' → two states, no journey; state-machine ceremony is for entities with a LIFECYCLE, not for booleans.\n\n## Instructions\nAct as an architect who treats an entity's status like the plot of its life — a story with named chapters and legal page-turns — because the alternative is a status field that is just a free-text mood, and every bug report that starts with 'how did this order even GET into that state?' is the price. For any entity with a lifecycle, do this: (1) Draw the machine before coding it: list every state the entity can be in (the enum — pending, confirmed, cancelled, expired…), then list every ALLOWED transition as arrows (pending→confirmed, pending→cancelled, confirmed→cancelled-with-refund…), and just as importantly, notice what is absent: cancelled→shipped does not exist, and that absence is a business rule worth writing down. Name who/what triggers each arrow (user action, payment webhook, cron expiring things). (2) Make the states real in the schema: an enum constraint on the field (Mongoose enum, SQL CHECK/enum) so a typo status physically cannot be stored — the DB refuses vocabulary that is not in the dictionary. (3) Enforce the ARROWS in one place, not per endpoint: a single transition function or method — changeStatus(entity, newStatus) — that checks 'is this move legal from the current state?' and rejects illegal jumps with a clear error; endpoints call it instead of writing the field directly. Scattered raw writes to the status field are how illegal states are born, so treat direct assignment as a code smell. (4) Record the journey where it matters: for entities with disputes or money (orders, memberships), keep a status history (state, timestamp, by whom) — the current value tells you where it is, the history tells you what happened, and support questions are always about what happened. (5) Let downstream logic lean on the machine and say so: 'refund assumes paid happened' is safe ONLY because the machine makes unpaid-delivered impossible — that is the payoff of the discipline. Only commit the design once you can show the drawn diagram, point to the one function that guards transitions, and answer for any two states: 'can it move from A to B, and where is that enforced?' — if the answer is 'wherever someone remembered to check', the entity has moods, not a lifecycle.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #219\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}