Skip to content

Commit

Permalink
feat(contact-center): emit AgentStateChange event
Browse files Browse the repository at this point in the history
  • Loading branch information
mkesavan13 committed Dec 17, 2024
1 parent 7a04e01 commit 9b8e4c8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 4 deletions.
11 changes: 8 additions & 3 deletions docs/samples/contact-center/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,13 @@ function register() {
taskEvents.detail.task = task;

incomingCallListener.dispatchEvent(taskEvents);
})
});

webex.cc.on('AgentStateChange', (data) => {
if(data.type = 'AgentStateChangeSuccess') {
idleCodesDropdown.value = data.auxCodeId.trim() !== '' ? data.auxCodeId : 0;
}
});
}

function populateWrapupCodesDropdown() {
Expand Down Expand Up @@ -242,9 +248,8 @@ function doAgentLogin() {
}

async function handleAgentStatus(event) {
const select = document.getElementById('idleCodesDropdown');
auxCodeId = event.target.value;
agentStatus = select.options[select.selectedIndex].text;
agentStatus = idleCodesDropdown.options[idleCodesDropdown.selectedIndex].text;
}

function setAgentStatus() {
Expand Down
10 changes: 9 additions & 1 deletion packages/@webex/plugin-cc/src/cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import HttpRequest from './services/core/HttpRequest';
import LoggerProxy from './logger-proxy';
import {StateChange, Logout} from './services/agent/types';
import {getErrorDetails} from './services/core/Utils';
import {Profile, WelcomeEvent} from './services/config/types';
import {Profile, WelcomeEvent, CC_EVENTS} from './services/config/types';
import {AGENT_STATE_AVAILABLE} from './services/config/constants';
import {ConnectionLostDetails} from './services/core/websocket/types';
import TaskManager from './services/task/TaskManager';
Expand Down Expand Up @@ -284,6 +284,14 @@ export default class ContactCenter extends WebexPlugin implements IContactCenter
*/
private setupEventListeners() {
this.services.connectionService.on('connectionLost', this.handleConnectionLost.bind(this));
this.services.webSocketManager.on('message', (event) => {
const eventData = JSON.parse(event);

if (eventData.type === CC_EVENTS.AGENT_STATE_CHANGE) {
// @ts-ignore
this.emit(CC_EVENTS.AGENT_STATE_CHANGE, eventData.data);
}
});
}

/**
Expand Down
45 changes: 45 additions & 0 deletions packages/@webex/plugin-cc/test/unit/spec/cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {SetStateResponse} from '../../../src/types';
import {AGENT, WEB_RTC_PREFIX} from '../../../src/services/constants';
import Services from '../../../src/services';
import config from '../../../src/config';
import {CC_EVENTS} from '../../../src/services/config/types';
import LoggerProxy from '../../../src/logger-proxy';
import {CC_FILE} from '../../../src/constants';

Expand Down Expand Up @@ -66,6 +67,7 @@ describe('webex.cc', () => {

mockWebSocketManager = {
initWebSocket: jest.fn(),
on: jest.fn(),
};

mockContact = {
Expand Down Expand Up @@ -218,6 +220,7 @@ describe('webex.cc', () => {
lostConnectionRecoveryTimeout: 0,
};
const connectWebsocketSpy = jest.spyOn(webex.cc, 'connectWebsocket');
const setupEventListenersSpy = jest.spyOn(webex.cc, 'setupEventListeners');
const reloadSpy = jest.spyOn(webex.cc.services.agent, 'reload').mockResolvedValue({
data: {
auxCodeId: 'auxCodeId',
Expand All @@ -236,6 +239,7 @@ describe('webex.cc', () => {
const result = await webex.cc.register();

expect(connectWebsocketSpy).toHaveBeenCalled();
expect(setupEventListenersSpy).toHaveBeenCalled();
expect(mockWebSocketManager.initWebSocket).toHaveBeenCalledWith({
body: {
force: true,
Expand Down Expand Up @@ -832,4 +836,45 @@ describe('webex.cc', () => {
expect(webex.cc.agentConfig.defaultDn).toBe('67890');
});
});

describe('setupEventListeners()', () => {
let connectionServiceOnSpy, cCEmitSpy;

beforeEach(() => {
connectionServiceOnSpy = jest.spyOn(webex.cc.services.connectionService, 'on');
cCEmitSpy = jest.spyOn(webex.cc, 'emit');
});

it('should set up connectionLost and message event listener', () => {
webex.cc.setupEventListeners();

expect(connectionServiceOnSpy).toHaveBeenCalledWith(
'connectionLost',
expect.any(Function)
);

expect(mockWebSocketManager.on).toHaveBeenCalledWith(
'message',
expect.any(Function)
);
});

it('should emit AGENT_STATE_CHANGE when message event is received', () => {
webex.cc.setupEventListeners();

const messageCallback = mockWebSocketManager.on.mock.calls[0][1];
const eventData = {
type: CC_EVENTS.AGENT_STATE_CHANGE,
data: { some: 'data' },
};

// Simulate receiving a message event
messageCallback(JSON.stringify(eventData));

expect(cCEmitSpy).toHaveBeenCalledWith(
CC_EVENTS.AGENT_STATE_CHANGE,
eventData.data
);
});
});
});

0 comments on commit 9b8e4c8

Please sign in to comment.