From d056f2c55cc993182c10dda74b1bd219e27e811b Mon Sep 17 00:00:00 2001 From: Arash Koushkebaghi Date: Tue, 17 Dec 2019 10:25:35 -0800 Subject: [PATCH] feat(PeopleSdkAdapter): getMe() fetches and emits the person data once --- src/PeopleSDKAdapter.integration.test.js | 13 +++++++++++++ src/PeopleSDKAdapter.js | 13 ++++++++++--- src/__mocks__/sdk.js | 2 +- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/PeopleSDKAdapter.integration.test.js b/src/PeopleSDKAdapter.integration.test.js index ab878319..125eb596 100644 --- a/src/PeopleSDKAdapter.integration.test.js +++ b/src/PeopleSDKAdapter.integration.test.js @@ -28,6 +28,19 @@ describe('People SDK Adapter', () => { } }); + describe('getMe()', () => { + test('returns person data of the access token bearer in a proper shape', (done) => { + subscription = webexSDKAdapter.peopleAdapter.getMe().subscribe((person) => { + expect(person).toMatchObject({ + ID: userID, + emails: [user.emailAddress], + displayName: user.displayName, + }); + done(); + }); + }); + }); + describe('getPerson() returns', () => { beforeEach(() => { getPerson$ = webexSDKAdapter.peopleAdapter.getPerson(userID); diff --git a/src/PeopleSDKAdapter.js b/src/PeopleSDKAdapter.js index 32bf125c..ff89d012 100644 --- a/src/PeopleSDKAdapter.js +++ b/src/PeopleSDKAdapter.js @@ -1,5 +1,5 @@ import {concat, from, fromEvent} from 'rxjs'; -import {flatMap, filter, finalize, map, publishReplay, refCount} from 'rxjs/operators'; +import {flatMap, filter, finalize, map, publishReplay, refCount, take} from 'rxjs/operators'; import {deconstructHydraId} from '@webex/common'; import {PeopleAdapter, PersonStatus} from '@webex/component-adapter-interfaces'; @@ -55,14 +55,21 @@ export default class PeopleSDKAdapter extends PeopleAdapter { /** * Returns an observable that emits person data of the access token bearer. + * The observable will emit once and then complete. * * @public * @returns {Observable.} * @memberof PeopleSDKAdapter */ getMe() { - // person ID must be retrieved in order to invoke `getPerson` method properly. - return from(this.datasource.people.get('me')).pipe(flatMap(({id}) => this.getPerson(id))); + return from(this.fetchPerson('me')).pipe( + flatMap((person) => + from(this.datasource.internal.presence.get([person.id])).pipe( + map(({status}) => ({...person, status: this.getStatus(status)})) + ) + ), + take(1) + ); } /** diff --git a/src/__mocks__/sdk.js b/src/__mocks__/sdk.js index 23bbd577..e672320e 100644 --- a/src/__mocks__/sdk.js +++ b/src/__mocks__/sdk.js @@ -52,7 +52,7 @@ export default function createMockSDK() { off: jest.fn(), }, presence: { - get: jest.fn(), + get: jest.fn(() => Promise.resolve({status: 'active'})), subscribe: jest.fn(() => Promise.resolve({responses: [{status: {status: 'active'}}]})), unsubscribe: jest.fn(() => Promise.resolve()), },