diff --git a/apps/meteor/app/livechat/server/lib/QueueManager.ts b/apps/meteor/app/livechat/server/lib/QueueManager.ts index 2075a5e9af97..e1ea79d84163 100644 --- a/apps/meteor/app/livechat/server/lib/QueueManager.ts +++ b/apps/meteor/app/livechat/server/lib/QueueManager.ts @@ -223,7 +223,7 @@ export class QueueManager { const name = (roomInfo?.fname as string) || guest.name || guest.username; - const room = await createLivechatRoom(rid, name, guest, roomInfo, { + const room = await createLivechatRoom(rid, name, { ...guest, ...(department && { department }) }, roomInfo, { ...extraData, ...(Boolean(customFields) && { customFields }), }); diff --git a/apps/meteor/tests/data/livechat/department.ts b/apps/meteor/tests/data/livechat/department.ts index 47d0f7f2b468..fa37e698c52c 100644 --- a/apps/meteor/tests/data/livechat/department.ts +++ b/apps/meteor/tests/data/livechat/department.ts @@ -38,7 +38,16 @@ const updateDepartment = async (departmentId: string, departmentData: Partial
  • +const createDepartmentWithMethod = ( + initialAgents: { agentId: string; username: string }[] = [], + { + allowReceiveForwardOffline = false, + fallbackForwardDepartment, + }: { + allowReceiveForwardOffline?: boolean; + fallbackForwardDepartment?: string; + } = {}, +) => new Promise((resolve, reject) => { void request .post(methodCall('livechat:saveDepartment')) @@ -56,6 +65,7 @@ const createDepartmentWithMethod = (initialAgents: { agentId: string; username: name: `new department ${Date.now()}`, description: 'created from api', allowReceiveForwardOffline, + fallbackForwardDepartment, }, initialAgents, ], @@ -126,8 +136,10 @@ export const addOrRemoveAgentFromDepartment = async ( export const createDepartmentWithAnOfflineAgent = async ({ allowReceiveForwardOffline = false, + fallbackForwardDepartment, }: { - allowReceiveForwardOffline: boolean; + allowReceiveForwardOffline?: boolean; + fallbackForwardDepartment?: string; }): Promise<{ department: ILivechatDepartment; agent: { @@ -137,7 +149,10 @@ export const createDepartmentWithAnOfflineAgent = async ({ }> => { const { user, credentials } = await createAnOfflineAgent(); - const department = (await createDepartmentWithMethod(undefined, allowReceiveForwardOffline)) as ILivechatDepartment; + const department = (await createDepartmentWithMethod(undefined, { + allowReceiveForwardOffline, + fallbackForwardDepartment, + })) as ILivechatDepartment; await addOrRemoveAgentFromDepartment(department._id, { agentId: user._id, username: user.username }, true); diff --git a/apps/meteor/tests/end-to-end/api/livechat/00-rooms.ts b/apps/meteor/tests/end-to-end/api/livechat/00-rooms.ts index 23f6d35d2acd..15f983927cf3 100644 --- a/apps/meteor/tests/end-to-end/api/livechat/00-rooms.ts +++ b/apps/meteor/tests/end-to-end/api/livechat/00-rooms.ts @@ -1001,6 +1001,45 @@ describe('LIVECHAT - rooms', () => { roomId = newRoom._id; visitorToken = newVisitor.token; }); + (IS_EE ? describe : describe.skip)('fallback department', () => { + let fallbackDepartment: Awaited>['department']; + let initialDepartment: Awaited>['department']; + let newVisitor: ILivechatVisitor; + let latestRoom: IOmnichannelRoom; + before(async () => { + await updateSetting('Livechat_Routing_Method', 'Auto_Selection'); + + fallbackDepartment = (await createDepartmentWithAnOnlineAgent()).department; + initialDepartment = ( + await createDepartmentWithAnOfflineAgent({ + fallbackForwardDepartment: fallbackDepartment._id, + }) + ).department; + + expect(initialDepartment.fallbackForwardDepartment).to.be.equal(fallbackDepartment._id); + }); + + after(async () => { + await Promise.all([ + deleteDepartment(fallbackDepartment._id), + deleteDepartment(initialDepartment._id), + deleteVisitor(newVisitor._id), + closeOmnichannelRoom(latestRoom._id), + ]); + }); + + it('should redirect chat to fallback department when all agents in the initial department are offline', async () => { + await updateSetting('Livechat_Routing_Method', 'Auto_Selection'); + + newVisitor = await createVisitor(initialDepartment._id); + const newRoom = await createLivechatRoom(newVisitor.token); + + latestRoom = await getLivechatRoomInfo(newRoom._id); + + expect(latestRoom).to.have.property('departmentId'); + expect(latestRoom.departmentId).to.be.equal(fallbackDepartment._id); + }); + }); (IS_EE ? it : it.skip)('system messages sent on transfer should be properly generated', async () => { const messagesList = await fetchMessages(roomId, visitorToken);