2014-07-31 11:27:14 +03:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
/**
|
2016-01-07 22:18:36 +01:00
|
|
|
* Module dependencies
|
2014-07-31 11:27:14 +03:00
|
|
|
*/
|
|
|
|
|
var _ = require('lodash'),
|
2015-07-25 16:53:11 -04:00
|
|
|
mongoose = require('mongoose'),
|
|
|
|
|
User = mongoose.model('User');
|
2014-07-31 11:27:14 +03:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* User middleware
|
|
|
|
|
*/
|
2015-07-26 01:40:16 -04:00
|
|
|
exports.userByID = function (req, res, next, id) {
|
2015-07-25 16:53:11 -04:00
|
|
|
if (!mongoose.Types.ObjectId.isValid(id)) {
|
|
|
|
|
return res.status(400).send({
|
2018-01-12 16:53:24 +08:00
|
|
|
message: 'SERVER.INVALID_OBJECTID'
|
2015-07-25 16:53:11 -04:00
|
|
|
});
|
|
|
|
|
}
|
2015-07-26 01:40:16 -04:00
|
|
|
|
2015-07-25 16:53:11 -04:00
|
|
|
User.findOne({
|
|
|
|
|
_id: id
|
2018-05-29 18:05:10 +08:00
|
|
|
}, '-history -remarks')
|
2018-05-13 16:48:15 +08:00
|
|
|
.populate('invited_by', 'username displayName profileImageURL isVip score uploaded downloaded')
|
2017-10-19 18:44:22 +08:00
|
|
|
.populate('makers', 'name')
|
|
|
|
|
.exec(function (err, user) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
} else if (!user) {
|
|
|
|
|
return next(new Error('Failed to load User ' + id));
|
|
|
|
|
}
|
2015-07-25 16:53:11 -04:00
|
|
|
|
2017-10-19 18:44:22 +08:00
|
|
|
req.profile = user;
|
|
|
|
|
next();
|
|
|
|
|
});
|
2014-07-31 11:27:14 +03:00
|
|
|
};
|