Skip to content

Commit

Permalink
feat(plugin-cc): add relogin methods
Browse files Browse the repository at this point in the history
  • Loading branch information
rarajes2 committed Nov 7, 2024
1 parent 4364a44 commit 6818b34
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 4 deletions.
11 changes: 11 additions & 0 deletions docs/samples/contact-center/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 10 additions & 1 deletion packages/@webex/plugin-cc/src/cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -127,4 +127,13 @@ export default class ContactCenter extends WebexPlugin implements IContactCenter

return loginPromise;
}

/**
* Re-Login to the station.
* @returns {Promise<StationLoginSuccess>} A promise that resolves when the re-login is successful.
* @throws Will throw an error if the re-login fails.
*/
public async stationReLogin(): Promise<StationReLoginResponse> {
return this.agent.stationReLogin();
}
}
6 changes: 5 additions & 1 deletion packages/@webex/plugin-cc/src/features/Agent.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -37,4 +37,8 @@ export default class Agent {

return loginResponse;
}

public async stationReLogin(): Promise<StationReLoginResponse> {
return this.agentService.stationReLogin();
}
}
33 changes: 31 additions & 2 deletions packages/@webex/plugin-cc/src/services/AgentService.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -40,4 +47,26 @@ export default class AgentService {
throw error;
}
}

public async stationReLogin(): Promise<StationReLoginResponse> {
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;
}
}
}
6 changes: 6 additions & 0 deletions packages/@webex/plugin-cc/src/services/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
2 changes: 2 additions & 0 deletions packages/@webex/plugin-cc/src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,5 @@ export interface AuxCode {
export interface ListAuxCodesResponse {
data: AuxCode[];
}

export type StationReLoginResponse = StationLoginSuccess | Error;
32 changes: 32 additions & 0 deletions packages/@webex/plugin-cc/test/unit/spec/services/AgentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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}`);
});
});
});

0 comments on commit 6818b34

Please sign in to comment.