{"record":{"id":"0526d67a8fddddef","repo":"RocketChat/Rocket.Chat","slug":"error-invalid-user-0526d6","errorCode":null,"errorMessage":"error-invalid-user","messagePattern":"error-invalid-user","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/ee/server/api/v1/omnichannel/lib/inquiries.ts","lineNumber":18,"sourceCode":"import { LivechatInquiry, Users, OmnichannelServiceLevelAgreements } from '@rocket.chat/models';\n\nimport { updateRoomSLA } from './sla';\n\nexport async function setSLAToInquiry({ userId, roomId, sla }: { userId: string; roomId: string; sla?: string }): Promise<void> {\n\tconst inquiry = await LivechatInquiry.findOneByRoomId(roomId, { projection: { status: 1 } });\n\tif (!inquiry || inquiry.status !== 'queued') {\n\t\tthrow new Error('error-invalid-inquiry');\n\t}\n\n\tconst slaData = sla && (await OmnichannelServiceLevelAgreements.findOneByIdOrName(sla));\n\tif (!slaData) {\n\t\tthrow new Error('error-invalid-sla');\n\t}\n\n\tconst user = await Users.findOneById(userId, { projection: { _id: 1, username: 1, name: 1 } });\n\tif (!user?.username) {\n\t\tthrow new Error('error-invalid-user');\n\t}\n\n\tawait updateRoomSLA(\n\t\troomId,\n\t\t{\n\t\t\t_id: user._id,\n\t\t\tname: user.name || '',\n\t\t\tusername: user.username,\n\t\t},\n\t\tslaData,\n\t);\n}\n","sourceCodeStart":1,"sourceCodeEnd":31,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/f9d3ec372bb580fa8d036f94cf03925a478ef768/apps/meteor/ee/server/api/v1/omnichannel/lib/inquiries.ts#L1-L31","documentation":"Thrown by setSLAToInquiry when the user referenced by userId cannot be found or has no username. SLA assignment records the acting user (needs _id, name, username); a user without a username cannot be attributed. Plain Error, code 'error-invalid-user'.","triggerScenarios":"Calling setSLAToInquiry with a userId that does not exist in Users, or whose document has no username field; calling with a system/bot user that was created without a username; userId from a deleted account.","commonSituations":"Bot/service account created without a username; user deleted but userId still referenced in a queue job; cross-DB inconsistency; userId passed as the wrong type.","solutions":["Confirm the user exists and has a username via Users.findOneById before calling.","Backfill a username on the acting service account.","Use an actual agent userId (with a username) as the actor for SLA assignment.","Validate userId is a non-empty string matching an existing user."],"exampleFix":"// before: system actor without username\nawait setSLAToInquiry({ userId: systemAccountId, roomId, sla });\n\n// after: ensure the actor has a username\nconst actor = await Users.findOneById(systemAccountId, { projection: { username: 1 } });\nif (!actor?.username) throw new Error('Actor must have a username');\nawait setSLAToInquiry({ userId: actor._id, roomId, sla });","handlingStrategy":"validation","validationCode":"// Ensure the acting user has a username\nimport { Users } from '@rocket.chat/models';\nconst actor = await Users.findOneById(userId, { projection: { username: 1 } });\nif (!actor?.username) {\n  throw new Error('Actor user must exist and have a username');\n}\nawait setSLAToInquiry({ userId: actor._id, roomId, sla });","typeGuard":"function hasUsername(u: { username?: string } | null | undefined): u is { username: string } {\n  return !!u && typeof u.username === 'string' && u.username.length > 0;\n}","tryCatchPattern":"try {\n  await setSLAToInquiry({ userId, roomId, sla });\n} catch (e) {\n  if (e.message === 'error-invalid-user') {\n    // backfill username or switch to a real agent actor\n    return;\n  }\n  throw e;\n}","preventionTips":["Backfill usernames on service/bot accounts used as workflow actors.","Use a real agent userId (with a username) for attribution-bearing operations."],"tags":["omnichannel","sla","users","validation"],"backgroundTag":null,"analyzedSha":"f9d3ec372bb580fa8d036f94cf03925a478ef768","analyzedAt":"2026-08-12T19:07:17.372Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}