From 6818b34e90214ac3a9075e651c0d0e5f64618546 Mon Sep 17 00:00:00 2001 From: Rajesh Kumar Date: Wed, 6 Nov 2024 12:14:30 +0530 Subject: [PATCH] feat(plugin-cc): add relogin methods --- docs/samples/contact-center/app.js | 11 +++++++ packages/@webex/plugin-cc/src/cc.ts | 11 ++++++- .../@webex/plugin-cc/src/features/Agent.ts | 6 +++- .../plugin-cc/src/services/AgentService.ts | 33 +++++++++++++++++-- .../plugin-cc/src/services/constants.ts | 6 ++++ .../@webex/plugin-cc/src/services/types.ts | 2 ++ .../test/unit/spec/services/AgentService.ts | 32 ++++++++++++++++++ 7 files changed, 97 insertions(+), 4 deletions(-) diff --git a/docs/samples/contact-center/app.js b/docs/samples/contact-center/app.js index de62381ffe2..b03b89fbff1 100644 --- a/docs/samples/contact-center/app.js +++ b/docs/samples/contact-center/app.js @@ -151,6 +151,17 @@ async function handleAgentLogin(e) { function doAgentLogin() { webex.cc.stationLogin({teamId: teamsDropdown.value, loginOption: agentDeviceType, dialNumber: dialNumber.value}).then((response) => { console.log('Agent Logged in successfully', response); + + // Re-Login Agent after 5 seconds for testing purpose + setTimeout(async () => { + try { + const response = await webex.cc.stationReLogin(); + + console.log('Agent Re-Login successful', response); + } catch (error) { + console.log('Agent Re-Login failed', error); + } + }, 5000); } ).catch((error) => { console.log('Agent Login failed', error); diff --git a/packages/@webex/plugin-cc/src/cc.ts b/packages/@webex/plugin-cc/src/cc.ts index bce108881b9..85fd09f840a 100644 --- a/packages/@webex/plugin-cc/src/cc.ts +++ b/packages/@webex/plugin-cc/src/cc.ts @@ -12,7 +12,7 @@ import { import {READY, CC_FILE} from './constants'; import HttpRequest from './services/HttpRequest'; import WebRTCCalling from './WebRTCCalling'; -import {AgentLoginRequest} from './services/types'; +import {AgentLoginRequest, StationReLoginResponse} from './services/types'; import Agent from './features/Agent'; export default class ContactCenter extends WebexPlugin implements IContactCenter { @@ -127,4 +127,13 @@ export default class ContactCenter extends WebexPlugin implements IContactCenter return loginPromise; } + + /** + * Re-Login to the station. + * @returns {Promise} A promise that resolves when the re-login is successful. + * @throws Will throw an error if the re-login fails. + */ + public async stationReLogin(): Promise { + return this.agent.stationReLogin(); + } } diff --git a/packages/@webex/plugin-cc/src/features/Agent.ts b/packages/@webex/plugin-cc/src/features/Agent.ts index bedbba95c2d..8588ea04a1e 100644 --- a/packages/@webex/plugin-cc/src/features/Agent.ts +++ b/packages/@webex/plugin-cc/src/features/Agent.ts @@ -1,7 +1,7 @@ import {LoginOption, WebexSDK} from '../types'; import HttpRequest from '../services/HttpRequest'; import AgentService from '../services/AgentService'; -import {AgentLoginRequest} from '../services/types'; +import {AgentLoginRequest, StationReLoginResponse} from '../services/types'; import {StationLoginResponse} from './types'; import {AGENT, WEB_RTC_PREFIX} from '../services/constants'; @@ -37,4 +37,8 @@ export default class Agent { return loginResponse; } + + public async stationReLogin(): Promise { + return this.agentService.stationReLogin(); + } } diff --git a/packages/@webex/plugin-cc/src/services/AgentService.ts b/packages/@webex/plugin-cc/src/services/AgentService.ts index 5f5bd68cd45..02b62c362d1 100644 --- a/packages/@webex/plugin-cc/src/services/AgentService.ts +++ b/packages/@webex/plugin-cc/src/services/AgentService.ts @@ -1,7 +1,14 @@ import {WebexSDK, HTTP_METHODS} from '../types'; -import {LOGIN_API, WCC_API_GATEWAY} from './constants'; +import { + AGENT_RE_LOGIN_FAILED, + AGENT_RE_LOGIN_SUCCESS, + LOGIN_API, + RE_LOGIN_API, + STATION_RE_LOGIN, + WCC_API_GATEWAY, +} from './constants'; import HttpRequest from './HttpRequest'; -import {StationLoginSuccess, UserStationLogin} from './types'; +import {StationLoginSuccess, StationReLoginResponse, UserStationLogin} from './types'; export default class AgentService { private webex: WebexSDK; @@ -40,4 +47,26 @@ export default class AgentService { throw error; } } + + public async stationReLogin(): Promise { + try { + const response = await this.httpRequest.sendRequestWithEvent({ + service: WCC_API_GATEWAY, + resource: RE_LOGIN_API, + method: HTTP_METHODS.POST, + payload: {}, + eventType: STATION_RE_LOGIN, + success: [AGENT_RE_LOGIN_SUCCESS], + failure: [AGENT_RE_LOGIN_FAILED], + }); + + this.webex.logger.log('Station re-login success'); + + return response; + } catch (error) { + this.webex.logger.error(`Error during station re-login: ${error}`); + + throw error; + } + } } diff --git a/packages/@webex/plugin-cc/src/services/constants.ts b/packages/@webex/plugin-cc/src/services/constants.ts index 24198ef0e33..cea9c3793a5 100644 --- a/packages/@webex/plugin-cc/src/services/constants.ts +++ b/packages/@webex/plugin-cc/src/services/constants.ts @@ -7,3 +7,9 @@ export const AGENT = 'agent'; export const SUBSCRIBE_API = 'v1/notification/subscribe'; export const LOGIN_API = 'v1/agents/login'; export const WEB_RTC_PREFIX = 'webrtc-'; +export const RE_LOGIN_API = 'v1/agents/reload'; + +// Events +export const STATION_RE_LOGIN = 'StationReLogin'; +export const AGENT_RE_LOGIN_SUCCESS = 'AgentReloginSuccess'; +export const AGENT_RE_LOGIN_FAILED = 'AgentReloginFailed'; diff --git a/packages/@webex/plugin-cc/src/services/types.ts b/packages/@webex/plugin-cc/src/services/types.ts index 5e30818e533..f7d09986b36 100644 --- a/packages/@webex/plugin-cc/src/services/types.ts +++ b/packages/@webex/plugin-cc/src/services/types.ts @@ -229,3 +229,5 @@ export interface AuxCode { export interface ListAuxCodesResponse { data: AuxCode[]; } + +export type StationReLoginResponse = StationLoginSuccess | Error; diff --git a/packages/@webex/plugin-cc/test/unit/spec/services/AgentService.ts b/packages/@webex/plugin-cc/test/unit/spec/services/AgentService.ts index c3fd9f4db40..ea6eb6f1f18 100644 --- a/packages/@webex/plugin-cc/test/unit/spec/services/AgentService.ts +++ b/packages/@webex/plugin-cc/test/unit/spec/services/AgentService.ts @@ -6,6 +6,10 @@ import { AGENT, WCC_API_GATEWAY, LOGIN_API, + STATION_RE_LOGIN, + AGENT_RE_LOGIN_SUCCESS, + AGENT_RE_LOGIN_FAILED, + RE_LOGIN_API, } from '../../../../src/services/constants'; import HttpRequest from '../../../../src/services/HttpRequest'; import {LoginOption, HTTP_METHODS} from '../../../../src/types'; @@ -77,4 +81,32 @@ describe('plugin-cc AgentService tests', () => { expect(webex.logger.error).toHaveBeenCalledWith(`Error during station login: ${error}`); }); }); + + describe.only('AgentService.stationReLogin', () => { + it('should call sendRequestWithEvent with correct parameters', async () => { + httpRequestMock.sendRequestWithEvent.mockResolvedValue('response_data'); + + const result = await agentService.stationReLogin(); + + expect(httpRequestMock.sendRequestWithEvent).toHaveBeenCalledWith({ + service: WCC_API_GATEWAY, + resource: RE_LOGIN_API, + method: HTTP_METHODS.POST, + payload: {}, + eventType: STATION_RE_LOGIN, + success: [AGENT_RE_LOGIN_SUCCESS], + failure: [AGENT_RE_LOGIN_FAILED], + }); + + expect(result).toBe('response_data'); + }); + + it('should log error and reject the promise on failure', async () => { + const error = new Error('Network Error'); + httpRequestMock.sendRequestWithEvent.mockRejectedValue(error); + + await expect(agentService.stationReLogin()).rejects.toThrow('Network Error'); + expect(webex.logger.error).toHaveBeenCalledWith(`Error during station re-login: ${error}`); + }); + }); });