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

Aperta
#38 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
postgresql, typescript

Direzione di ricerca

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.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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).
Lingua principale
TypeScript
Stelle
2
Fork
1
Merge medio
6m
PR unite (30g)
27

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di sumamakhan761/tryinstance

Tutte le issue di sumamakhan761/tryinstance

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.