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

Abierto
#38 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
35/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
postgresql, typescript

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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).
Lenguaje dominante
TypeScript
Estrellas
2
Forks
1
Merge medio
6 min
PR fusionados (30 d)
27

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de sumamakhan761/tryinstance

Todos los issues de sumamakhan761/tryinstance

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.