Skip to content

Commit

Permalink
feat(MembershipsAdapter): add addRoomMember method
Browse files Browse the repository at this point in the history
  • Loading branch information
harshrai-11 authored and kponmuth committed May 2, 2022
1 parent 5c390a6 commit 0cbc537
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 0 deletions.
49 changes: 49 additions & 0 deletions src/MembershipsSDKAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
publishReplay,
refCount,
tap,
catchError,
} from 'rxjs/operators';
import {SDK_EVENT, constructHydraId, deconstructHydraId} from '@webex/common';
import {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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<Member>} 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;
}),
);
}
}
51 changes: 51 additions & 0 deletions src/MembershipsSDKAdapter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 protected]',
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();
},
);
});
});
});
1 change: 1 addition & 0 deletions src/mockSdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
Expand Down

0 comments on commit 0cbc537

Please sign in to comment.