Skip to content

Commit

Permalink
fix: Unit's numDepartments prop not being updated after a departmen…
Browse files Browse the repository at this point in the history
…t is removed (#34137)
  • Loading branch information
KevLehman authored Dec 11, 2024
1 parent e39db8d commit f11efb4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changeset/blue-rats-kick.md
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions apps/meteor/app/livechat/server/lib/departmentsLib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Pick<ILivechatDepartment, '_id' | 'businessHourId'>>(departmentId, {
projection: { _id: 1, businessHourId: 1 },
const department = await LivechatDepartment.findOneById<Pick<ILivechatDepartment, '_id' | 'businessHourId' | 'parentId'>>(departmentId, {
projection: { _id: 1, businessHourId: 1, parentId: 1 },
});
if (!department) {
throw new Error('error-department-not-found');
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ILivechatDepartment, '_id' | 'businessHourId'>;
department: AtLeast<ILivechatDepartment, '_id' | 'businessHourId' | 'parentId'>;
agentsId: ILivechatAgent['_id'][];
}) => {
if (!options?.department) {
Expand All @@ -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;
};
Expand Down
13 changes: 9 additions & 4 deletions apps/meteor/tests/end-to-end/api/livechat/14-units.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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', () => {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit f11efb4

Please sign in to comment.