fix(vsla): loan repayment claim-before-posting + persisted close-cycle share-out plan (audit A1-4/A4-4/A4-5) - #54
Open
munisp wants to merge 8 commits into
Open
fix(vsla): loan repayment claim-before-posting + persisted close-cycle share-out plan (audit A1-4/A4-4/A4-5)#54munisp wants to merge 8 commits into
munisp wants to merge 8 commits into
Conversation
…lan (migration 050 + token)
…share-out plan repo
…loseCycle persisted plan
…nts + closeCycle crash-resume
…iring (undo drifted rewrite)
…ropped in prior push
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.
Stage 24 audit fixes: VSLA loans + close-cycle
Fixes three findings from the stage-24 audits:
repayLoanCAS-after-posting: on an overpayment conflict the loser posted its ledger entry first and only then hit the repayment-sum guard, leaving an orphaned posting with no repayment row. Repayment is now claimed before posting:VslaLoanRepository.claimRepaymentperforms an atomicrepaidKobo + amount <= totalDueKoboguard (pgUPDATE ... WHERE, in-memory equivalent), the ledger entry is posted, then the repayment row + status flip (recordRepayment) land; any failure rolls back the claim viarollbackRepaymentClaim. Same-idempotency-key replays adopt the prior entry without re-claiming; twin-key conflicts adopt the twin posting when the amount matches, else 409.closeCycleresume recomputed from the reduced pool: the payout plan (memberId, shareKobo, residual) is now persisted to a newvsla.share_out_planstable (migration050_vsla_close_cycle_plan.sql, guarded + re-apply-safe) before the OPEN→CLOSED CAS, so a crash/retry mid-payout pays the remaining members from the persisted plan instead of recomputing share-outs against the reduced pool balance. Concurrent closers adopt the persisted plan (uniquecycle_id+member_id), and replays no longer recompute from ledger state.Changes
database/repositories/vsla-carbon.repository.ts—claimRepayment/rollbackRepaymentClaimonVslaLoanRepository(interface + in-memory); newVslaShareOutPlanRepositoryport + in-memory impl.database/repositories/vsla-carbon.pg-repository.ts— pg implementations (atomic claim guard, share-out plan CRUD with unique-conflict adopt).modules/vsla-carbon/vsla-carbon.service.ts— claim-before-posting repayLoan; persisted-plan closeCycle with crash-resume + concurrent-closer adoption.database/persistence.tokens.ts/database/database.module.ts—VSLA_SHARE_OUT_PLAN_REPOSITORYtoken + provider/export wiring.infra/postgres/050_vsla_close_cycle_plan.sql—vsla.share_out_planstable.vsla-carbon.service.spec.ts— red specs from A1/A4 adopted as regression tests (7 new): no-orphan-posting on overpayment conflict, twin-key adoption, same-key crash-resume without double-claim, closeCycle crash mid-payout resumes from persisted plan, concurrent closers share one plan.Checks (local)
npm run typecheck(all workspaces): clean.apps/apivitest: 2464 passed / 0 failed (209 files); vsla-carbon specs 72/72 incl. new regression tests.lint:sql: 050 parses clean.