Skip to content

fix: Unhandled rejection when removing an agent from a department when multiple business hours are enabled #35736

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

Merged
merged 5 commits into from
Apr 10, 2025
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
5 changes: 5 additions & 0 deletions .changeset/fluffy-weeks-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@rocket.chat/meteor": patch
---

Fixes an error causing the server to throw an "unhandled promise rejection" when removing an agent from a department without a business hour when using `Multiple` business hours
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export class MultipleBusinessHoursBehavior extends AbstractBusinessHourBehavior
if (!department || !agentsId.length) {
return options;
}

return this.handleRemoveAgentsFromDepartments(department, agentsId, options);
}

Expand Down Expand Up @@ -325,7 +326,7 @@ export class MultipleBusinessHoursBehavior extends AbstractBusinessHourBehavior

const [agentsWithDepartment, [agentsOfDepartment] = []] = await Promise.all([
LivechatDepartmentAgents.findByAgentIds(agentsIds, { projection: { agentId: 1 } }).toArray(),
LivechatDepartment.findAgentsByBusinessHourId(department.businessHourId).toArray(),
...[department?.businessHourId ? LivechatDepartment.findAgentsByBusinessHourId(department.businessHourId).toArray() : []],
]);

for (const agentId of agentsIds) {
Expand Down
48 changes: 45 additions & 3 deletions apps/meteor/tests/end-to-end/api/livechat/10-departments.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { faker } from '@faker-js/faker';
import type { ILivechatDepartment } from '@rocket.chat/core-typings';
import type { Credentials } from '@rocket.chat/api-client';
import type { ILivechatDepartment, IUser } from '@rocket.chat/core-typings';
import { expect } from 'chai';
import { before, describe, it, after } from 'mocha';
import type { Response } from 'supertest';
Expand All @@ -15,8 +16,9 @@ import {
getLivechatRoomInfo,
} from '../../../data/livechat/rooms';
import { createMonitor, createUnit } from '../../../data/livechat/units';
import { restorePermissionToRoles, updatePermission, updateSetting } from '../../../data/permissions.helper';
import { createUser, deleteUser } from '../../../data/users.helper';
import { restorePermissionToRoles, updateEESetting, updatePermission, updateSetting } from '../../../data/permissions.helper';
import { password } from '../../../data/user';
import { createUser, deleteUser, login } from '../../../data/users.helper';
import { IS_EE } from '../../../e2e/config/constants';

(IS_EE ? describe.skip : describe)('LIVECHAT - Departments[CE]', () => {
Expand Down Expand Up @@ -919,4 +921,44 @@ import { IS_EE } from '../../../e2e/config/constants';
.expect(200);
});
});

describe('With multiple bussines hours', () => {
before(async () =>
Promise.all([updateEESetting('Livechat_enable_business_hours', true), updateEESetting('Livechat_business_hour_type', 'Multiple')]),
);
after(async () =>
Promise.all([updateEESetting('Livechat_enable_business_hours', false), updateEESetting('Livechat_business_hour_type', 'Single')]),
);

let testUser: { user: IUser; credentials: Credentials };
let testDepartment: ILivechatDepartment;
before(async () => {
const user = await createUser();
await createAgent(user.username);
const credentials3 = await login(user.username, password);
await makeAgentAvailable(credentials3);

testUser = {
user,
credentials: credentials3,
};
});

before(async () => {
testDepartment = await createDepartment([{ agentId: testUser.user._id }], `${new Date().toISOString()}-department`, true);
});

after(async () => {
await Promise.all([deleteUser(testUser.user), deleteDepartment(testDepartment._id)]);
});

it('should allow to remove an agent from a department when multiple business hours are enabled', async () => {
const res = await request
.post(api(`livechat/department/${testDepartment._id}/agents`))
.set(credentials)
.send({ upsert: [], remove: [{ agentId: testUser.user._id, username: testUser.user.username }] })
.expect(200);
expect(res.body).to.have.property('success', true);
});
});
});
Loading