Skip to content

Instantly share code, notes, and snippets.

@lefnire
Created April 1, 2014 01:55
Show Gist options
  • Save lefnire/9906282 to your computer and use it in GitHub Desktop.
Save lefnire/9906282 to your computer and use it in GitHub Desktop.
diff --git a/src/controllers/challenges.js b/src/controllers/challenges.js
index 9545f4a..6c74de5 100644
--- a/src/controllers/challenges.js
+++ b/src/controllers/challenges.js
@@ -105,20 +105,26 @@ api.csv = function(req, res, next) {
api.getMember = function(req, res, next) {
var cid = req.params.cid, uid = req.params.uid;
- User.findById(uid)
- .select('profile.name habits dailys todos rewards')
- .exec(function(err, member){
+
+ User.aggregate([
+ {$match:{_id:uid}},
+ {$project:{'profile.name':1,habits:1,dailys:1,todos:1,rewards:1}},
+ {$unwind:"$habits"},
+ {$match:{"habits.challenge.id":cid}},
+ {$group:{_id:"$_id", "habits":{$push:"$habits"}}},
+ {$unwind:"$dailys"},
+ {$match:{"dailys.challenge.id":cid}},
+ {$group:{_id:"$_id", "dailys":{$push:"$dailys"}}},
+ {$unwind:"$todos"},
+ {$match:{"todos.challenge.id":cid}},
+ {$group:{_id:"$_id", "todos":{$push:"$todos"}}},
+ {$unwind:"$rewards"},
+ {$match:{"rewards.challenge.id":cid}},
+ {$group:{_id:"$_id", "rewards":{$push:"$rewards"}}}
+ ]).exec(function(err, member){
+ member = member[0];
if(err) return next(err);
if (!member) return res.json(404, {err: 'Member '+uid+' for challenge '+cid+' not found'});
-
- // Prune un-related tasks. See http://goo.gl/9fnwei
- // See http://stackoverflow.com/a/18546277/362790 for fix
- _.each(['habits','dailys','todos', 'rewards'], function(type){
- member[type] = _.filter(member[type], function(task){
- return task.challenge && task.challenge.id && task.challenge.id == cid;
- });
- });
-
res.json(member);
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment