Skip to content

Commit

Permalink
Back-end: Created route for fetching users list by ids
Browse files Browse the repository at this point in the history
Testing: Back-end: Modified current & created new tests for fetching users list by ids

I modified the GET /users/:id route to support single value or array as id parameter. The returned value is a JSON map that its properties are the ids and the value for each one is the relevant user document.
The commit includes tests modification and new tests.

#317, #337
  • Loading branch information
OrAbramovich committed Apr 27, 2018
1 parent 9dc3a26 commit 3d17b8f
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
26 changes: 25 additions & 1 deletion server/helpers/arrayFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,35 @@ const unionArrays = (arrayA, arrayB) => {
return res;
};

/**
*
* @author: Or Abramovich
* @date: 04/18
*
* Creates a json with the format key:value from an array where the value is the original elemnt in the array and the key
* is the value of a given property of the element in the array
*
* @param {Array} array: the array to be converted into a JSON map
* @param {String} elementKeyPropertyName: the property of the array elements that its value will be the key in the JSON map.
*
* @returns a JSON map which is a JSON format string tht its properties are the keys.
*/
const convertArrayToJsonMap = (array, elementKeyPropertyName) =>{
var res = {};
array.forEach(function(element){
Object.assign(res, {
[element[elementKeyPropertyName]]: element
});
});
return res;
}

module.exports = {
findMatchingValuesInArrays,
containsElementWithProperty,
sortArrayASC,
getIndexOfValue,
getIndexOfFirstElementMatchKey,
unionArrays
unionArrays,
convertArrayToJsonMap
};
20 changes: 14 additions & 6 deletions server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const { logInfo } = require('./services/logger/logger');
const { ObjectID } = require('mongodb');
const httpLogger = require('./services/logger/http-logger');
const { notifyUsers } = require('./services/notifications-system/notifier');
const {convertArrayToJsonMap} = require('./helpers/arrayFunctions');
const userVerificator = require('./services/user-verification/user-verificator');
const passwordReset = require('./services/password-reset/password-reset');
const errors = require('./errors');
Expand Down Expand Up @@ -474,19 +475,26 @@ app.get('/users/tags', async (req, res) => {
});

/**
* Get user details for the given id
*
* @param {String} id
* @updatedBy: Or Abramovich
* @date: 04/18
*
* Get user details for the given id - the roue supports a single id parameter or multiple values.
*
* @returns {JSON} a JSON map that its properties are the ids and the value for each one is the relevant user document.
*
*/
app.get('/users/:id', async (req, res) => {
try {
const { id } = req.params;
const {id} = req.params;
const ids = id.toString().split(',');

const user = await User.findById(id);
if (!user) {
const users = convertArrayToJsonMap(await User.find({"_id" : {"$in" : Array.from([].concat(ids), x => new ObjectID(x))}}), '_id');
if (Object.keys(users).length != ids.length) {
return res.status(NOT_FOUND).send();
}
return res.send({ user });

return res.send({users: users});
} catch (err) {
return res.status(BAD_REQUEST).send(err);
}
Expand Down
26 changes: 24 additions & 2 deletions tests/server/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1448,7 +1448,7 @@ describe('Server Tests', () => {
});

describe('#GET /users/:id', () => {
it('should find existing user by id', (done) => {
it('should find single existing user by id', (done) => {
const id = users[1]._id.toHexString();

request(app)
Expand All @@ -1457,11 +1457,33 @@ describe('Server Tests', () => {
.expect((res) => {
const expected = User.toJSON(users[1]);
expected._id = expected._id.toHexString();
expect(res.body.user).toEqual(expected);
expect(res.body.users[expected._id]).toEqual(expected);
})
.end(done);
});

it('should find multiple existing user by id', (done) => {
var id = [users[0]._id.toHexString(), users[1]._id.toHexString() ];
request(app)
.get(`/users/${id}`)
.expect(OK)
.expect(async (res) => {
const user1 = JSON.stringify(await User.findById(users[0]._id.toHexString()));
const user2 = JSON.stringify(await User.findById(users[1]._id.toHexString()));
expect(JSON.stringify(res.body.users[users[0]._id])).toEqual(user1);
expect(JSON.stringify(res.body.users[users[1]._id])).toEqual(user2);
})
.end(done);
});

it('should not find multiple users by id where one doesnt exist', (done) => {
var id = [users[0]._id.toHexString(), new ObjectID() ];
request(app)
.get(`/users/${id}`)
.expect(NOT_FOUND)
.end(done);
});

it('should not find user with invalid id', (done) => {
const id = '1234';

Expand Down

0 comments on commit 3d17b8f

Please sign in to comment.