gitroomhq/postiz-app · error · HttpException

Could not add the user to the organization

Error message

Could not add the user to the organization

What it means

Thrown as HTTP 400 when the repository's addUserToOrg returns a falsy result after attempting to insert the user-organization link (with a random inviteId, org id, and role). Indicates the DB write failed or was blocked without raising its own exception.

Source

Thrown at libraries/nestjs-libraries/src/database/prisma/organizations/organization.service.ts:152

    const userOrgs = await this._organizationRepository.getOrgsByUserId(
      user.id
    );
    if (userOrgs.some((current) => current.id === org.id)) {
      throw new HttpException(
        'User is already a member of this organization',
        400
      );
    }

    const added = await this._organizationRepository.addUserToOrg(
      user.id,
      makeId(5),
      org.id,
      body.role as 'USER' | 'ADMIN'
    );

    if (!added) {
      throw new HttpException(
        'Could not add the user to the organization',
        400
      );
    }

    return { added: true };
  }

  async deleteTeamMember(org: Organization, userId: string) {
    const userOrgs = await this._organizationRepository.getOrgsByUserId(userId);
    const findOrgToDelete = userOrgs.find((orgUser) => orgUser.id === org.id);
    if (!findOrgToDelete) {
      throw new Error('User is not part of this organization');
    }

    // @ts-ignore
    const myRole = org.users[0].role;
    const userRole = findOrgToDelete.users[0].role;

View on GitHub (pinned to 0f1647f749)

Solutions

  1. Retry after re-checking membership (may be a concurrent-add race; the user may now exist)
  2. Validate body.role is exactly 'USER' or 'ADMIN' before calling
  3. Inspect Prisma logs for the underlying constraint/FK error and fix schema/data accordingly
Defensive patterns

Strategy: retry

Validate before calling

if (!['USER','ADMIN'].includes(body.role)) { throw new Error('role must be USER or ADMIN'); }

Type guard

const isValidRole = (r?: string): r is 'USER' | 'ADMIN' => r === 'USER' || r === 'ADMIN';

Try / catch

try { await addTeamMember(orgId, email); } catch (e) { if (/Could not add/.test(e.message)) { await recheckMembership(); return retryOnce(); } throw e; }

Prevention

When it happens

Trigger: Unique-constraint violation on the user-org pair, prisma create returning null, invalid role value ('USER'/'ADMIN' cast from unvalidated body input), or FK errors from a deleted org/user between check and write.

Common situations: Race condition where the same user was added concurrently; role string other than USER/ADMIN sent by a custom client; DB constraints not matching the schema after migrations.

Related errors


AI-assisted analysis of gitroomhq/postiz-app@0f1647f749 (2026-08-27). Data as JSON: /api/errors/87cd884acef3e0a7. Report an issue: GitHub.