{"record":{"id":"9a92a23db29202de","repo":"RocketChat/Rocket.Chat","slug":"department-not-found","errorCode":"department-not-found","errorMessage":"department-not-found","messagePattern":"department-not-found","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"apps/meteor/server/lib/omnichannel/departmentsLib.ts","lineNumber":155,"sourceCode":"\tif (department?.enabled && !departmentDB?.enabled) {\n\t\tawait callbacks.run('livechat.afterDepartmentDisabled', departmentDB);\n\t\tvoid Apps.self?.triggerEvent(AppEvents.IPostLivechatDepartmentDisabled, { department: departmentDB });\n\t}\n\n\tif (departmentUnit) {\n\t\tawait callbacks.run('livechat.manageDepartmentUnit', { userId, departmentId: departmentDB._id, unitId: departmentUnit._id });\n\t}\n\n\treturn departmentDB;\n}\n\nexport async function archiveDepartment(_id: string) {\n\tconst department = await LivechatDepartment.findOneById<Pick<ILivechatDepartment, '_id' | 'businessHourId'>>(_id, {\n\t\tprojection: { _id: 1, businessHourId: 1 },\n\t});\n\n\tif (!department) {\n\t\tthrow new Error('department-not-found');\n\t}\n\n\tconst status = await LivechatDepartment.archiveDepartment(department._id);\n\tif (status.modifiedCount) {\n\t\tawait afterDepartmentArchived(department);\n\t}\n}\n\nexport async function unarchiveDepartment(_id: string) {\n\tconst department = await LivechatDepartment.findOneById<Pick<ILivechatDepartment, '_id'>>(_id, { projection: { _id: 1 } });\n\n\tif (!department) {\n\t\tthrow new Meteor.Error('department-not-found');\n\t}\n\n\tconst status = await LivechatDepartment.unarchiveDepartment(department._id);\n\tif (status.modifiedCount) {\n\t\tawait afterDepartmentUnarchived(department);","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/RocketChat/Rocket.Chat/blob/b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0/apps/meteor/server/lib/omnichannel/departmentsLib.ts#L137-L173","documentation":"Thrown by `archiveDepartment(_id)` (departmentsLib.ts:157) when `LivechatDepartment.findOneById` with projection `{ _id, businessHourId }` returns null. Note it is a plain `new Error('department-not-found')`, not a `Meteor.Error`, so there is no structured code/metadata — callers must match on the message.","triggerScenarios":"Archiving a department id that was already deleted (double action, stale table row), a typo'd id, or a REST/Meteor call racing another admin's delete.","commonSituations":"Two admins working the departments list; retry of an archive action after the first succeeded via a different path; frontend not refreshing after deletion.","solutions":["Reload the department list and confirm the id still exists; if gone, the goal is already achieved — treat as success","Guard with an existence check before calling archiveDepartment","If wrapping this in an API, catch the plain Error and map 'department-not-found' to a 404-style response"],"exampleFix":"// before\nawait archiveDepartment(maybeStaleId);\n\n// after\nconst dep = await LivechatDepartment.findOneById(maybeStaleId, { projection: { _id: 1 } });\nif (!dep) return; // already gone — nothing to archive\nawait archiveDepartment(maybeStaleId);","handlingStrategy":"validation","validationCode":"const dep = await LivechatDepartment.findOneById(_id, { projection: { _id: 1 } });\nif (!dep) return; // nothing to archive — treat as done\nawait archiveDepartment(_id);","typeGuard":"const isExistingDepartment = async (id: string): Promise<boolean> =>\n  (await LivechatDepartment.findOneById(id, { projections: { _id: 1 } } as any)) != null;","tryCatchPattern":"try {\n  await archiveDepartment(_id);\n} catch (e) {\n  // plain Error here — match on message, not a Meteor.Error code\n  if (e instanceof Error && e.message === 'department-not-found') {\n    // idempotent: already deleted elsewhere\n  }\n}","preventionTips":["Refresh the departments table before archive actions","Make archive idempotent in wrappers (missing id = success)","Remember this site throws plain Error, not Meteor.Error"],"tags":["omnichannel","departments","archive","not-found","livechat"],"backgroundTag":"entity-not-found","analyzedSha":"b2c16d5842cbe6b69b59bdf6fc5e5f1afcd1f0b0","analyzedAt":"2026-08-18T15:26:39.429Z","schemaVersion":2},"datasetVersion":"2026-08-27T19:17:21.184Z"}