From 2a4c044767a3955e51c797e52f546aa97f27e39d Mon Sep 17 00:00:00 2001 From: Haoming Li Date: Sun, 24 Nov 2024 21:41:28 -0800 Subject: [PATCH] Clean up event subscriptions for agent session when transfer.succeeded is received over websocket --- package-lock.json | 4 ++-- src/core/chatController.js | 5 +++++ src/core/chatController.spec.js | 24 ++++++++++++++++++++++-- 3 files changed, 29 insertions(+), 4 deletions(-) diff --git a/package-lock.json b/package-lock.json index ca30e4b..6500e13 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "amazon-connect-chatjs", - "version": "3.0.1", + "version": "3.0.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "amazon-connect-chatjs", - "version": "3.0.1", + "version": "3.0.2", "license": "Apache-2.0", "dependencies": { "detect-browser": "5.3.0", diff --git a/src/core/chatController.js b/src/core/chatController.js index 75649f2..88b3ccf 100644 --- a/src/core/chatController.js +++ b/src/core/chatController.js @@ -322,6 +322,11 @@ class ChatController { }); this.breakConnection(); } + if (incomingData.ContentType === CONTENT_TYPE.transferSucceeded && this.sessionType !== SESSION_TYPES.CUSTOMER) { + // calls LpcConnectionHelper to remove message subscriptions for agent and supervisor sessions in Agent transfer use case + // Customer SIM: https://t.corp.amazon.com/P149853425/communication + this.breakConnection(); + } } catch (e) { this._sendInternalLogToServer(this.logger.error( "Error occured while handling message from Connection. eventData:", diff --git a/src/core/chatController.spec.js b/src/core/chatController.spec.js index 30fc614..89debda 100644 --- a/src/core/chatController.spec.js +++ b/src/core/chatController.spec.js @@ -41,14 +41,14 @@ describe("ChatController", () => { let startResponse; let endResponse; - function getChatController(shouldSendMessageReceipts = true) { + function getChatController(shouldSendMessageReceipts = true, sessionType = SESSION_TYPES.AGENT) { if (!shouldSendMessageReceipts) { GlobalConfig.removeFeatureFlag(FEATURES.MESSAGE_RECEIPTS_ENABLED); } GlobalConfig.updateThrottleTime(1000); return new ChatController({ - sessionType: SESSION_TYPES.AGENT, + sessionType: sessionType, chatDetails: chatDetails, chatClient: chatClient, websocketManager: websocketManager, @@ -121,6 +121,12 @@ describe("ChatController", () => { Type: EVENT, ContentType: CONTENT_TYPE.chatEnded })); + }, + $simulateConnectionTransferred: () => { + messageHandlers.forEach(f => f({ + Type: EVENT, + ContentType: CONTENT_TYPE.transferSucceeded + })); } }; }); @@ -538,6 +544,20 @@ describe("ChatController", () => { chatController.connectionHelper.$simulateConnectionEnding(); expect(breakConnectionSpy).toHaveBeenCalledTimes(1); }); + test("should call breakConnection for agent session method when transfer.succeeded event is received", async () => { + const chatController = getChatController(); + const breakConnectionSpy = jest.spyOn(chatController, "breakConnection"); + await chatController.connect(); + chatController.connectionHelper.$simulateConnectionTransferred(); + expect(breakConnectionSpy).toHaveBeenCalledTimes(1); + }); + test("should not call breakConnection for customer session method when transfer.succeeded event is received", async () => { + const chatController = getChatController(true, SESSION_TYPES.CUSTOMER); + const breakConnectionSpy = jest.spyOn(chatController, "breakConnection"); + await chatController.connect(); + chatController.connectionHelper.$simulateConnectionTransferred(); + expect(breakConnectionSpy).toHaveBeenCalledTimes(0); + }); test("should throttle sendEvent for MessageReceipts", done => { jest.useRealTimers(); const args = {