diff --git a/.changeset/blue-rats-kick.md b/.changeset/blue-rats-kick.md new file mode 100644 index 000000000000..16b0bbc97e21 --- /dev/null +++ b/.changeset/blue-rats-kick.md @@ -0,0 +1,6 @@ +--- +"@rocket.chat/meteor": patch +"@rocket.chat/model-typings": patch +--- + +Fixes Unit's `numDepartments` property not being updated after a department is removed diff --git a/apps/meteor/app/livechat/server/lib/departmentsLib.ts b/apps/meteor/app/livechat/server/lib/departmentsLib.ts index 76c70cba49ba..fdc85105ab07 100644 --- a/apps/meteor/app/livechat/server/lib/departmentsLib.ts +++ b/apps/meteor/app/livechat/server/lib/departmentsLib.ts @@ -231,8 +231,8 @@ export async function setDepartmentForGuest({ token, department }: { token: stri export async function removeDepartment(departmentId: string) { livechatLogger.debug(`Removing department: ${departmentId}`); - const department = await LivechatDepartment.findOneById>(departmentId, { - projection: { _id: 1, businessHourId: 1 }, + const department = await LivechatDepartment.findOneById>(departmentId, { + projection: { _id: 1, businessHourId: 1, parentId: 1 }, }); if (!department) { throw new Error('error-department-not-found'); diff --git a/apps/meteor/ee/app/livechat-enterprise/server/hooks/afterRemoveDepartment.ts b/apps/meteor/ee/app/livechat-enterprise/server/hooks/afterRemoveDepartment.ts index 1bab201ab41b..64d92cca5f1e 100644 --- a/apps/meteor/ee/app/livechat-enterprise/server/hooks/afterRemoveDepartment.ts +++ b/apps/meteor/ee/app/livechat-enterprise/server/hooks/afterRemoveDepartment.ts @@ -1,11 +1,11 @@ import type { AtLeast, ILivechatAgent, ILivechatDepartment } from '@rocket.chat/core-typings'; -import { LivechatDepartment } from '@rocket.chat/models'; +import { LivechatDepartment, LivechatUnit } from '@rocket.chat/models'; import { callbacks } from '../../../../../lib/callbacks'; import { cbLogger } from '../lib/logger'; const afterRemoveDepartment = async (options: { - department: AtLeast; + department: AtLeast; agentsId: ILivechatAgent['_id'][]; }) => { if (!options?.department) { @@ -15,8 +15,14 @@ const afterRemoveDepartment = async (options: { const { department } = options; - cbLogger.debug(`Removing department from forward list: ${department._id}`); - await LivechatDepartment.removeDepartmentFromForwardListById(department._id); + cbLogger.debug({ + msg: 'Post removal actions on EE code for department', + department, + }); + await Promise.all([ + LivechatDepartment.removeDepartmentFromForwardListById(department._id), + ...(department.parentId ? [LivechatUnit.decrementDepartmentsCount(department.parentId)] : []), + ]); return options; }; diff --git a/apps/meteor/tests/end-to-end/api/livechat/14-units.ts b/apps/meteor/tests/end-to-end/api/livechat/14-units.ts index e0c079ece243..95c24f524486 100644 --- a/apps/meteor/tests/end-to-end/api/livechat/14-units.ts +++ b/apps/meteor/tests/end-to-end/api/livechat/14-units.ts @@ -482,11 +482,10 @@ import { IS_EE } from '../../../e2e/config/constants'; it('should succesfully create a department into an existing unit that a monitor supervises', async () => { const department = await createDepartment(undefined, undefined, undefined, undefined, { _id: unit._id }, monitor1Credentials); - // Deleting a department currently does not decrease its unit's counter. We must adjust this check when this is fixed const updatedUnit = await getUnit(unit._id); expect(updatedUnit).to.have.property('name', unit.name); expect(updatedUnit).to.have.property('numMonitors', 1); - expect(updatedUnit).to.have.property('numDepartments', 2); + expect(updatedUnit).to.have.property('numDepartments', 1); const fullDepartment = await getDepartmentById(department._id); expect(fullDepartment).to.have.property('parentId', unit._id); @@ -495,6 +494,13 @@ import { IS_EE } from '../../../e2e/config/constants'; await deleteDepartment(department._id); }); + + it('unit should end up with 0 departments after removing all of them', async () => { + const updatedUnit = await getUnit(unit._id); + expect(updatedUnit).to.have.property('name', unit.name); + expect(updatedUnit).to.have.property('numMonitors', 1); + expect(updatedUnit).to.have.property('numDepartments', 0); + }); }); describe('[PUT] livechat/department', () => { @@ -943,11 +949,10 @@ import { IS_EE } from '../../../e2e/config/constants'; }); testDepartmentId = testDepartment._id; - // Deleting a department currently does not decrease its unit's counter. We must adjust this check when this is fixed const updatedUnit = await getUnit(unit._id); expect(updatedUnit).to.have.property('name', unit.name); expect(updatedUnit).to.have.property('numMonitors', 1); - expect(updatedUnit).to.have.property('numDepartments', 3); + expect(updatedUnit).to.have.property('numDepartments', 2); const fullDepartment = await getDepartmentById(testDepartmentId); expect(fullDepartment).to.have.property('parentId', unit._id);