diff --git a/.changeset/fluffy-weeks-float.md b/.changeset/fluffy-weeks-float.md new file mode 100644 index 0000000000000..cca4e2c037d11 --- /dev/null +++ b/.changeset/fluffy-weeks-float.md @@ -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 diff --git a/apps/meteor/ee/app/livechat-enterprise/server/business-hour/Multiple.ts b/apps/meteor/ee/app/livechat-enterprise/server/business-hour/Multiple.ts index 00f67d0d8fd00..45f19dc2eda77 100644 --- a/apps/meteor/ee/app/livechat-enterprise/server/business-hour/Multiple.ts +++ b/apps/meteor/ee/app/livechat-enterprise/server/business-hour/Multiple.ts @@ -157,6 +157,7 @@ export class MultipleBusinessHoursBehavior extends AbstractBusinessHourBehavior if (!department || !agentsId.length) { return options; } + return this.handleRemoveAgentsFromDepartments(department, agentsId, options); } @@ -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) { diff --git a/apps/meteor/tests/end-to-end/api/livechat/10-departments.ts b/apps/meteor/tests/end-to-end/api/livechat/10-departments.ts index 6b377674bc646..9a92b6332d92e 100644 --- a/apps/meteor/tests/end-to-end/api/livechat/10-departments.ts +++ b/apps/meteor/tests/end-to-end/api/livechat/10-departments.ts @@ -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'; @@ -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]', () => { @@ -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); + }); + }); });