Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- static get(req, res) {
- return Promise.resolve()
- .then(() => {
- if (!isObjectId(req.params.userid)) {
- throw Boom.badData('incorrect userid');
- }
- return User.aggregate([
- {
- $match: {
- _id: {
- $eq: new mongoose.mongo.ObjectId(req.params.userid),
- },
- },
- },
- {
- $lookup: {
- from: 'proposals',
- localField: '_id',
- foreignField: 'consultant',
- as: 'proposals',
- },
- },
- {
- $addFields: {
- readProposals: {
- $filter: {
- input: '$proposals',
- as: 'readProposals',
- cond: {
- $eq: ['$$readProposals.read', true],
- },
- },
- },
- },
- },
- {
- $addFields: {
- proposalsCount: {
- $size: '$proposals',
- },
- readProposalsCount: {
- $size: '$readProposals',
- },
- },
- },
- {
- $project: {
- password: 0,
- proposals: 0,
- readProposals: 0,
- },
- },
- ]);
- })
- .then(user => user[0])
- .then(user => {
- if (!user) {
- throw Boom.notFound('no such user');
- }
- res.send(mongoToRes(user));
- })
- .catch(err => {
- debug(err);
- throw handleError(err);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement