Skip to content

feat(domain): explicit state machines for Loan, Vehicle, and Investment - #160

Open
Mozez155 wants to merge 1 commit into
Chainmove:mainfrom
Mozez155:feat/domain-state-machines-85
Open

feat(domain): explicit state machines for Loan, Vehicle, and Investment#160
Mozez155 wants to merge 1 commit into
Chainmove:mainfrom
Mozez155:feat/domain-state-machines-85

Conversation

@Mozez155

Copy link
Copy Markdown

Summary

  • Replace ad-hoc status field writes with typed, validated domain transition services for Loan, Vehicle, and Investment
  • Define authoritative transition maps, actor-permission rules, and terminal-state guards for all three entities
  • Cascade related record updates (Vehicle status, Investment status) atomically in a single MongoDB transaction when a loan transitions
  • Add StateTransitionHistory model — immutable, indexed audit trail recording every state change with actor, reason, timestamp, and correlationId
  • Block direct status/fundingStatus writes in PUT /api/vehicles/[id]; all status changes must go through /transition endpoints
  • Add scripts/check-state-machine-integrity.ts to detect and optionally repair impossible legacy combinations (--repair flag)

Key invariants enforced

  • Loan cannot activate until totalFunded >= requestedAmount AND downPaymentMade = true
  • Vehicle cannot enter Maintenance while an Active loan exists
  • Terminal states (Rejected, Completed, Cancelled, Retired) are permanently locked
  • Drivers may only cancel loans in pre-active states
  • Optimistic concurrency via version field — stale transitions return 409 CONCURRENCY_CONFLICT

Test plan

  • Pure state machine unit tests (all allowed and forbidden transitions, actor rules, terminal-state guards)
  • Service integration tests against MongoDB (happy path lifecycle, forbidden actor, missing preconditions, stale version / concurrent transition)
  • Rollback test — side-effect failure aborts the full transaction and leaves loan status unchanged
  • Migration detection tests — integrity scanner correctly identifies all four impossible-combination scenarios
  • Transition history completeness — multi-hop lifecycle records every step in order
  • TypeScript clean in all domain and route files

Closes #85

…nt (Chainmove#85)

Replaces ad-hoc status writes with typed, validated domain transition
services that enforce actor permissions, preconditions, atomic side
effects, and an immutable audit trail.

- Define authoritative transition maps and actor rules for Loan
  (Pending → Under Review → Approved → Active → Completed),
  Vehicle (Available ↔ Reserved → Financed, Maintenance, Retired),
  and Investment (Funding → Active → Completed)
- Add LoanTransitionService, VehicleTransitionService, and
  InvestmentTransitionService as the single entry points for all
  status mutations; direct field writes are no longer supported
- Enforce invariants: loan activation requires full funding and
  down-payment; maintenance blocked while active loan exists;
  terminal states (Rejected, Completed, Cancelled, Retired) are
  locked; optimistic concurrency via version field
- Cascade side effects in one MongoDB transaction: loan activation
  sets vehicle to Financed and investments to Active; loan completion
  releases vehicle to Available and marks investments Completed
- Add StateTransitionHistory model (immutable, indexed by entity,
  correlationId, and timestamp) for queryable audit trail
- Block direct status/fundingStatus writes in PUT /api/vehicles/[id];
  all status changes must flow through /transition endpoints
- Add integrity check script to detect and repair impossible legacy
  combinations (--repair flag)
- Tests: pure state machine assertions, service integration tests
  (happy path, forbidden transitions, concurrency, rollback on side-effect
  failure, migration detection, history completeness)

Closes Chainmove#85
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[HARD][LOANS] Implement guarded loan and vehicle lifecycle state machines

1 participant