diff --git a/.changeset/empty-tables-change.md b/.changeset/empty-tables-change.md new file mode 100644 index 00000000000..c79b473ecbd --- /dev/null +++ b/.changeset/empty-tables-change.md @@ -0,0 +1,5 @@ +--- +"@rocket.chat/meteor": patch +--- + +Fixes the condition for firing the `LivechatSessionTaken` webhook event. diff --git a/apps/meteor/app/livechat/server/lib/hooks.ts b/apps/meteor/app/livechat/server/lib/hooks.ts index 1a942e77ce8..0431d022a18 100644 --- a/apps/meteor/app/livechat/server/lib/hooks.ts +++ b/apps/meteor/app/livechat/server/lib/hooks.ts @@ -114,7 +114,7 @@ export const onNewRoom = makeFunction(async (room: IOmnichannelRoom) => { export const afterTakeInquiry = makeFunction( async ({ room }: { inquiry: InquiryWithAgentInfo; room: IOmnichannelRoom; agent: { agentId: string; username: string } }) => { - if (settings.get('Livechat_webhook_on_chat_taken')) { + if (!settings.get('Livechat_webhook_on_chat_taken')) { return; } await sendToCRM('LivechatSessionTaken', room); diff --git a/apps/meteor/tests/end-to-end/api/livechat/06-integrations.ts b/apps/meteor/tests/end-to-end/api/livechat/06-integrations.ts index bcdee0666dc..9937af8d6ce 100644 --- a/apps/meteor/tests/end-to-end/api/livechat/06-integrations.ts +++ b/apps/meteor/tests/end-to-end/api/livechat/06-integrations.ts @@ -1,10 +1,19 @@ -import type { ISetting } from '@rocket.chat/core-typings'; +import type { IOmnichannelRoom, ISetting } from '@rocket.chat/core-typings'; import { expect } from 'chai'; import { after, before, describe, it } from 'mocha'; import type { Response } from 'supertest'; import { getCredentials, api, request, credentials } from '../../../data/api-data'; -import { deleteVisitor } from '../../../data/livechat/rooms'; +import { + closeOmnichannelRoom, + createAgent, + createLivechatRoom, + createVisitor, + deleteVisitor, + getLivechatRoomInfo, + startANewLivechatRoomAndTakeIt, +} from '../../../data/livechat/rooms'; +import { sleep } from '../../../data/livechat/utils'; import { updatePermission, updateSetting } from '../../../data/permissions.helper'; describe('LIVECHAT - Integrations', () => { @@ -138,7 +147,86 @@ describe('LIVECHAT - Integrations', () => { await request.post(api('livechat/webhook.test')).set(credentials).expect(400); }); }); + + describe('Webhook notifications', () => { + before(async () => { + await updateSetting('Livechat_webhookUrl', `${webhookUrl}/anything`); + await updateSetting('Livechat_Routing_Method', 'Manual_Selection'); + await createAgent(); + }); + after(async () => { + await updateSetting('Livechat_webhookUrl', ''); + await updateSetting('Livechat_Routing_Method', 'Auto_Selection'); + await updateSetting('Livechat_webhook_on_start', false); + await updateSetting('Livechat_webhook_on_close', false); + await updateSetting('Livechat_webhook_on_chat_taken', false); + await updateSetting('Livechat_webhook_on_chat_queued', false); + }); + + it('should send a notification on chat start', async () => { + await updateSetting('Livechat_webhook_on_start', true); + + const { room } = await startANewLivechatRoomAndTakeIt(); + await sleep(1000); + + const roomInfo = await getLivechatRoomInfo(room._id); + + expect(roomInfo.crmData).to.be.an('string'); + expect(JSON.parse(roomInfo.crmData as string)) + .to.have.property('json') + .that.has.property('type', 'LivechatSessionStart'); + await updateSetting('Livechat_webhook_on_start', false); + await closeOmnichannelRoom(room._id); + }); + it('should send a notification on chat taken', async () => { + await updateSetting('Livechat_webhook_on_chat_taken', true); + + const { room } = await startANewLivechatRoomAndTakeIt(); + await sleep(1000); + + const roomInfo = await getLivechatRoomInfo(room._id); + + expect(roomInfo.crmData).to.be.an('string'); + expect(JSON.parse(roomInfo.crmData as string)) + .to.have.property('json') + .that.has.property('type', 'LivechatSessionTaken'); + await updateSetting('Livechat_webhook_on_chat_taken', false); + await closeOmnichannelRoom(room._id); + }); + let room: IOmnichannelRoom; + it('should send a notification on chat queued', async () => { + await updateSetting('Livechat_webhook_on_chat_queued', true); + + const visitor = await createVisitor(); + room = await createLivechatRoom(visitor.token); + await sleep(1000); + + const roomInfo = await getLivechatRoomInfo(room._id); + + expect(roomInfo.crmData).to.be.an('string'); + expect(JSON.parse(roomInfo.crmData as string)) + .to.have.property('json') + .that.has.property('type', 'LivechatSessionQueued'); + await updateSetting('Livechat_webhook_on_chat_queued', false); + }); + it('should send a notification on chat close', async () => { + await updateSetting('Livechat_webhook_on_close', true); + + await closeOmnichannelRoom(room._id); + + await sleep(1000); + + const roomInfo = await getLivechatRoomInfo(room._id); + + expect(roomInfo.crmData).to.be.an('string'); + expect(JSON.parse(roomInfo.crmData as string)) + .to.have.property('json') + .that.has.property('type', 'LivechatSession'); + await updateSetting('Livechat_webhook_on_close', false); + }); + }); }); + describe('omnichannel/integrations', () => { describe('POST', () => { it('should update the integration settings if the required parameters are provided', async () => {