Skip to content

Commit

Permalink
fix units
Browse files Browse the repository at this point in the history
  • Loading branch information
KevLehman committed Dec 6, 2024
1 parent 5c9dba6 commit 22edb80
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 8 deletions.
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),
LivechatUnit.removeDepartmentFromUnit(department.parentId),
]);

return options;
};
Expand Down
11 changes: 11 additions & 0 deletions apps/meteor/ee/server/models/raw/LivechatUnit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,15 @@ export class LivechatUnitRaw extends BaseRaw<IOmnichannelBusinessUnit> implement
countUnits(): Promise<number> {
return this.col.countDocuments({ type: 'u' });
}

removeDepartmentFromUnit(unitId: string): Promise<UpdateResult> {
return this.updateOne(
{
_id: unitId,
},
{
$inc: { numDepartments: -1 },
},
);
}
}
10 changes: 8 additions & 2 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
1 change: 1 addition & 0 deletions packages/model-typings/src/models/ILivechatUnitModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ export interface ILivechatUnitModel extends IBaseModel<IOmnichannelBusinessUnit>
findByMonitorId(monitorId: string): Promise<string[]>;
findMonitoredDepartmentsByMonitorId(monitorId: string, includeDisabled: boolean): Promise<ILivechatDepartment[]>;
countUnits(): Promise<number>;
removeDepartmentFromUnit(unitId: string): Promise<UpdateResult>;
}

0 comments on commit 22edb80

Please sign in to comment.