fix(company): enforce workspace membership and roles via CompanyMembership instead of accepted Invitations
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 35/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Tech stack
- postgresql, typescript
- Domain
- authentication, backend, databases
Research direction
Start with packages/prisma/schema.prisma and the listed methods in apps/api/src/company/company.service.ts; trace how workspace creation, invite acceptance, account listing, switching, role updates, and removal use membership data. Review the migration and existing tests for those flows. Done means CompanyMembership is authoritative, removed members cannot regain access, and demoted roles remain in effect after switching workspaces.
Written by the indexing model from the issue text.
Description
Problem Description
In apps/api/src/company/company.service.ts, workspace access and roles during multi-workspace switching rely on historical accepted Invitation records:
-
Removed members can rejoin:
removeTeamMemberclearsuser.companyId = nulland resetsuser.role = 'VIEWER', but does not delete or revoke the user's acceptedInvitationrows. BecausegetUserAccountsandswitchAccountauthorize access to any company matchinginvitations: { some: { email: userEmail, acceptedAt: { not: null } } }, a removed member can still see the workspace and callPOST /api/accounts/switchto rejoin. -
Demoted members can escalate their role:
updateTeamMemberRoleupdatesuser.role, but leaves the acceptedInvitation.roleunchanged. WhenswitchAccountruns, it setstargetRole = company.invitations[0].role, allowing a demoted member (e.g.,ADMIN -> VIEWER) to restore their originalADMINrole viaPOST /api/accounts/switch.
Expected Behavior
- Workspace membership and per-workspace roles must have a single, authoritative source of truth (
CompanyMembership). Invitationrecords must only represent pending/consumed invites and must never be used to authorizegetUserAccountsorswitchAccountor to determine a user's active role.- Removing a member must immediately revoke their workspace access across all endpoints; demoting a member must persist across workspace switches.
Where to Look
packages/prisma/schema.prisma(User,Company,Invitation)apps/api/src/company/company.service.ts(getUserAccounts,switchAccount,updateTeamMemberRole,removeTeamMember,acceptInvitation)
Solution / Implementation Guide (Recommended Enterprise Plan)
-
Add
CompanyMembershipmodel (packages/prisma/schema.prisma):- Create
CompanyMembershipwith{ id, userId, companyId, role, createdAt, updatedAt },@@unique([userId, companyId]), andonDelete: Cascaderelations toUserandCompany. - Backfill existing
User.companyId+User.roleand valid activeInvitationrelationships in the Prisma migration.
- Create
-
Use
CompanyMembershipas the single source of truth (company.service.ts):- Accepting an invite / creating a company: Upsert a
CompanyMembershiprow for(userId, companyId, role)when a user creates a workspace or accepts anInvitation. getUserAccounts&switchAccount: QueryCompanyMembershipforuserId(instead ofinvitations) to list accessible workspaces and resolvetargetRolefrommembership.role.updateTeamMemberRole: UpdateCompanyMembership.role(anduser.roleifuser.companyId === companyId) inside a transaction.removeTeamMember: Delete theCompanyMembershiprow for(userId, companyId)(and clearuser.companyId/ resetuser.roleif that was their active workspace).
- Accepting an invite / creating a company: Upsert a
- Dominant language
- TypeScript
- Stars
- 2
- Forks
- 1
- Avg merge
- 6m
- Merged PRs (30d)
- 27
Getting set up
- Ships a Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from sumamakhan761/tryinstance
-
backend good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
sumamakhan761/tryinstance#65 ·
Maintainers usually reply within 1 day
-
backend good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
sumamakhan761/tryinstance#64 ·
Maintainers usually reply within 1 day
-
backend good first issue
Difficulty 1/5 1-3 hours Newbie friendliness 88/100
sumamakhan761/tryinstance#63 ·
Maintainers usually reply within 1 day
-
backend good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
sumamakhan761/tryinstance#62 ·
Maintainers usually reply within 1 day
-
backend good first issue
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
sumamakhan761/tryinstance#60 ·
Maintainers usually reply within 1 day
All issues in sumamakhan761/tryinstance
Similar issues
-
prio:low scope:hud
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
OliverRomhagen/vrigstad-joyride#953 ·
Maintainers usually reply within 1 day
-
review-followup
Difficulty 1/5 Under an hour Newbie friendliness 92/100
griffinwork40/agent-afk#5446 ·
Maintainers usually reply within 1 day
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
kacper-szewczyk/Ticknest#346 ·
Maintainers usually reply within 1 day
-
echarts geo: a GeoJSON URL that returns 200 with a body that is not JSON is fetched on each refreshOpenbug plugin/echarts/geo
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 79/100
Yeachan-Heo/gajae-code#8230 ·
Maintainers usually reply within 1 day