{"record":{"id":"f096e46617509a49","repo":"RocketChat/Rocket.Chat","slug":"error-adding-monitor-role","errorCode":"error-adding-monitor-role","errorMessage":"Error adding monitor role","messagePattern":"Error adding monitor role","errorType":"error_code","errorClass":"MeteorError","httpStatus":null,"severity":"error","filePath":"apps/meteor/ee/server/lib/omnichannel/LivechatEnterprise.ts","lineNumber":27,"sourceCode":"import { removeSLAFromRooms } from './SlaHelper';\nimport { callbacks } from '../../../../server/lib/callbacks';\nimport { addUserRolesAsync } from '../../../../server/lib/roles/addUserRoles';\nimport { removeUserFromRolesAsync } from '../../../../server/lib/roles/removeUserFromRoles';\n\nexport const LivechatEnterprise = {\n\tasync addMonitor(username: string) {\n\t\tconst user = await Users.findOneByUsername<Pick<IUser, '_id' | 'username' | 'roles'>>(username, {\n\t\t\tprojection: { _id: 1, username: 1, roles: 1 },\n\t\t});\n\n\t\tif (!user) {\n\t\t\tthrow new MeteorError('error-invalid-user', 'Invalid user', {\n\t\t\t\tmethod: 'livechat:addMonitor',\n\t\t\t});\n\t\t}\n\n\t\tif (!(await addUserRolesAsync(user._id, ['livechat-monitor']))) {\n\t\t\tthrow new MeteorError('error-adding-monitor-role', 'Error adding monitor role', {\n\t\t\t\tmethod: 'livechat:addMonitor',\n\t\t\t});\n\t\t}\n\n\t\treturn user;\n\t},\n\n\tasync removeMonitor(username: string) {\n\t\tconst user = await Users.findOneByUsername<Pick<IUser, '_id'>>(username, {\n\t\t\tprojection: { _id: 1 },\n\t\t});\n\n\t\tif (!user) {\n\t\t\tthrow new Meteor.Error('error-invalid-user', 'Invalid user', {\n\t\t\t\tmethod: 'livechat:removeMonitor',\n\t\t\t});\n\t\t}\n","sourceCodeStart":9,"sourceCodeEnd":45,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/ee/server/lib/omnichannel/LivechatEnterprise.ts#L9-L45","documentation":"The username lookup succeeded, but addUserRolesAsync(user._id, ['livechat-monitor']) returned false: the roles write did not acknowledge adding the livechat-monitor role to the user.","triggerScenarios":"The 'livechat-monitor' role is missing from the Roles collection (fresh or partially migrated instance), or the roles update failed at the database level.","commonSituations":"Database restored without seed roles; migrations skipped on an old instance; role removed manually; replica set write failures.","solutions":["Ensure default roles are seeded: run pending migrations or restart so 'livechat-monitor' is created","Inspect the Roles collection for 'livechat-monitor' and re-create it if absent","Check MongoDB logs/health for the failing role update"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"const roleExists = await Roles.findOneById('livechat-monitor');\nif (!roleExists) {\n\t// seed roles / run migrations before adding monitors\n}","typeGuard":null,"tryCatchPattern":"try {\n\tawait LivechatEnterprise.addMonitor(username);\n} catch (err) {\n\tif ((err as Meteor.Error)?.error === 'error-adding-monitor-role') {\n\t\t// the user existed but the role write failed: check Roles collection and Mongo health\n\t} else {\n\t\tthrow err;\n\t}\n}","preventionTips":["Run pending migrations on upgraded instances so default roles exist","Never delete built-in roles manually","Include a roles-seeded assertion in deployment health checks"],"tags":["omnichannel","roles","monitor","database"],"backgroundTag":"role-assignment-failed","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","contentChangedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}