fix(company): enforce workspace membership and roles via CompanyMembership instead of accepted Invitations

Open
#38 0 comments 0 reactions 0 assignees View on GitHub

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

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

backend bug database
Problem Description

In apps/api/src/company/company.service.ts, workspace access and roles during multi-workspace switching rely on historical accepted Invitation records:

  1. Removed members can rejoin:
    removeTeamMember clears user.companyId = null and resets user.role = 'VIEWER', but does not delete or revoke the user's accepted Invitation rows. Because getUserAccounts and switchAccount authorize access to any company matching invitations: { some: { email: userEmail, acceptedAt: { not: null } } }, a removed member can still see the workspace and call POST /api/accounts/switch to rejoin.

  2. Demoted members can escalate their role:
    updateTeamMemberRole updates user.role, but leaves the accepted Invitation.role unchanged. When switchAccount runs, it sets targetRole = company.invitations[0].role, allowing a demoted member (e.g., ADMIN -> VIEWER) to restore their original ADMIN role via POST /api/accounts/switch.


Expected Behavior
  1. Workspace membership and per-workspace roles must have a single, authoritative source of truth (CompanyMembership).
  2. Invitation records must only represent pending/consumed invites and must never be used to authorize getUserAccounts or switchAccount or to determine a user's active role.
  3. 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)
  1. Add CompanyMembership model (packages/prisma/schema.prisma):

    • Create CompanyMembership with { id, userId, companyId, role, createdAt, updatedAt }, @@unique([userId, companyId]), and onDelete: Cascade relations to User and Company.
    • Backfill existing User.companyId + User.role and valid active Invitation relationships in the Prisma migration.
  2. Use CompanyMembership as the single source of truth (company.service.ts):

    • Accepting an invite / creating a company: Upsert a CompanyMembership row for (userId, companyId, role) when a user creates a workspace or accepts an Invitation.
    • getUserAccounts & switchAccount: Query CompanyMembership for userId (instead of invitations) to list accessible workspaces and resolve targetRole from membership.role.
    • updateTeamMemberRole: Update CompanyMembership.role (and user.role if user.companyId === companyId) inside a transaction.
    • removeTeamMember: Delete the CompanyMembership row for (userId, companyId) (and clear user.companyId / reset user.role if that was their active workspace).
Dominant language
TypeScript
Stars
2
Forks
1
Avg merge
6m
Merged PRs (30d)
27

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sumamakhan761/tryinstance

All issues in sumamakhan761/tryinstance

Similar issues

More TypeScript issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.