Skip to content

Commit

Permalink
feat(users): add stats example ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
PierreBrisorgueil committed Jul 16, 2020
1 parent f91dddf commit e9108b2
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
14 changes: 14 additions & 0 deletions modules/users/controllers/admin.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,20 @@ exports.delete = async (req, res) => {
}
};

/**
* @desc Endpoint to get stats of users and return data
* @param {Object} req - Express request object
* @param {Object} res - Express response object
*/
exports.stats = async (req, res) => {
const data = await UserService.stats();
if (!data.err) {
responses.success(res, 'users stats')(data);
} else {
responses.error(res, 422, 'Unprocessable Entity', errors.getMessage(data.err))(data.err);
}
};

/**
* @desc MiddleWare to ask the service the user for this id
* @param {Object} req - Express request object
Expand Down
9 changes: 7 additions & 2 deletions modules/users/policies/users.policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ exports.invokeRolesPolicies = () => {
resources: '/api/users/:userId',
permissions: ['get', 'put', 'delete'],
}],
},
]);
}, {
roles: ['guest'],
allows: [{
resources: '/api/users/stats',
permissions: ['get'],
}],
}]);
};
6 changes: 6 additions & 0 deletions modules/users/repositories/user.repository.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ exports.delete = async (user) => {
return null;
};

/**
* @desc Function to get collection stats
* @return {Object} scrap
*/
exports.stats = () => User.countDocuments();

/**
* @desc Function to import list of users in db
* @param {[Object]} users
Expand Down
4 changes: 4 additions & 0 deletions modules/users/routes/admin.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ module.exports = (app) => {
/* eslint global-require: 0 */
require('./users.routes.js')(app);

// stats
app.route('/api/users/stats').all(policy.isAllowed)
.get(admin.stats);

// Users collection routes
app.route('/api/users')
.get(passport.authenticate('jwt'), policy.isAllowed, admin.list); // list
Expand Down
9 changes: 9 additions & 0 deletions modules/users/services/user.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ exports.delete = async (user) => {
return Promise.resolve(result);
};

/**
* @desc Function to get all stats of db
* @return {Promise} All stats
*/
exports.stats = async () => {
const result = await UserRepository.stats();
return Promise.resolve(result);
};

/**
* @desc Function to authenticate user)
* @param {String} email
Expand Down

0 comments on commit e9108b2

Please sign in to comment.