feat: Family & Team Sub-Accounts with Scoped Permissions - #306
Merged
robertocarlous merged 1 commit intoJul 28, 2026
Merged
Conversation
Add SubAccount model with granular permissions (VIEW, DEPOSIT, WITHDRAW, MANAGE_STRATEGY) allowing a parent user to delegate limited access to a child user's account. Permissions are independent flags, not hierarchical. New middleware requireSubAccountPermission(permission) enforces delegation on deposit/withdraw routes without altering the existing self-access path. Delegated actions are attributed via actingAsUserId on Transaction and AgentLog for a complete audit trail visible to both parent and child. CRUD endpoints: - POST /api/v1/sub-accounts — create relationship with initial permissions - PATCH /api/v1/sub-accounts/:id/permissions — adjust granted permissions - DELETE /api/v1/sub-accounts/:id — soft-revoke (preserves audit trail) Security guarantees: - Self-access passes through untouched (no DB lookup) - Revocation takes effect on the very next request (no caching) - No chained sub-accounts (child cannot be a parent) - Denial paths return 403: missing permission, revoked, non-existent Closes Neurowealth#286
6 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.
Closes #286
Problem
The platform is single-user-per-wallet. A parent wanting to manage a child's savings, or a small team wanting a shared treasury, has no way to do that without sharing one account's full credentials — meaning full withdraw/strategy-change power, with no audit trail distinguishing who actually took an action.
Solution
Adds sub-accounts with scoped, revocable permissions.
Data Model
parentUserId,childUserId,permissions[],status(ACTIVE/REVOKED)VIEW,DEPOSIT,WITHDRAW,MANAGE_STRATEGY(independent flags, not hierarchical)TransactionandAgentLogfor delegated-action audit trailNew Middleware
requireSubAccountPermission(permission)— layered afterrequireAuth, checks for active sub-account relationship + required permission. Self-access passes through untouched.API Surface
/api/v1/sub-accounts/api/v1/sub-accounts/:id/permissions/api/v1/sub-accounts/:idIntegration Points
requireSubAccountPermission('DEPOSIT')/requireSubAccountPermission('WITHDRAW')actingAsUserIdSecurity Guarantees
req.auth.userId === targetUserId, middleware is a no-opDEPOSITdoes not implicitly grantVIEWTransaction.actingAsUserIdandAgentLog.actingAsUserIddistinguish delegated from self-actions, visible to both parent and childTests (27 new, 22 existing unaffected)
Files Changed
prisma/schema.prisma— SubAccount model, enums, actingAsUserId fieldsprisma/migrations/20260728000000_add_sub_accounts/— migration SQLsrc/middleware/subAccount.ts— new permission middlewaresrc/routes/sub-accounts.ts— CRUD endpointssrc/routes/deposit.ts,src/routes/withdraw.ts— middleware integrationsrc/controllers/transaction-controller.ts— delegation support + actingAsUserIdsrc/types/express.d.ts— type augmentationsrc/index.ts— route mountingdocs/openapi.yaml— schema + endpoint documentationtests/unit/middleware/subAccount.test.ts,tests/unit/routes/sub-accounts.test.ts— test suite