Skip to content

Commit

Permalink
feat(core, users): add team route ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Nov 16, 2020
1 parent 069e132 commit 17c97ba
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 12 deletions.
14 changes: 14 additions & 0 deletions modules/core/controllers/core.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,17 @@ exports.changelogs = async (req, res) => {
responses.error(res, 422, 'Unprocessable Entity', errors.getMessage(err))(err);
}
};

/**
* @desc Endpoint to ask the service to get the list of users
* @param {Object} req - Express request object
* @param {Object} res - Express response object
*/
exports.team = async (req, res) => {
try {
const users = await CoreService.team();
responses.success(res, 'team list')(users);
} catch (err) {
responses.error(res, 422, 'Unprocessable Entity', errors.getMessage(err))(err);
}
};
3 changes: 3 additions & 0 deletions modules/core/policies/core.policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ exports.invokeRolesPolicies = () => {
}, {
resources: '/api/core/changelogs',
permissions: ['get'],
}, {
resources: '/api/core/team',
permissions: ['get'],
}],
}]);
};
12 changes: 12 additions & 0 deletions modules/core/repositories/core.repository.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Module dependencies
*/
const mongoose = require('mongoose');

const User = mongoose.model('User');

/**
* @desc Function to get all user in db
* @return {Array} All users
*/
exports.team = () => User.find({ roles: 'admin' }, '-password -providerData -complementary').sort('-createdAt').exec();
3 changes: 3 additions & 0 deletions modules/core/routes/core.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ module.exports = (app) => {
// changelogs
app.route('/api/core/changelogs').all(policy.isAllowed)
.get(core.changelogs);
// changelogs
app.route('/api/core/team').all(policy.isAllowed)
.get(core.team);
};
12 changes: 12 additions & 0 deletions modules/core/services/core.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ const path = require('path');
const _ = require('lodash');
const base64 = require('js-base64').Base64;

const UserService = require(path.resolve('./modules/users/services/user.service'));
const config = require(path.resolve('./config'));
const CoreRepository = require('../repositories/core.repository');

/**
* @desc Function to get all versions
* @return {Promise} All versions
Expand Down Expand Up @@ -43,3 +46,12 @@ exports.changelogs = async () => {
}));
return Promise.resolve(results);
};

/**
* @desc Function to get all admin users in db
* @return {Promise} All users
*/
exports.team = async () => {
const result = await CoreRepository.team();
return Promise.resolve(result.map((user) => UserService.removeSensitive(user)));
};
17 changes: 15 additions & 2 deletions modules/core/tests/core.crud.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ describe('Tasks CRUD Tests :', () => {
});

describe('Logout', () => {
test('should not be able to save a task', async () => {
test('shouldbe able to get releases', async () => {
try {
const result = await agent.get('/api/core/releases')
.expect(200);
Expand All @@ -44,7 +44,7 @@ describe('Tasks CRUD Tests :', () => {
}
});

test('should not be able to save a task', async () => {
test('should be able to get changelogs', async () => {
try {
const result = await agent.get('/api/core/changelogs')
.expect(200);
Expand All @@ -56,6 +56,19 @@ describe('Tasks CRUD Tests :', () => {
console.log(err);
}
});

test('should be able to get team members', async () => {
try {
const result = await agent.get('/api/core/team')
.expect(200);
expect(result.body.type).toBe('success');
expect(result.body.message).toBe('team list');
expect(result.body.data).toBeInstanceOf(Array);
} catch (err) {
expect(err).toBeFalsy();
console.log(err);
}
});
});

// Mongoose disconnect
Expand Down
20 changes: 10 additions & 10 deletions modules/users/services/user.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const saltRounds = 10;
* @param {Object} user
* @return {Object} user
*/
const removeSensitive = (user, conf) => {
exports.removeSensitive = (user, conf) => {
if (!user || typeof user !== 'object') return null;
const keys = conf || config.whitelists.users.default;
return _.pick(user, keys);
Expand All @@ -30,7 +30,7 @@ const removeSensitive = (user, conf) => {
*/
exports.list = async () => {
const result = await UserRepository.list();
return Promise.resolve(result.map((user) => removeSensitive(user)));
return Promise.resolve(result.map((user) => this.removeSensitive(user)));
};

/**
Expand All @@ -53,7 +53,7 @@ exports.create = async (user) => {
}
const result = await UserRepository.create(user);
// Remove sensitive data before return
return Promise.resolve(removeSensitive(result));
return Promise.resolve(this.removeSensitive(result));
};

/**
Expand All @@ -63,7 +63,7 @@ exports.create = async (user) => {
*/
exports.get = async (user) => {
const result = await UserRepository.get(user);
return Promise.resolve(removeSensitive(result));
return Promise.resolve(this.removeSensitive(result));
};

/**
Expand All @@ -83,7 +83,7 @@ exports.getBrut = async (user) => {
*/
exports.search = async (input) => {
const result = await UserRepository.search(input);
return Promise.resolve(result.map((user) => removeSensitive(user)));
return Promise.resolve(result.map((user) => this.removeSensitive(user)));
};

/**
Expand All @@ -94,12 +94,12 @@ exports.search = async (input) => {
* @return {Promise} user -
*/
exports.update = async (user, body, option) => {
if (!option) user = _.assignIn(user, removeSensitive(body, config.whitelists.users.update));
else if (option === 'admin') user = _.assignIn(user, removeSensitive(body, config.whitelists.users.updateAdmin));
else if (option === 'recover') user = _.assignIn(user, removeSensitive(body, config.whitelists.users.recover));
if (!option) user = _.assignIn(user, this.removeSensitive(body, config.whitelists.users.update));
else if (option === 'admin') user = _.assignIn(user, this.removeSensitive(body, config.whitelists.users.updateAdmin));
else if (option === 'recover') user = _.assignIn(user, this.removeSensitive(body, config.whitelists.users.recover));

const result = await UserRepository.update(user);
return Promise.resolve(removeSensitive(result));
return Promise.resolve(this.removeSensitive(result));
};

/**
Expand Down Expand Up @@ -130,7 +130,7 @@ exports.stats = async () => {
exports.authenticate = async (email, password) => {
const user = await UserRepository.get({ email });
if (!user) throw new AppError('invalid user or password.', { code: 'SERVICE_ERROR' });
if (await this.comparePassword(password, user.password)) return removeSensitive(user);
if (await this.comparePassword(password, user.password)) return this.removeSensitive(user);
throw new AppError('invalid user or password.', { code: 'SERVICE_ERROR' });
};

Expand Down

0 comments on commit 17c97ba

Please sign in to comment.