Skip to content
This repository has been archived by the owner on Jul 4, 2019. It is now read-only.

Commit

Permalink
Merge pull request #14 from Financial-Times/groups-api
Browse files Browse the repository at this point in the history
Small changes for the group API
  • Loading branch information
ciprianlujeru authored Jul 19, 2017
2 parents b715a44 + a7c2848 commit 2135e18
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 9 deletions.
54 changes: 45 additions & 9 deletions lib/myFTClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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 -
Expand All @@ -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} : {})
);
}
Expand Down Expand Up @@ -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 -
Expand All @@ -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
Expand Down Expand Up @@ -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 -
Expand Down Expand Up @@ -877,6 +911,7 @@ function syncUserFollowers(groupId, userId) {
module.exports = {
addLicence,
updateLicence,
updateGroup,
getLicence,
getUserFromLicence,
getUserFromGroup,
Expand All @@ -888,6 +923,7 @@ module.exports = {
getConceptsFollowedByGroup,
getUsersForLicence,
getUsersForGroup,
getUsersForGroupByPage,
getGroupsForLicence,
addUsersToLicence,
removeUsersFromLicence,
Expand Down
1 change: 1 addition & 0 deletions test/mocks/fixtures/getGroupFromLicence.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"uuid": "8619e7a0-65b7-446b-9931-4197b3fe0cbf",
"name": "All users",
"_rel": {
"byTool": "KAT",
"created": 1472335707780,
Expand Down
25 changes: 25 additions & 0 deletions test/mocks/fixtures/getGroupUsersByPage.json
Original file line number Diff line number Diff line change
@@ -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
}
52 changes: 52 additions & 0 deletions test/myFTClient.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 2135e18

Please sign in to comment.