{"record":{"id":"1f2cd73f6272996a","repo":"RocketChat/Rocket.Chat","slug":"error-invalid-user-1f2cd7","errorCode":"error-invalid-user","errorMessage":"Invalid user","messagePattern":"Invalid user","errorType":"exception","errorClass":"MeteorError","httpStatus":null,"severity":"error","filePath":"apps/meteor/ee/server/lib/omnichannel/LivechatEnterprise.ts","lineNumber":21,"sourceCode":"import { Users, OmnichannelServiceLevelAgreements, LivechatTag, LivechatUnitMonitors, LivechatUnit } from '@rocket.chat/models';\nimport { getUnitsFromUser } from '@rocket.chat/omni-core-ee';\nimport { Match, check } from 'meteor/check';\nimport { Meteor } from 'meteor/meteor';\n\nimport { updateSLAInquiries } from './Helper';\nimport { 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","sourceCodeStart":3,"sourceCodeEnd":39,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/f9d3ec372bb580fa8d036f94cf03925a478ef768/apps/meteor/ee/server/lib/omnichannel/LivechatEnterprise.ts#L3-L39","documentation":"Thrown by LivechatEnterprise.addMonitor when no user is found for the given username (Users.findOneByUsername returns null). MeteorError code 'error-invalid-user', method 'livechat:addMonitor'. This is the user-existence guard before the addMonitor role assignment (error 238).","triggerScenarios":"Calling livechat:addMonitor with a username that does not exist; typo in username; user was deleted between the admin UI lookup and the API call.","commonSituations":"Admin types a non-existent username in the add-monitor dialog; import script passes an unnormalized username (case/whitespace); user deactivated/deleted.","solutions":["Confirm the username exists (case-sensitive lookup) before calling addMonitor.","Resolve/normalize the username from a user picker rather than free text.","If the user was deleted, restore or pick a different monitor."],"exampleFix":"// before\nawait LivechatEnterprise.addMonitor('jdoe'); // throws if missing\n\n// after\nconst u = await Users.findOneByUsername('jdoe');\nif (!u) throw new Meteor.Error('error-invalid-user', 'Pick a valid user');\nawait LivechatEnterprise.addMonitor(u.username);","handlingStrategy":"validation","validationCode":"async function findMonitorUser(username: string) {\n  const u = await Users.findOneByUsername(username, { projection: { _id: 1, username: 1, roles: 1 } });\n  if (!u) throw new Meteor.Error('error-invalid-user', 'User not found', { method: 'livechat:addMonitor' });\n  return u;\n}","typeGuard":"const isUsername = (s: unknown): s is string => typeof s === 'string' && s.trim().length > 0;","tryCatchPattern":"try { await LivechatEnterprise.addMonitor(username); } catch (e) {\n  if (e?.error === 'error-invalid-user') { /* repick from user list */ } else throw e;\n}","preventionTips":["Use a validated user picker rather than free-text username entry.","Normalize username case before submission."],"tags":["omnichannel","monitor","users","enterprise"],"backgroundTag":null,"analyzedSha":"f9d3ec372bb580fa8d036f94cf03925a478ef768","analyzedAt":"2026-08-12T19:07:17.372Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}