{"record":{"id":"466472756de0741a","repo":"RocketChat/Rocket.Chat","slug":"error-invalid-user-466472","errorCode":"error-invalid-user","errorMessage":"error-invalid-user","messagePattern":"error-invalid-user","errorType":"exception","errorClass":"Meteor.Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/lib/omnichannel/rooms.ts","lineNumber":240,"sourceCode":"\tif (!room.open) {\n\t\tthrow new Meteor.Error('room-closed', 'Room closed');\n\t}\n\n\tif (room.onHold) {\n\t\tthrow new Meteor.Error('error-room-onHold');\n\t}\n\n\tif (!(await Omnichannel.isWithinMACLimit(room))) {\n\t\tthrow new Meteor.Error('error-mac-limit-reached');\n\t}\n\n\tif (!room.servedBy) {\n\t\treturn false;\n\t}\n\n\tconst user = await Users.findOneById(room.servedBy._id);\n\tif (!user?._id) {\n\t\tthrow new Meteor.Error('error-invalid-user');\n\t}\n\n\tconst inquiry = await LivechatInquiry.findOne({ rid: room._id });\n\tif (!inquiry) {\n\t\treturn false;\n\t}\n\n\t// update inquiry's last message with room's last message to correctly display in the queue\n\t// because we stop updating the inquiry when it's been taken\n\tif (room.lastMessage) {\n\t\tawait LivechatInquiry.setLastMessageById(inquiry._id, room.lastMessage);\n\t}\n\n\tconst transferredBy = normalizeTransferredByData(user, room);\n\tlivechatLogger.debug({ msg: 'Transferring room by user', roomId: room._id, transferredBy: transferredBy._id });\n\tconst transferData = { scope: 'queue' as const, departmentId, transferredBy, ...overrideTransferData };\n\ttry {\n\t\tawait saveTransferHistory(room, transferData);","sourceCodeStart":222,"sourceCodeEnd":258,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/lib/omnichannel/rooms.ts#L222-L258","documentation":"After the state and MAC checks, returnRoomAsInquiry loads the serving agent with Users.findOneById(room.servedBy._id). If that user no longer exists in the users collection, it throws Meteor.Error('error-invalid-user') — the room references an agent that was deleted.","triggerScenarios":"Returning a room to the queue when the agent recorded in room.servedBy has been deleted from the database, leaving the reference dangling.","commonSituations":"Agent user hard-deleted while still serving chats; imported/restored database with orphan rooms; user-deletion cascades that skip livechat room cleanup.","solutions":["Restore or re-create the agent user, or clear/reassign room.servedBy before returning the room.","Audit for orphan rooms (servedBy._id missing from users) after any bulk user deletion.","Only delete users after their open omnichannel rooms are closed or transferred."],"exampleFix":"// before\nawait returnRoomAsInquiry(room, departmentId); // servedBy agent deleted -> error-invalid-user\n\n// after\nconst agent = room.servedBy ? await Users.findOneById(room.servedBy._id, { projection: { _id: 1 } }) : null;\nif (!agent) {\n\t// reassign or clear the stale agent reference first\n\tawait LivechatRooms.unsetAgentByRoomId(room._id);\n}\nawait returnRoomAsInquiry(room, departmentId);","handlingStrategy":"validation","validationCode":"const agent = room.servedBy\n\t? await Users.findOneById(room.servedBy._id, { projection: { _id: 1 } })\n\t: null;\nif (!agent) {\n\t// reassign or clear the stale servedBy reference before returning the room\n}","typeGuard":"const hasLiveServingAgent = async (room: Pick<IOmnichannelRoom, 'servedBy'>): Promise<boolean> =>\n\tBoolean(room.servedBy && (await Users.findOneById(room.servedBy._id, { projection: { _id: 1 } })));","tryCatchPattern":"try {\n\tawait returnRoomAsInquiry(room, departmentId);\n} catch (err) {\n\tif (err instanceof Meteor.Error && err.error === 'error-invalid-user') {\n\t\t// serving agent is gone: repair room.servedBy or close the orphan room\n\t\treturn;\n\t}\n\tthrow err;\n}","preventionTips":["Close or transfer open omnichannel rooms before deleting agent users.","Run periodic audits for rooms whose servedBy._id no longer resolves.","After DB restores/imports, validate referential integrity between rooms and users."],"tags":["omnichannel","livechat","agents","rooms","data-integrity"],"backgroundTag":"user-not-found","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-21T18:17:14.833Z"}