{"record":{"id":"87cd884acef3e0a7","repo":"gitroomhq/postiz-app","slug":"could-not-add-the-user-to-the-organization","errorCode":null,"errorMessage":"Could not add the user to the organization","messagePattern":"Could not add the user to the organization","errorType":"http","errorClass":"HttpException","httpStatus":400,"severity":"error","filePath":"libraries/nestjs-libraries/src/database/prisma/organizations/organization.service.ts","lineNumber":152,"sourceCode":"    const userOrgs = await this._organizationRepository.getOrgsByUserId(\n      user.id\n    );\n    if (userOrgs.some((current) => current.id === org.id)) {\n      throw new HttpException(\n        'User is already a member of this organization',\n        400\n      );\n    }\n\n    const added = await this._organizationRepository.addUserToOrg(\n      user.id,\n      makeId(5),\n      org.id,\n      body.role as 'USER' | 'ADMIN'\n    );\n\n    if (!added) {\n      throw new HttpException(\n        'Could not add the user to the organization',\n        400\n      );\n    }\n\n    return { added: true };\n  }\n\n  async deleteTeamMember(org: Organization, userId: string) {\n    const userOrgs = await this._organizationRepository.getOrgsByUserId(userId);\n    const findOrgToDelete = userOrgs.find((orgUser) => orgUser.id === org.id);\n    if (!findOrgToDelete) {\n      throw new Error('User is not part of this organization');\n    }\n\n    // @ts-ignore\n    const myRole = org.users[0].role;\n    const userRole = findOrgToDelete.users[0].role;","sourceCodeStart":134,"sourceCodeEnd":170,"githubUrl":"https://github.com/gitroomhq/postiz-app/blob/0f1647f7491a217d43eb5ae7a480484bdf0aff3e/libraries/nestjs-libraries/src/database/prisma/organizations/organization.service.ts#L134-L170","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Retry after re-checking membership (may be a concurrent-add race; the user may now exist)","Validate body.role is exactly 'USER' or 'ADMIN' before calling","Inspect Prisma logs for the underlying constraint/FK error and fix schema/data accordingly"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"if (!['USER','ADMIN'].includes(body.role)) { throw new Error('role must be USER or ADMIN'); }","typeGuard":"const isValidRole = (r?: string): r is 'USER' | 'ADMIN' => r === 'USER' || r === 'ADMIN';","tryCatchPattern":"try { await addTeamMember(orgId, email); } catch (e) { if (/Could not add/.test(e.message)) { await recheckMembership(); return retryOnce(); } throw e; }","preventionTips":["Validate role input at the DTO layer","Guard against concurrent duplicate invites with an in-flight lock"],"tags":["team-members","database-write","invite"],"backgroundTag":"database-write-failed","analyzedSha":"0f1647f7491a217d43eb5ae7a480484bdf0aff3e","analyzedAt":"2026-08-27T12:09:55.020Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}