Zum Hauptinhalt springen

Borrow Lifecycle State Machine

Borrowing an item involves two users (Lender and Borrower), physical handoffs, and verification steps. I modeled this flow as an explicit Finite State Machine.


Role-Based Transition Rules​

To prevent illegal state jumps or unauthorized status changes, every transition is validated against the user's role:

TransitionFrom StateTo StateAllowed RoleTrigger Action
AcceptREQUESTEDACCEPTEDLenderaccept()
RejectREQUESTEDREJECTEDLenderreject()
CancelREQUESTED / ACCEPTEDCANCELLEDBorrower / Lendercancel()
Confirm PickupACCEPTEDBORROWEDLenderconfirmPickup()
Confirm ReturnBORROWEDRETURNEDLenderconfirmReturn()
Automated Transitions

There are also automated system transitions! If a REQUESTED borrow isn't accepted within a few days, a scheduled cron job transitions it to EXPIRED. Similarly, an active BORROWED state moves to OVERDUE if the item isn't returned by the expected date.


Why an Explicit State Machine?​

  • Zero Invalid States: It is impossible for an item to jump directly from REQUESTED to RETURNED.
  • Role Verification: Attempting an action without the correct role (e.g. Borrower trying to accept their own request) throws an immediate ForbiddenException.
  • Auditability: Every state transition generates a timestamped audit log entry and publishes an event for user notifications.