From 1be505991a1ee930426f049362c4a096a27905b9 Mon Sep 17 00:00:00 2001 From: Ravi Chandra Sekhar Sarika Date: Mon, 11 Nov 2024 23:55:06 +0530 Subject: [PATCH] fix: rebased code and fixed tests --- packages/@webex/plugin-cc/src/cc.ts | 6 +- .../@webex/plugin-cc/src/features/Agent.ts | 61 -------------- .../@webex/plugin-cc/src/features/types.ts | 13 --- packages/@webex/plugin-cc/src/types.ts | 1 + .../@webex/plugin-cc/test/unit/spec/cc.ts | 81 ++++++++++--------- .../test/unit/spec/features/AgentConfig.ts | 45 +++++++++++ 6 files changed, 91 insertions(+), 116 deletions(-) delete mode 100644 packages/@webex/plugin-cc/src/features/Agent.ts diff --git a/packages/@webex/plugin-cc/src/cc.ts b/packages/@webex/plugin-cc/src/cc.ts index b2d59bd9068..a0599bbefe2 100644 --- a/packages/@webex/plugin-cc/src/cc.ts +++ b/packages/@webex/plugin-cc/src/cc.ts @@ -1,7 +1,7 @@ import {WebexPlugin} from '@webex/webex-core'; import AgentConfig from './features/Agentconfig'; -import {SetStateResponse} from './features/types'; import { + SetStateResponse, CCPluginConfig, IContactCenter, WebexSDK, @@ -195,7 +195,7 @@ export default class ContactCenter extends WebexPlugin implements IContactCenter * @throws Error */ - public async setAgentStatus(data: StateChange): Promise { + public async setAgentState(data: StateChange): Promise { try { const agentStatusPromise = await this.services.agent.stateChange({data}); @@ -203,7 +203,7 @@ export default class ContactCenter extends WebexPlugin implements IContactCenter return Promise.resolve(agentStatusPromise); } catch (error) { - return Promise.reject(error); + throw getErrorDetails(error, 'setAgentState'); } } } diff --git a/packages/@webex/plugin-cc/src/features/Agent.ts b/packages/@webex/plugin-cc/src/features/Agent.ts deleted file mode 100644 index 1fcfdf605d0..00000000000 --- a/packages/@webex/plugin-cc/src/features/Agent.ts +++ /dev/null @@ -1,61 +0,0 @@ -import {LoginOption, WebexSDK} from '../types'; -import HttpRequest from '../services/HttpRequest'; -import AgentService from '../services/AgentService'; -import {AgentLoginRequest, StateChange} from '../services/types'; -import {StationLoginResponse} from './types'; -import {AGENT, WEB_RTC_PREFIX} from '../services/constants'; - -export default class Agent { - private webex: WebexSDK; - private agentService: AgentService; - private httpRequest: HttpRequest; - - constructor(webex: WebexSDK, httpRequest: HttpRequest) { - this.webex = webex; - this.httpRequest = httpRequest; - this.agentService = new AgentService(webex, this.httpRequest); - } - - private getDeviceId(loginOption: string, dialNumber: string): string { - if (loginOption === LoginOption.EXTENSION || loginOption === LoginOption.AGENT_DN) { - return dialNumber; - } - - return WEB_RTC_PREFIX + dialNumber; - } - - public async stationLogin(data: AgentLoginRequest): Promise { - try { - const loginResponse = await this.agentService.stationLogin({ - dialNumber: data.dialNumber, - teamId: data.teamId, - deviceType: data.loginOption, - isExtension: data.loginOption === LoginOption.EXTENSION, - deviceId: this.getDeviceId(data.loginOption, data.dialNumber), - roles: [AGENT], - }); - this.webex.logger.log('LOGIN API SUCCESS'); - - return { - data: loginResponse, - }; - } catch (error) { - return Promise.reject(error); - } - } - - public async setAgentStatus(data: StateChange) { - try { - const agentStatusResponse = await this.agentService.setAgentStatus({ - state: data.state, - auxCodeId: data.auxCodeId, - agentId: data.agentId, - lastStateChangeReason: data.lastStateChangeReason, - }); - - return {data: agentStatusResponse}; - } catch (error) { - return Promise.reject(error); - } - } -} diff --git a/packages/@webex/plugin-cc/src/features/types.ts b/packages/@webex/plugin-cc/src/features/types.ts index 8ff0528c982..40fab576405 100644 --- a/packages/@webex/plugin-cc/src/features/types.ts +++ b/packages/@webex/plugin-cc/src/features/types.ts @@ -1,5 +1,4 @@ import {WebexSDK} from '../types'; -import * as Agent from '../services/agent/types'; type Enum> = T[keyof T]; @@ -34,15 +33,3 @@ export type AgentConfigRequest = { */ orgId: string; }; - -/** - * Represents the response from setAgentStatus. - * - * @public - */ -export type SetStateResponse = { - data?: Agent.StateChangeSuccess; - error?: string; -}; - -export type StationLoginResponse = Agent.StationLoginSuccess | Error; diff --git a/packages/@webex/plugin-cc/src/types.ts b/packages/@webex/plugin-cc/src/types.ts index 36e2ac9c859..4fa82e018e8 100644 --- a/packages/@webex/plugin-cc/src/types.ts +++ b/packages/@webex/plugin-cc/src/types.ts @@ -299,3 +299,4 @@ export type RequestBody = export type StationLoginResponse = Agent.StationLoginSuccess | Error; export type StationLogoutResponse = Agent.LogoutSuccess | Error; export type StationReLoginResponse = Agent.ReloginSuccess | Error; +export type SetStateResponse = Agent.StateChangeSuccess | Error; diff --git a/packages/@webex/plugin-cc/test/unit/spec/cc.ts b/packages/@webex/plugin-cc/test/unit/spec/cc.ts index 8f08ef1b3d5..461f3c7ba1e 100644 --- a/packages/@webex/plugin-cc/test/unit/spec/cc.ts +++ b/packages/@webex/plugin-cc/test/unit/spec/cc.ts @@ -61,6 +61,7 @@ describe('webex.cc', () => { stationLogin: jest.fn(), logout: jest.fn(), reload: jest.fn(), + stateChange: jest.fn(), }, }; (Services.getInstance as jest.Mock).mockReturnValue(mockServicesInstance); @@ -312,8 +313,7 @@ describe('webex.cc', () => { describe('setAgentStatus', () => { it('should set agent status successfully when status is Available', async () => { - - const expectedPayload = { + const expectedPayload = { state: 'Available', auxCodeId: '0', agentId: '123', @@ -321,35 +321,18 @@ describe('webex.cc', () => { }; const setAgentStatusMock = jest - .spyOn(webex.cc.agent, 'setAgentStatus') + .spyOn(webex.cc.services.agent, 'stateChange') .mockResolvedValue(expectedPayload); - const result = await webex.cc.setAgentStatus(expectedPayload); + const result = await webex.cc.setAgentState(expectedPayload); - expect(setAgentStatusMock).toHaveBeenCalledWith(expectedPayload); + expect(setAgentStatusMock).toHaveBeenCalledWith({data: expectedPayload}); expect(result).toEqual(expectedPayload); expect(webex.logger.log).toHaveBeenCalledWith('file: cc: SET AGENT STATUS API SUCCESS'); }); - it('should handle error during setAgentStatus when status is Available', async () => { - - const expectedPayload = { - state: 'Available', - auxCodeId: '0', - agentId: '123', - lastStateChangeReason: 'Agent is available', - }; - - const error = new Error('Set status failed'); - jest.spyOn(webex.cc.agent, 'setAgentStatus').mockRejectedValue(error); - - await expect(webex.cc.setAgentStatus(expectedPayload)).rejects.toThrow(error); - expect(webex.logger.error).toHaveBeenCalledWith('SET AGENT STATUS FAILED', error); - }); - it('should set agent status successfully when status is Meeting', async () => { - - const expectedPayload = { + const expectedPayload = { state: 'Meeting', auxCodeId: '12345', agentId: '123', @@ -357,45 +340,65 @@ describe('webex.cc', () => { }; const setAgentStatusMock = jest - .spyOn(webex.cc.agent, 'setAgentStatus') + .spyOn(webex.cc.services.agent, 'stateChange') .mockResolvedValue(expectedPayload); - const result = await webex.cc.setAgentStatus(expectedPayload); + const result = await webex.cc.setAgentState(expectedPayload); - expect(setAgentStatusMock).toHaveBeenCalledWith(expectedPayload); + expect(setAgentStatusMock).toHaveBeenCalledWith({data: expectedPayload}); expect(result).toEqual(expectedPayload); expect(webex.logger.log).toHaveBeenCalledWith('file: cc: SET AGENT STATUS API SUCCESS'); }); it('should handle error during setAgentStatus when status is Meeting', async () => { - - const expectedPayload = { + const expectedPayload = { state: 'Meeting', auxCodeId: '12345', agentId: '123', lastStateChangeReason: 'Agent is in meeting', }; - const error = new Error('Set status failed'); - jest.spyOn(webex.cc.agent, 'setAgentStatus').mockRejectedValue(error); + const error = { + details: { + trackingId: '1234', + data: { + reason: 'missing status', + }, + }, + }; + jest.spyOn(webex.cc.services.agent, 'stateChange').mockRejectedValue(error); - await expect(webex.cc.setAgentStatus(expectedPayload)).rejects.toThrow(error); - expect(webex.logger.error).toHaveBeenCalledWith('SET AGENT STATUS FAILED', error); + await expect(webex.cc.setAgentState(expectedPayload)).rejects.toThrow( + error.details.data.reason + ); + expect(LoggerProxy.logger.error).toHaveBeenCalledWith( + `setAgentState failed with trackingId: ${error.details.trackingId}` + ); }); it('should handle invalid status', async () => { - - const invalidPayload = { + const invalidPayload = { state: 'invalid', auxCodeId: '12345', agentId: '123', lastStateChangeReason: 'invalid', }; - const error = new Error('Invalid status'); - jest.spyOn(webex.cc.agent, 'setAgentStatus').mockRejectedValue(error); + const error = { + details: { + trackingId: '1234', + data: { + reason: 'Invalid status', + }, + }, + }; + jest.spyOn(webex.cc.services.agent, 'stateChange').mockRejectedValue(error); - await expect(webex.cc.setAgentStatus(invalidPayload)).rejects.toThrow(error); - expect(webex.logger.error).toHaveBeenCalledWith('SET AGENT STATUS FAILED', error); + await expect(webex.cc.setAgentState(invalidPayload)).rejects.toThrow( + error.details.data.reason + ); + expect(LoggerProxy.logger.error).toHaveBeenCalledWith( + `setAgentState failed with trackingId: ${error.details.trackingId}` + ); }); }); -}); \ No newline at end of file +}); diff --git a/packages/@webex/plugin-cc/test/unit/spec/features/AgentConfig.ts b/packages/@webex/plugin-cc/test/unit/spec/features/AgentConfig.ts index 598f7f9eda2..6882cfdcb18 100644 --- a/packages/@webex/plugin-cc/test/unit/spec/features/AgentConfig.ts +++ b/packages/@webex/plugin-cc/test/unit/spec/features/AgentConfig.ts @@ -113,6 +113,15 @@ describe('AgentConfig', () => { name: 'testName', workTypeCode: WORK_TYPE_CODE.IDLE_CODE, }, + { + active: true, + defaultCode: true, + description: 'Agent is available to receive calls', + id: '0', + isSystemCode: false, + name: 'Available', + workTypeCode: 'IDLE_CODE', + }, ], }; @@ -221,6 +230,15 @@ describe('AgentConfig', () => { name: 'testName', workTypeCode: WORK_TYPE_CODE.IDLE_CODE, }, + { + active: true, + defaultCode: true, + description: 'Agent is available to receive calls', + id: '0', + isSystemCode: false, + name: 'Available', + workTypeCode: 'IDLE_CODE', + }, ], }; @@ -297,6 +315,15 @@ describe('AgentConfig', () => { name: 'testName', workTypeCode: WORK_TYPE_CODE.IDLE_CODE, }, + { + active: true, + defaultCode: true, + description: 'Agent is available to receive calls', + id: '0', + isSystemCode: false, + name: 'Available', + workTypeCode: 'IDLE_CODE', + }, ], }; @@ -373,6 +400,15 @@ describe('AgentConfig', () => { name: 'testName', workTypeCode: WORK_TYPE_CODE.IDLE_CODE, }, + { + active: true, + defaultCode: true, + description: 'Agent is available to receive calls', + id: '0', + isSystemCode: false, + name: 'Available', + workTypeCode: 'IDLE_CODE', + }, ], }; @@ -449,6 +485,15 @@ describe('AgentConfig', () => { name: 'testName', workTypeCode: WORK_TYPE_CODE.IDLE_CODE, }, + { + active: true, + defaultCode: true, + description: 'Agent is available to receive calls', + id: '0', + isSystemCode: false, + name: 'Available', + workTypeCode: 'IDLE_CODE', + }, ], };