Skip to content

Commit

Permalink
fix: updated response structure
Browse files Browse the repository at this point in the history
  • Loading branch information
rsarika committed Nov 5, 2024
1 parent 34eb9b1 commit 4364a44
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 93 deletions.
76 changes: 42 additions & 34 deletions packages/@webex/plugin-cc/src/cc.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import {WebexPlugin} from '@webex/webex-core';
import AgentConfig from './features/Agentconfig';
import {IAgentProfile, StationLoginResponse} from './features/types';
import {CCPluginConfig, IContactCenter, WebexSDK, SubscribeRequest, LoginOption} from './types';
import {
CCPluginConfig,
IContactCenter,
WebexSDK,
SubscribeRequest,
LoginOption,
WelcomeEvent,
} from './types';
import {READY, CC_FILE} from './constants';
import HttpRequest from './services/HttpRequest';
import WebRTCCalling from './WebRTCCalling';
Expand Down Expand Up @@ -67,20 +74,25 @@ export default class ContactCenter extends WebexPlugin implements IContactCenter
};

try {
const welcomeData = await this.httpRequest.subscribeNotifications({
body: connectionConfig,
});

const agentId = welcomeData.data?.agentId;
const agentConfig = new AgentConfig(agentId, this.$webex, this.httpRequest);
this.agentConfig = await agentConfig.getAgentProfile();
this.$webex.logger.log(`file: ${CC_FILE}: agent config is fetched successfully`);

return this.agentConfig;
return this.httpRequest
.subscribeNotifications({
body: connectionConfig,
})
.then(async (data: WelcomeEvent) => {
const agentId = data.agentId;
const agentConfig = new AgentConfig(agentId, this.$webex, this.httpRequest);
this.agentConfig = await agentConfig.getAgentProfile();
this.$webex.logger.log(`file: ${CC_FILE}: agent config is fetched successfully`);

return this.agentConfig;
})
.catch((error) => {
throw error;
});
} catch (error) {
this.$webex.logger.error(`file: ${CC_FILE}: Error during register: ${error}`);

return Promise.reject(new Error('Error while performing register`', error));
throw error;
}
}

Expand All @@ -91,32 +103,28 @@ export default class ContactCenter extends WebexPlugin implements IContactCenter
* @throws Error
*/
public async stationLogin(data: AgentLoginRequest): Promise<StationLoginResponse> {
try {
let callingSDKRegister: Promise<void> | null = null;
let callingSDKRegister: Promise<void> | null = null;

if (data.loginOption === LoginOption.BROWSER) {
this.webRTCCalling = new WebRTCCalling(this.$webex, {}); // TODO: add callingClientConfig
callingSDKRegister = this.webRTCCalling.registerWebCallingLine();
data.dialNumber = this.agentConfig.agentId; // replacing dialNumber with agentId for BROWSER case
}
if (data.loginOption === LoginOption.BROWSER) {
this.webRTCCalling = new WebRTCCalling(this.$webex, {}); // TODO: add callingClientConfig
callingSDKRegister = this.webRTCCalling.registerWebCallingLine();
data.dialNumber = this.agentConfig.agentId; // replacing dialNumber with agentId for BROWSER case
}

const loginPromise = this.agent.stationLogin({
...data,
dialNumber: data.dialNumber || this.agentConfig.agentId,
});
const loginPromise = this.agent.stationLogin({
...data,
dialNumber: data.dialNumber || this.agentConfig.agentId,
});

if (callingSDKRegister) {
// LoginOption.BROWSER case we have to wait until calling sdk also registered.
await Promise.all([callingSDKRegister, loginPromise]);
} else {
await loginPromise;
}
if (callingSDKRegister) {
// LoginOption.BROWSER case we have to wait until calling sdk also registered.
await Promise.all([callingSDKRegister, loginPromise]);
} else {
await loginPromise;
}

this.$webex.logger.log(`file: ${CC_FILE}: Station Login Success`);
this.$webex.logger.log(`file: ${CC_FILE}: Station Login Success`);

return Promise.resolve(loginPromise);
} catch (error) {
return Promise.reject(error);
}
return loginPromise;
}
}
26 changes: 10 additions & 16 deletions packages/@webex/plugin-cc/src/features/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,16 @@ export default class Agent {
}

public async stationLogin(data: AgentLoginRequest): Promise<StationLoginResponse> {
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('Station Login Success');
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('Station Login Success');

return {
data: loginResponse,
};
} catch (error) {
return Promise.reject(error);
}
return loginResponse;
}
}
6 changes: 1 addition & 5 deletions packages/@webex/plugin-cc/src/features/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,4 @@ export interface IAgentProfile {
wrapUpCodes: AuxCode[];
}

export type StationLoginResponse = {
data?: StationLoginSuccess;
error?: string;
// TODO: enhance this with more details like status code etc. after copy pasting code from agentx
};
export type StationLoginResponse = StationLoginSuccess | Error;
2 changes: 1 addition & 1 deletion packages/@webex/plugin-cc/src/services/AgentService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default class AgentService {
} catch (error) {
this.webex.logger.error(`Error during station login: ${error}`);

return Promise.reject(error);
throw error;
}
}
}
52 changes: 23 additions & 29 deletions packages/@webex/plugin-cc/src/services/HttpRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,39 +43,33 @@ class HttpRequest {
* If the Welcome event is not received, it rejects the promise
*/
public async subscribeNotifications(options: {body: SubscribeRequest}): Promise<WelcomeResponse> {
try {
const {body} = options;
const eventType = CC_EVENTS.WELCOME;
const subscribeResponse: SubscribeResponse = await this.webex.request({
service: WCC_API_GATEWAY,
resource: SUBSCRIBE_API,
method: HTTP_METHODS.POST,
body,
});
const {body} = options;
const eventType = CC_EVENTS.WELCOME;
const subscribeResponse: SubscribeResponse = await this.webex.request({
service: WCC_API_GATEWAY,
resource: SUBSCRIBE_API,
method: HTTP_METHODS.POST,
body,
});

return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
this.webex.logger.error('Timeout waiting for event');
this.eventHandlers.delete(eventType);
reject(new Error('Timeout waiting for event'));
}, WEBSOCKET_EVENT_TIMEOUT);
return new Promise((resolve, reject) => {
const timeoutId = setTimeout(() => {
this.webex.logger.error('Timeout waiting for event');
this.eventHandlers.delete(eventType);
reject(new Error('Timeout waiting for event'));
}, WEBSOCKET_EVENT_TIMEOUT);

// Store the event handler
this.eventHandlers.set(eventType, (data: WelcomeEvent) => {
clearTimeout(timeoutId);
this.eventHandlers.delete(eventType);
resolve({
data,
});
});
// Store the event handler
this.eventHandlers.set(eventType, (data: WelcomeEvent) => {
clearTimeout(timeoutId);
this.eventHandlers.delete(eventType);
resolve(data);
});

this.webSocket.connectWebSocket({
webSocketUrl: subscribeResponse.body.webSocketUrl,
});
this.webSocket.connectWebSocket({
webSocketUrl: subscribeResponse.body.webSocketUrl,
});
} catch (error) {
return Promise.reject(error);
}
});
}

/* This sends a request and waits for a websocket event
Expand Down
5 changes: 1 addition & 4 deletions packages/@webex/plugin-cc/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,7 @@ export type WelcomeEvent = {
agentId: string;
};

export type WelcomeResponse = {
data?: WelcomeEvent;
error?: string;
};
export type WelcomeResponse = WelcomeEvent | Error;

export type SubscribeRequest = {
force: boolean;
Expand Down
2 changes: 1 addition & 1 deletion packages/@webex/plugin-cc/test/unit/spec/cc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ describe('webex.cc', () => {
mockAgentConfig.getAgentProfile.mockResolvedValue(mockAgentProfile);

webex.cc.httpRequest.subscribeNotifications.mockResolvedValue({
data: {agentId: 'agent123'},
agentId: 'agent123',
});

const result = await webex.cc.register();
Expand Down
4 changes: 2 additions & 2 deletions packages/@webex/plugin-cc/test/unit/spec/features/Agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ describe('Agent', () => {
roles: [AGENT],
});
expect(webexMock.logger.log).toHaveBeenCalledWith('Station Login Success');
expect(response).toEqual({data: loginResponse});
expect(response).toEqual(loginResponse);
});

it('should handle failure when stationLogin is called', async () => {
Expand Down Expand Up @@ -157,6 +157,6 @@ describe('Agent', () => {
roles: [AGENT],
});
expect(webexMock.logger.log).toHaveBeenCalledWith('Station Login Success');
expect(response).toEqual({data: loginResponse});
expect(response).toEqual(loginResponse);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ describe('HttpRequest', () => {
}, 100);

const result = await httpRequest.subscribeNotifications({body: {}});
expect(result).toEqual({data: mockWelcomeEvent.data});
expect(result).toEqual(mockWelcomeEvent.data);
});

it(
Expand Down

0 comments on commit 4364a44

Please sign in to comment.