diff --git a/lib/myFTClient.js b/lib/myFTClient.js index bd2c7b2..547eb14 100644 --- a/lib/myFTClient.js +++ b/lib/myFTClient.js @@ -60,9 +60,7 @@ function _getAllNodeItems(node, nodeId, relationship, relatedNode) { params.page++; // get the next list of items - return _createAndTriggerRelationshipRequest("GET", node, nodeId, relationship, relatedNode, undefined, params) - .then(helpers.parseJsonRes) - .then(thenFn); + return _getNodeItemsByPage(node, nodeId, relationship, relatedNode, params).then(thenFn); } } @@ -71,9 +69,22 @@ function _getAllNodeItems(node, nodeId, relationship, relatedNode) { return allItems; }; + return _getNodeItemsByPage(node, nodeId, relationship, relatedNode, params).then(thenFn); +} + +/** + * Get the node items by page + * @param {String} node - + * @param {String} nodeId - + * @param {String} relationship - + * @param {String} relatedNode - + * @param {Object} params - + * @returns {Promise} response - + * @private + */ +function _getNodeItemsByPage(node, nodeId, relationship, relatedNode, params) { return _createAndTriggerRelationshipRequest("GET", node, nodeId, relationship, relatedNode, undefined, params) .then(helpers.parseJsonRes) - .then(thenFn); } /** @@ -274,7 +285,7 @@ function _getMemberFromNode(node, nodeId, memberType, memberId) { * @param {String} nodeId - * @param {String} rel - * @param {String} relType - - * @param {String|Array} relIds - + * @param {String|Object|Array} relIds - * @param {Object} [relProp] - * @param {Object} [options] - * @returns {Promise} response - @@ -290,15 +301,17 @@ function _addRemoveRelationships(method, node, nodeId, rel, relType, relIds, rel let body; if (Array.isArray(relIds)) { - body = relIds.map(uuid => { + body = relIds.map(data => { + const relData = data === Object(data) ? data : {uuid: data}; return Object.assign( - {uuid}, + relData, (relProp !== undefined ? {_rel: relProp} : {}) ); }); } else { + const relData = relIds === Object(relIds) ? relIds : {uuid: relIds}; body = Object.assign( - {uuid: relIds}, + relData, (relProp !== undefined ? {_rel: relProp} : {}) ); } @@ -507,7 +520,7 @@ function addLicence(uuid) { } /** - * Adds a License in myFT + * Updates a License in myFT * @param {String} uuid - * @param {Object} data - * @return {Promise} response - @@ -517,6 +530,17 @@ function updateLicence(uuid, data) { return _updateNode(myftConst.licenceNodeName, uuid, data); } +/** + * Updates a Group in myFT + * @param {String} uuid - + * @param {Object} data - + * @return {Promise} response - + * @throws {Error} statusError - + **/ +function updateGroup(uuid, data) { + return _updateNode(myftConst.groupNodeName, uuid, data); +} + /** * Gets a License from myFT * @param {String} uuid - of the licence @@ -604,6 +628,16 @@ function getUsersForGroup(uuid){ return _getAllNodeItems(myftConst.groupNodeName, uuid, myftConst.memberRelName, myftConst.userNodeName); } +/** + * Gets the Users who are members of a group + * @param {String} uuid - of the group + * @param {Object} params - { page, limit } + * @return {Promise} response - array of users + */ +function getUsersForGroupByPage(uuid, params){ + return _getNodeItemsByPage(myftConst.groupNodeName, uuid, myftConst.memberRelName, myftConst.userNodeName, params); +} + /** * Gets the users that are following a concept/topic * @param {String} licenceId - @@ -877,6 +911,7 @@ function syncUserFollowers(groupId, userId) { module.exports = { addLicence, updateLicence, + updateGroup, getLicence, getUserFromLicence, getUserFromGroup, @@ -888,6 +923,7 @@ module.exports = { getConceptsFollowedByGroup, getUsersForLicence, getUsersForGroup, + getUsersForGroupByPage, getGroupsForLicence, addUsersToLicence, removeUsersFromLicence, diff --git a/test/mocks/fixtures/getGroupFromLicence.json b/test/mocks/fixtures/getGroupFromLicence.json index 454a05c..c510265 100644 --- a/test/mocks/fixtures/getGroupFromLicence.json +++ b/test/mocks/fixtures/getGroupFromLicence.json @@ -1,5 +1,6 @@ { "uuid": "8619e7a0-65b7-446b-9931-4197b3fe0cbf", + "name": "All users", "_rel": { "byTool": "KAT", "created": 1472335707780, diff --git a/test/mocks/fixtures/getGroupUsersByPage.json b/test/mocks/fixtures/getGroupUsersByPage.json new file mode 100644 index 0000000..205549d --- /dev/null +++ b/test/mocks/fixtures/getGroupUsersByPage.json @@ -0,0 +1,25 @@ +{ + "group": { + "_id": 1, + "labels": ["Group"], + "properties": { + "name": "All users", + "updated": 1500377383712, + "uuid": "8eb26ed7-68c8-44c6-b6ce-52d61500f301" + } + }, + "total": 1, + "items": [ + { + "uuid": "b2697f93-52d3-4d42-8409-bdf91b09e894", + "_rel": { + "byTool": "KAT", + "created": 1498743272070, + "count": 1, + "updated": 1498743272070, + "byUser": "c62c4485-7183-494c-a947-d754f5cd0a15" + } + } + ], + "count": 1 +} diff --git a/test/myFTClient.spec.js b/test/myFTClient.spec.js index a883571..421e769 100644 --- a/test/myFTClient.spec.js +++ b/test/myFTClient.spec.js @@ -356,6 +356,34 @@ describe('myFT Client proxy', () => { .catch(done); }); + it ('Should be able to update a group', done => { + const newGroupName = 'All users'; + if (mockAPI) { + nock(baseUrl) + .put(`/${myftConst.groupNodeName}/${uuids.validLicence}`) + .reply(200, () => ({})); + + nock(baseUrl) + .get(`/${myftConst.licenceNodeName}/${uuids.validLicence}/${myftConst.memberRelName}/${myftConst.groupNodeName}/${uuids.validLicence}`) + .reply(200, () => require('./mocks/fixtures/getGroupFromLicence')); + } + + myFT.updateGroup(uuids.validLicence, {"name": newGroupName}) + .then(resp => { + expect(resp).to.be.an('object'); + + return myFT.getGroupFromLicence(uuids.validLicence, uuids.validLicence); + }) + .then(resp => { + expectOwnProperties(resp, ['uuid', 'name', '_rel']); + expect(resp.uuid).to.equal(uuids.validLicence); + expect(resp.name).to.equal(newGroupName); + + done(); + }) + .catch(done); + }); + it ('Should be able to get a valid licence', done => { if (mockAPI) { nock(baseUrl) @@ -497,6 +525,30 @@ describe('myFT Client proxy', () => { .catch(done); }); + it ('Should get users registered to a group by pages', done => { + const page = 1; + const limit = 10; + if (mockAPI) { + nock(baseUrl) + .get(`/${myftConst.groupNodeName}/${uuids.validLicence}/${myftConst.memberRelName}/${myftConst.userNodeName}?page=${page}&limit=${limit}`) + .reply(200, () => require('./mocks/fixtures/getGroupUsersByPage')); + } + + myFT.getUsersForGroupByPage(uuids.validLicence, { page, limit }) + .then(usersResponse => { + expect(usersResponse).to.be.an('object'); + expectOwnProperties(usersResponse, ['group', 'total', 'items', 'count']); + + const items = usersResponse.items; + expect(items).to.be.an('array'); + expect(items.length).to.be.at.least(1); + expectOwnProperties(items, ['uuid']); + + done(); + }) + .catch(done); + }); + it ('Should get groups registered to a licence', done => { if (mockAPI) { nock(baseUrl)