Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cc): getBuddyAgents() implementation #1

Closed
wants to merge 4 commits into from

Conversation

bhabalan
Copy link
Collaborator

@bhabalan bhabalan commented Oct 29, 2024

COMPLETES SPARK-567680

This pull request addresses

Fetching Buddy Agents

by making the following changes

introducing webex.cc.getBuddyAgents() API

Vidcast: https://app.vidcast.io/share/ae742d0a-126f-4e4c-90f0-0cf596473063

Pasted Graphic 1

Change Type

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Documentation update
  • Tooling change
  • Internal code refactor

The following scenarios where tested

Manually tested in samples page for fetching buddy agents in following scenarios:

  1. Station login not done
  2. Station login done, buddy agents available
  3. Station login done, buddy agents not available
    < ENUMERATE TESTS PERFORMED, WHETHER MANUAL OR AUTOMATED >

I certified that

  • I have read and followed contributing guidelines

  • I discussed changes with code owners prior to submitting this pull request

  • I have not skipped any automated checks

  • All existing and new tests passed

  • I have updated the documentation accordingly


Make sure to have followed the contributing guidelines before submitting.

Copy link

@Kesari3008 Kesari3008 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have done a quick review, please check comments

@@ -56,4 +70,31 @@ export default class AgentService {
return Promise.reject(error);
}
}

public async getBuddyAgents(options: GetBuddyAgentsOptions): Promise<BuddyAgentsResponse> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's call it data instead of options and please change the name from GetBuddyAgentsOptions to BuddyAgents

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is not addressed

resource: GET_BUDDY_AGENTS_API,
method: HTTP_METHODS.POST,
payload,
eventType: GET_BUDDY_AGENTS_EVENT,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not use GET and EVENT in the naming convention

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the GET, But EVENT is required to differentiate it from an other type named BUDDY_AGENTS which defines the param shape for getBuddyAgents()


export const AgentDesktopMessage = 'AgentDesktopMessage';

export const GET_BUDDY_AGENTS_EVENT = 'BuddyAgents';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These 2 are the same string values...we can keep single constant here

mediaType: BuddyAgentMediaType;
state?: BuddyAgentState;
}): Promise<BuddyAgentsResponse> {
const {agentProfileId, mediaType, state} = options;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we fetching it here and then passing it one by one. We can pass the data as it is. We are not doing anything with the data herfe

@@ -34,4 +34,25 @@ export default class Agent {
return Promise.reject(new Error('Error while performing agent login', error));
}
}

public async getBuddyAgents(options: {
agentProfileId?: string;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agentProfileId should not be optional

// Send the service request
const response = await this.webex.request({
const response: IHttpResponse = await this.webex.request({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to specify the type here? Based on the value assigned, type will be inferred automatically

expect(agentServiceMock.getBuddyAgents).toHaveBeenCalledWith(options);
});

it('should throw an error when getBuddyAgents fails', async () => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should have one testcase for error response as well

@@ -96,14 +96,24 @@ class HttpRequest {
try {
const {service, resource, method, payload, eventType, success, failure} = options;

// prune undefined values
Object.keys(payload).forEach((key) => payload[key] === undefined && delete payload[key]);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this code supposed to be doing ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
"someKey": "someValue",
"someOtherKey": undefined
}

will get transformed to

{
"someKey": "someValue"
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In which case we are going to pass a key with undefined value?

* state: BuddyAgentState.AVAILABLE,
* });
*/
public async getBuddyAgents({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have we created 3 functions for this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed 1:1

<fieldset id="buddyAgentsBox">
<legend>Buddy Agents</legend>
<button id="fetchBuddyAgents" class="btn btn-primary my-3" onclick="fetchBuddyAgents()">Get Buddy Agents</button>
<ul id="buddyAgentsList"></ul>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a dropdown to display the list of buddy agent list

buddyAgentsDropdown.add(option);
});
} catch (error) {
const buddyAgentsDropdown = document.getElementById('buddyAgentsDropdown');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add this to the top of file. Please remove from the try block as well.

});
} catch (error) {
const buddyAgentsDropdown = document.getElementById('buddyAgentsDropdown');
buddyAgentsDropdown.innerHTML = ''; // Clear previous options
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line can be moved to finally { } block. Can be removed from both try and catch

buddyAgentsDropdown.innerHTML = ''; // Clear previous options
const option = document.createElement('option');
option.text = `Failed to fetch buddy agents, ${error}`;
option.disabled = true;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the three lines can be written in simple way

From

const option = document.createElement('option');
option.text = `Failed to fetch buddy agents, ${error}`;
option.disabled = true;

To

const option = `<option disabled="true">Failed to fetch buddy agents, ${error}<option>`

Please check all other places as well.

channelName,
state,
});
this.$webex.logger.log('Get Buddy Agents API SUCCESS');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agent code is already logging the success message. This will add redundant log


public async getBuddyAgents(options: BuddyAgents): Promise<BuddyAgentsSuccess> {
try {
const data = await this.httpRequest.sendRequestWithEvent({
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion (Not to be fixed in this PR) - We should allow sendRequestWithEvent to accept a response type so that we don't have to typecast the response while returning.

cc - @Kesari3008, Ravi

@@ -96,14 +96,24 @@ class HttpRequest {
try {
const {service, resource, method, payload, eventType, success, failure} = options;

// prune undefined values
Object.keys(payload).forEach((key) => payload[key] === undefined && delete payload[key]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In which case we are going to pass a key with undefined value?

CHAT: 'chat',
SOCIAL: 'social',
EMAIL: 'email',
} as const;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we using as const ? What does it do?

For this we can create enum.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rarajes2 This is the new way of creating enum. Please refer to this document: https://confluence-eng-gpk2.cisco.com/conf/display/WTWC/How+to+use+enums+and+alternatives

EMAIL: 'email',
} as const;

export type CHANNEL_NAME = Enum<typeof CHANNEL_NAME>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have to create a separate type like this. The constant defined above can be converted to an enum, no need to create a const variable.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rarajes2 , Can you please provide an example on how to do this?

@arun3528 arun3528 force-pushed the AgentLogin branch 2 times, most recently from 2ece117 to ac5285e Compare October 31, 2024 13:30
* });
*/
public async getBuddyAgents(
options: GetBuddyAgentsRequest = {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still have Get in the name here

@@ -56,4 +64,28 @@ export default class AgentService {
return Promise.reject(error);
}
}

public async getBuddyAgents(options: BuddyAgents): Promise<BuddyAgentsSuccess> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return type for all the methods should be generic not just associated with success

@@ -6,4 +6,10 @@ export const AGENT = 'agent';
// CC GATEWAY API URL PATHS
export const SUBSCRIBE_API = 'v1/notification/subscribe';
export const LOGIN_API = 'v1/agents/login';
export const GET_BUDDY_AGENTS_API = 'v1/agents/buddyList';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove Get from here as well

@bhabalan bhabalan closed this Nov 12, 2024
@bhabalan
Copy link
Collaborator Author

Closed this in favor of rsarika#3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants