Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Unit's numDepartments prop not being updated after a department is removed #34137

Merged
merged 6 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading