diff --git a/src/MembershipsSDKAdapter.js b/src/MembershipsSDKAdapter.js index f39be24a..c34d5804 100644 --- a/src/MembershipsSDKAdapter.js +++ b/src/MembershipsSDKAdapter.js @@ -16,6 +16,7 @@ import { publishReplay, refCount, tap, + catchError, } from 'rxjs/operators'; import {SDK_EVENT, constructHydraId, deconstructHydraId} from '@webex/common'; import { @@ -117,6 +118,29 @@ function getMembers(sdkMembers, meeting) { })); } +/** + * Maps SDK membership to adapter membership + * + * @private + * @param {object} sdkMembership SDK membership object + * @returns {Member} Adapter membership object + */ +function fromSDKMembership(sdkMembership) { + return { + ID: sdkMembership.id, + roomID: sdkMembership.roomId, + isModerator: sdkMembership.isModerator, + isMonitor: sdkMembership.isMonitor, + isRoomHidden: sdkMembership.isRoomHidden, + personDisplayName: sdkMembership.personDisplayName, + personEmail: sdkMembership.personEmail, + personID: sdkMembership.personId, + personOrgID: sdkMembership.personOrgId, + roomType: sdkMembership.roomType, + created: sdkMembership.created, + }; +} + /** * The `MembershipsSDKAdapter` is an implementation of the `MembershipsAdapter` interface. * This adapter utilizes the Webex JS SDK to fetch data about a memberships. @@ -307,4 +331,29 @@ export default class MembershipsSDKAdapter extends MembershipsAdapter { return members$; } + + /** + * Returns an observable that emits the membership with a member added to the room. + * Observable will complete after one emission + * + * @param {string} personID ID of the person to add to a room + * @param {string} roomID ID of the room to add the person into + * @returns {Observable} Observable stream that emits the membership with the added member + */ + addRoomMember(personID, roomID) { + logger.debug('MEMBERSHIP', undefined, 'addRoomMember()', ['called with', {personID, roomID}]); + + return defer(() => this.datasource.memberships.create({ + personId: personID, + roomId: roomID, + })) + .pipe( + map(fromSDKMembership), + tap((membership) => logger.debug('MEMBERSHIP', undefined, 'addRoomMember()', ['member added to room', membership])), + catchError((error) => { + logger.error('MEMBERSHIP', undefined, 'addRoomMember()', 'Error adding member to room', error); + throw error; + }), + ); + } } diff --git a/src/MembershipsSDKAdapter.test.js b/src/MembershipsSDKAdapter.test.js index 98a864bf..31a7d353 100644 --- a/src/MembershipsSDKAdapter.test.js +++ b/src/MembershipsSDKAdapter.test.js @@ -151,4 +151,55 @@ describe('Memberships SDK Adapter', () => { }); }); }); + + describe('addRoomMember()', () => { + let roomID; + let personID; + + beforeEach(() => { + roomID = 'roomID'; + personID = 'personID'; + }); + afterEach(() => { + roomID = null; + personID = null; + }); + + test('returns an observable', (done) => { + expect(isObservable(membershipSDKAdapter.addRoomMember(personID, roomID))) + .toBeTruthy(); + done(); + }); + + test('emits success when member is added to room', (done) => { + membershipSDKAdapter.addRoomMember(personID, roomID).subscribe((membership) => { + expect(membership).toEqual({ + ID: 'id', + roomID: 'roomID', + personID: 'personID', + personOrgID: 'organizationID', + personEmail: 'email@cisco.com', + personDisplayName: 'Simon Damiano', + isModerator: false, + isMonitor: false, + created: '', + }); + }); + done(); + }); + + test('emits error when sdk fails to add member to room', (done) => { + const errorMsg = 'Error adding member to room'; + + mockSDK.memberships.create = jest.fn(() => Promise.reject(new Error(errorMsg))); + + membershipSDKAdapter.addRoomMember(personID, roomID).subscribe( + () => {}, + (error) => { + expect(error.message).toBe(errorMsg); + done(); + }, + ); + }); + }); }); diff --git a/src/mockSdk.js b/src/mockSdk.js index 9d5021de..24755c02 100644 --- a/src/mockSdk.js +++ b/src/mockSdk.js @@ -346,6 +346,7 @@ export default function createMockSDK(api = {}) { stopListening: jest.fn(() => Promise.resolve()), on: jest.fn(), off: jest.fn(), + create: jest.fn(() => Promise.resolve(mockSDKMembership)), }, attachmentActions: { create: jest.fn(() => Promise.resolve(mockSDKAttachmentAction)),