feat(domain): explicit state machines for Loan, Vehicle, and Investment - #160
Open
Mozez155 wants to merge 1 commit into
Open
feat(domain): explicit state machines for Loan, Vehicle, and Investment#160Mozez155 wants to merge 1 commit into
Mozez155 wants to merge 1 commit into
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
statusfield writes with typed, validated domain transition services for Loan, Vehicle, and InvestmentStateTransitionHistorymodel — immutable, indexed audit trail recording every state change with actor, reason, timestamp, and correlationIdstatus/fundingStatuswrites inPUT /api/vehicles/[id]; all status changes must go through/transitionendpointsscripts/check-state-machine-integrity.tsto detect and optionally repair impossible legacy combinations (--repairflag)Key invariants enforced
totalFunded >= requestedAmountANDdownPaymentMade = trueversionfield — stale transitions return409 CONCURRENCY_CONFLICTTest plan
Closes #85