{"record":{"id":"f57af31522618755","repo":"RocketChat/Rocket.Chat","slug":"unit-not-found","errorCode":"unit-not-found","errorMessage":"Unit not found","messagePattern":"Unit not found","errorType":"error_code","errorClass":"Meteor.Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/ee/server/lib/omnichannel/LivechatEnterprise.ts","lineNumber":63,"sourceCode":"\n\t\tif (!(await removeUserFromRolesAsync(user._id, ['livechat-monitor']))) {\n\t\t\treturn false;\n\t\t}\n\n\t\t// remove this monitor from any unit it is assigned to\n\t\tawait LivechatUnitMonitors.removeByMonitorId(user._id);\n\n\t\treturn true;\n\t},\n\n\tasync removeUnit(_id: string, userId: string) {\n\t\tcheck(_id, String);\n\n\t\tconst unitsFromUser = await getUnitsFromUser(userId);\n\n\t\tconst result = await LivechatUnit.removeByIdAndUnit(_id, unitsFromUser);\n\t\tif (!result.deletedCount) {\n\t\t\tthrow new Meteor.Error('unit-not-found', 'Unit not found', { method: 'livechat:removeUnit' });\n\t\t}\n\n\t\treturn result;\n\t},\n\n\tasync saveUnit(\n\t\t_id: string | null,\n\t\tunitData: Omit<IOmnichannelBusinessUnit, '_id'>,\n\t\tunitMonitors: { monitorId: string; username: string },\n\t\tunitDepartments: { departmentId: string }[],\n\t\tuserId: string,\n\t) {\n\t\tcheck(_id, Match.Maybe(String));\n\n\t\tcheck(unitData, {\n\t\t\tname: String,\n\t\t\tvisibility: String,\n\t\t\tenabled: Match.Optional(Boolean),","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/ee/server/lib/omnichannel/LivechatEnterprise.ts#L45-L81","documentation":"removeUnit deletes a business unit scoped to the units the executing user may see (getUnitsFromUser). If no document matched both the given _id and that scope, deletedCount is 0 and it throws unit-not-found for method livechat:removeUnit.","triggerScenarios":"Calling livechat:removeUnit with a wrong or stale unit id, or with a unit that exists but is outside the caller's allowed units.","commonSituations":"UI holding a stale list after the unit was removed in another session; admin with unit-scoped visibility trying to remove a unit they cannot see; double-submit.","solutions":["Re-fetch the current units list and retry with the existing id","Verify the caller has visibility of the unit (getUnitsFromUser scope) or use an unrestricted admin","If it was already deleted, no action is needed"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"const unit = await LivechatUnit.findOneById(_id);\nif (!unit) {\n\t// unit already gone; treat removal as done\n}\nawait LivechatEnterprise.removeUnit(_id, userId);","typeGuard":null,"tryCatchPattern":"try {\n\tawait LivechatEnterprise.removeUnit(_id, userId);\n} catch (err) {\n\tif ((err as Meteor.Error)?.error === 'unit-not-found') {\n\t\t// refresh list; if caller scope hid it, retry as unrestricted admin\n\t} else {\n\t\tthrow err;\n\t}\n}","preventionTips":["Refresh unit lists before destructive actions","Remember removal is scoped to the caller's visible units (getUnitsFromUser)","Make delete flows idempotent against stale ids"],"tags":["omnichannel","business-unit","not-found"],"backgroundTag":"resource-not-found","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"}