feat(investments): add transactional pool reservations - #233
Merged
Obiajulu-gif merged 1 commit intoAug 30, 2026
Merged
Conversation
7 tasks
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
Description
InvestmentReservationwith statesPENDING,RESERVED,SETTLED,EXPIRED,CANCELLED, andFAILEDand a user-scoped uniqueidempotencyKeyindex (models/InvestmentReservation.ts).investInPoolinlib/services/investments.service.tsto run the full flow inside a MongoDB transaction: create reservation, conditionally debit and hold wallet funds, require consent, createPoolInvestment(withreservationId), update pool totals atomically, create aTransactionledger entry, and mark the reservationSETTLEDon success.Idempotency-Keyreturn the original settled result or prevent duplicate processing; add duplicate-key and transient-transaction retry handling in the service.expireInvestmentReservations()and a runnable worker scriptscripts/expire-investment-reservations.tsthat selects onlyRESERVEDreservations and releases held funds (so settled reservations cannot be released).Idempotency-Keyfrom the API route by reading the header inapp/api/pools/[poolId]/invest/route.tsand map KYC/permission failures to403in the route error mapper.reservationIdonPoolInvestmentand add an index to prevent duplicate association; updateInvestmentPool/funding writes to incrementinvestorCountonly when the investor had no prior confirmed investment.docs/investment-reservations.mddescribing the state transitions and expiry-worker invocation and a small unit test for the reservation state machine and deterministic ownership calculation (__tests__/lib/services/investments.service.test.ts).Testing
npm run lintand observed 0 errors (3 unrelated warnings remain); success.npm run typecheckandnpm run typecheck:gate; both succeeded.npx vitest run __tests__/lib/services/investments.service.test.tsand they passed.npm run build(with CI env vars); the build completed successfully.__tests__/lib/api/openapi.test.ts(one failing test) that is unrelated to the implemented transactional reservation logic; full integration/load tests that exercise MongoDB transactions require a MongoDB replica set (not available in this environment) and should be run in CI or an integration environment to validate concurrent settlement, rollback, and expiry behaviour under load.Close #83