Advertisement
Guest User

Untitled

a guest
Mar 18th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. static get(req, res) {
  2.     return Promise.resolve()
  3.       .then(() => {
  4.         if (!isObjectId(req.params.userid)) {
  5.           throw Boom.badData('incorrect userid');
  6.         }
  7.         return User.aggregate([
  8.           {
  9.             $match: {
  10.               _id: {
  11.                 $eq: new mongoose.mongo.ObjectId(req.params.userid),
  12.               },
  13.             },
  14.           },
  15.           {
  16.             $lookup: {
  17.               from: 'proposals',
  18.               localField: '_id',
  19.               foreignField: 'consultant',
  20.               as: 'proposals',
  21.             },
  22.           },
  23.           {
  24.             $addFields: {
  25.               readProposals: {
  26.                 $filter: {
  27.                   input: '$proposals',
  28.                   as: 'readProposals',
  29.                   cond: {
  30.                     $eq: ['$$readProposals.read', true],
  31.                   },
  32.                 },
  33.               },
  34.             },
  35.           },
  36.           {
  37.             $addFields: {
  38.               proposalsCount: {
  39.                 $size: '$proposals',
  40.               },
  41.               readProposalsCount: {
  42.                 $size: '$readProposals',
  43.               },
  44.             },
  45.           },
  46.           {
  47.             $project: {
  48.               password: 0,
  49.               proposals: 0,
  50.               readProposals: 0,
  51.             },
  52.           },
  53.         ]);
  54.       })
  55.       .then(user => user[0])
  56.       .then(user => {
  57.         if (!user) {
  58.           throw Boom.notFound('no such user');
  59.         }
  60.         res.send(mongoToRes(user));
  61.       })
  62.       .catch(err => {
  63.         debug(err);
  64.         throw handleError(err);
  65.       });
  66.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement