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

fix(contacts): incorporate dss into contact resolution correctly #3617

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions packages/calling/src/Contacts/ContactsClient.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
mockContactResponseBodyOne,
mockCountry,
mockDisplayNameOne,
mockDSSResponse,
mockEmail,
mockFirstName,
mockLastName,
Expand Down Expand Up @@ -114,8 +113,8 @@ describe('ContactClient Tests', () => {
mockNumber2,
mockSipAddress,
mockTitle,
mockNumber2,
mockSipAddress,
// mockNumber2,
// mockSipAddress,
mockGroupName,
],
},
Expand Down Expand Up @@ -202,13 +201,13 @@ describe('ContactClient Tests', () => {
codeObj.decryptTextList.forEach((text) => {
webex.internal.encryption.decryptText.mockResolvedValueOnce(text);
});
webex.internal.dss.lookup.mockResolvedValueOnce(mockDSSResponse);
} else {
respPayload['message'] = FAILURE_MESSAGE;
respPayload['data'] = codeObj.payloadData;
webex.request.mockRejectedValueOnce(respPayload);
}

contactClient['fetchContactFromDSS'] = jest.fn();
const contactsResponse = await contactClient.getContacts();

expect(webex.request).toBeCalledOnceWith({
Expand Down Expand Up @@ -581,7 +580,7 @@ describe('ContactClient Tests', () => {
expect(res.statusCode).toEqual(400);
expect(res.data.error).toEqual('contactId is required for contactType:CLOUD.');

webex.internal.dss.lookup.mockResolvedValueOnce(mockDSSResponse);
// webex.internal.dss.lookup.mockResolvedValueOnce(mockDSSResponse);
contact.contactId = mockContactResponse.contactId;

res = await contactClient.createContact(contact);
Expand Down
173 changes: 96 additions & 77 deletions packages/calling/src/Contacts/ContactsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ export class ContactsClient implements IContacts {

private webex: WebexSDK;

private groups: ContactGroup[] | undefined;
private groups: ContactGroup[];

private contacts: Contact[] | undefined;
private contacts: Contact[];

private defaultGroupId: string;

Expand All @@ -65,8 +65,8 @@ export class ContactsClient implements IContacts {
this.webex = this.sdkConnector.getWebex();

this.encryptionKeyUrl = '';
this.groups = undefined;
this.contacts = undefined;
this.groups = [];
this.contacts = [];
this.defaultGroupId = '';

log.setLogger(logger.level, CONTACTS_FILE);
Expand Down Expand Up @@ -252,70 +252,91 @@ export class ContactsClient implements IContacts {
/**
* Fetches contacts from DSS.
*/
private async fetchContactFromDSS(contactsDataMap: ContactIdContactInfo): Promise<Contact[]> {
const contactList = [];
const dssResult = await this.webex.internal.dss.lookup({ids: Object.keys(contactsDataMap)});

for (let i = 0; i < dssResult.length; i += 1) {
const contact = dssResult[i];
const contactId = contact.identity;
const {displayName, emails, phoneNumbers, sipAddresses, photos} = contact;
const {department, firstName, identityManager, jobTitle, lastName} = contact.additionalInfo;
const manager =
identityManager && identityManager.displayName ? identityManager.displayName : undefined;
const {contactType, avatarUrlDomain, encryptionKeyUrl, ownerId, groups} =
contactsDataMap[contactId];
let avatarURL = '';

if (photos.length) {
avatarURL = photos[0].value;
}
private async fetchContactFromDSS(contactsDataMap: ContactIdContactInfo): Promise<void> {
// const contactList = [];
// console.debug('sreenara contacts', this.contacts);
return new Promise<void>((resolve, reject) => {
const processSearchResult = async (payload: any) => {
// console.debug('sreenara payload', payload);
const {lookupResult} = payload.data;
// const contacts = [];

for (let i = 0; i < lookupResult.entities.length; i += 1) {
const contact = lookupResult.entities[i];
const {displayName, emails, phoneNumbers, sipAddresses, photos} = contact;
const {department, firstName, identityManager, jobTitle, lastName} =
contact.additionalInfo;
const manager =
identityManager && identityManager.displayName
? identityManager.displayName
: undefined;
const {contactType, avatarUrlDomain, encryptionKeyUrl, ownerId, groups} =
contactsDataMap[contact.identity];
let avatarURL = '';

if (photos.length) {
avatarURL = photos[0].value;
}

const addedPhoneNumbers = contactsDataMap[contactId].phoneNumbers;
const addedPhoneNumbers = contactsDataMap[contact.identity].phoneNumbers;

if (addedPhoneNumbers) {
const decryptedPhoneNumbers = await this.decryptContactDetail(
encryptionKeyUrl,
addedPhoneNumbers
);
if (addedPhoneNumbers) {
const decryptedPhoneNumbers = await this.decryptContactDetail(
encryptionKeyUrl,
addedPhoneNumbers
);

decryptedPhoneNumbers.forEach((number) => phoneNumbers.push(number));
}
decryptedPhoneNumbers.forEach((number) => phoneNumbers.push(number));
}

const addedSipAddresses = contactsDataMap[contactId].sipAddresses;
const addedSipAddresses = contactsDataMap[contact.identity].sipAddresses;

if (addedSipAddresses) {
const decryptedSipAddresses = await this.decryptContactDetail(
encryptionKeyUrl,
addedSipAddresses
);
if (addedSipAddresses) {
const decryptedSipAddresses = await this.decryptContactDetail(
encryptionKeyUrl,
addedSipAddresses
);

decryptedSipAddresses.forEach((address) => sipAddresses.push(address));
}
decryptedSipAddresses.forEach((address) => sipAddresses.push(address));
}

const cloudContact = {
avatarUrlDomain,
avatarURL,
contactId,
contactType,
department,
displayName,
emails,
encryptionKeyUrl,
firstName,
groups,
lastName,
manager,
ownerId,
phoneNumbers,
sipAddresses,
title: jobTitle,
};
const cloudContact = {
avatarUrlDomain,
avatarURL,
contactId: contact.identity,
contactType,
department,
displayName,
emails,
encryptionKeyUrl,
firstName,
groups,
lastName,
manager,
ownerId,
phoneNumbers,
sipAddresses,
title: jobTitle,
};

// console.debug('sreenara in for loop', this.contacts);
this.contacts.push(cloudContact);
}

contactList.push(cloudContact);
}
this.webex.internal.mercury.off('event:directory.lookup');
// console.debug('sreenara just before resolve', this.contacts);
resolve();
};

return contactList;
try {
this.webex.internal.mercury.on('event:directory.lookup', processSearchResult);
const contactsToResolve = Object.keys(contactsDataMap);
contactsToResolve.map((contact) => this.webex.internal.dss.lookup({id: contact}));
} catch (error) {
this.webex.internal.mercury.off('event:directory.lookup');
reject(error);
}
});
}

/**
Expand All @@ -327,7 +348,7 @@ export class ContactsClient implements IContacts {
method: 'getContacts',
};

const contactList: Contact[] = [];
// const contactList: Contact[] = [];
const contactsDataMap: ContactIdContactInfo = {};

try {
Expand All @@ -351,16 +372,14 @@ export class ContactsClient implements IContacts {
if (contact.contactType === ContactType.CUSTOM) {
const decryptedContact = await this.decryptContact(contact);

contactList.push(decryptedContact);
this.contacts.push(decryptedContact);
} else if (contact.contactType === ContactType.CLOUD && contact.contactId) {
contactsDataMap[contact.contactId] = contact;
}
}

if (Object.keys(contactsDataMap).length) {
const cloudContacts = await this.fetchContactFromDSS(contactsDataMap);

contactList.push(...cloudContacts);
await this.fetchContactFromDSS(contactsDataMap);
}

await Promise.all(
Expand All @@ -373,11 +392,11 @@ export class ContactsClient implements IContacts {
);

this.groups = groups;
this.contacts = contactList;
// this.contacts = contactList;
const contactResponse: ContactResponse = {
statusCode: Number(response[STATUS_CODE]),
data: {
contacts: contactList,
contacts: this.contacts,
groups,
},
message: SUCCESS_MESSAGE,
Expand Down Expand Up @@ -694,17 +713,17 @@ export class ContactsClient implements IContacts {
message: SUCCESS_MESSAGE,
};

if (contact.contactType === ContactType.CLOUD) {
const decryptedContacts = await this.fetchContactFromDSS(
Object.fromEntries([[newContact.contactId, newContact]]) as ContactIdContactInfo
);

if (decryptedContacts.length && decryptedContacts[0]) {
this.contacts?.push(decryptedContacts[0]);
}
} else {
this.contacts?.push(contact);
}
// if (contact.contactType === ContactType.CLOUD) {
// const decryptedContacts = await this.fetchContactFromDSS(
// Object.fromEntries([[newContact.contactId, newContact]]) as ContactIdContactInfo
// );

// if (decryptedContacts.length && decryptedContacts[0]) {
// this.contacts?.push(decryptedContacts[0]);
// }
// } else {
// this.contacts?.push(contact);
// }

return contactResponse;
} catch (err: unknown) {
Expand Down
56 changes: 28 additions & 28 deletions packages/calling/src/Contacts/contactFixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,34 +198,34 @@ export const mockContactListOne = [
},
avatarURL: 'avatar-prod-us-east-2.webexcontent.com',
},
{
avatarUrlDomain: 'avatar-prod-us-east-2.webexcontent.com',
contactId: '801bb994-343b-4f6b-97ae-d13c91d4b877',
contactType: 'CLOUD',
department: '123029217',
displayName: 'Emily Nagakawa',
emails: [{value: '[email protected]'}],
encryptionKeyUrl: 'kms://cisco.com/keys/dcf18f9d-155e-44ff-ad61-c8a69b7103ab',
firstName: 'Emily',
groups: ['1561977e-3443-4ccf-a591-69686275d7d2'],
lastName: 'Nagakawa',
manager: 'Robert Langdon',
ownerId: '0fea4a63-4e27-46ee-99c3-2472cb12bf68',
phoneNumbers: [
{type: 'mobile', value: '+1 835 648 8750'},
{type: 'work', value: '+1 791 723 8825'},
{type: 'work', value: mockNumber2},
],
sipAddresses: [
{type: 'cloud-calling', value: '[email protected]', primary: true},
{type: 'personal-room', value: '[email protected]', primary: false},
{type: 'enterprise', value: '[email protected]', primary: true},
{type: 'personal-room', value: '[email protected]', primary: false},
{type: 'work', value: mockSipAddress},
],
title: 'Software Engineer',
avatarURL: 'avatar-prod-us-east-2.webexcontent.com',
},
// {
// avatarUrlDomain: 'avatar-prod-us-east-2.webexcontent.com',
// contactId: '801bb994-343b-4f6b-97ae-d13c91d4b877',
// contactType: 'CLOUD',
// department: '123029217',
// displayName: 'Emily Nagakawa',
// emails: [{value: '[email protected]'}],
// encryptionKeyUrl: 'kms://cisco.com/keys/dcf18f9d-155e-44ff-ad61-c8a69b7103ab',
// firstName: 'Emily',
// groups: ['1561977e-3443-4ccf-a591-69686275d7d2'],
// lastName: 'Nagakawa',
// manager: 'Robert Langdon',
// ownerId: '0fea4a63-4e27-46ee-99c3-2472cb12bf68',
// phoneNumbers: [
// {type: 'mobile', value: '+1 835 648 8750'},
// {type: 'work', value: '+1 791 723 8825'},
// {type: 'work', value: mockNumber2},
// ],
// sipAddresses: [
// {type: 'cloud-calling', value: '[email protected]', primary: true},
// {type: 'personal-room', value: '[email protected]', primary: false},
// {type: 'enterprise', value: '[email protected]', primary: true},
// {type: 'personal-room', value: '[email protected]', primary: false},
// {type: 'work', value: mockSipAddress},
// ],
// title: 'Software Engineer',
// avatarURL: 'avatar-prod-us-east-2.webexcontent.com',
// },
];

export const mockContactListTwo = [
Expand Down
3 changes: 1 addition & 2 deletions packages/calling/src/SDKConnector/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
DSSLookupResponse,
KmsKey,
KmsResourceObject,
LookupOptions,
Expand Down Expand Up @@ -83,7 +82,7 @@ export interface WebexSDK {
};
};
dss: {
lookup: (options: LookupOptions) => Promise<DSSLookupResponse[]>;
lookup: (options: LookupOptions) => Promise<void>;
};
encryption: {
decryptText: (encryptionKeyUrl: string, encryptedData?: string) => Promise<string>;
Expand Down
24 changes: 1 addition & 23 deletions packages/calling/src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,31 +182,9 @@ export type ContactDetail = {
};

export interface LookupOptions {
ids: string[];
id: string;
}

export type DSSLookupResponse = {
additionalInfo: {
department: string;
firstName: string;
identityManager: {
managerId: string;
displayName: string;
};
jobTitle: string;
lastName: string;
};
displayName: string;
emails: ContactDetail[];
entityProviderType: string;
identity: string;
orgId: string;
phoneNumbers: ContactDetail[];
photos: ContactDetail[];
sipAddresses: ContactDetail[];
type: string;
};

export type KmsKey = {
uri: string;
userId: string;
Expand Down
Loading