challenges: stats setup

This commit is contained in:
Tyler Renelle
2013-10-27 17:46:39 -07:00
parent 44d67ac9f8
commit d62586ef0a
2 changed files with 21 additions and 30 deletions

View File

@@ -19,12 +19,25 @@ var api = module.exports;
// GET
api.get = function(req, res) {
// TODO only find in user's groups (+ public)
// TODO populate (group, leader, members)
Challenge.find({},function(err, challenges){
if(err) return res.json(500, {err:err});
res.json(challenges);
})
var user = res.locals.user;
Challenge.find({$or:[{leader: user._id}, {members:{$in:[user._id]}}]})
.populate('members', 'profile.name habits dailys rewards todos')
.exec(function(err, challenges){
if(err) return res.json(500, {err:err});
// slim down the return members' tasks to only the ones in the challenge
_.each(challenges, function(challenge){
_.each(challenge.members, function(member){
_.each(['habits', 'dailys', 'todos', 'rewards'], function(type){
member[type] = _.where(member[type], function(task){
return task.challenge && task.challenge.id == challenge._id;
})
})
})
});
res.json(challenges);
})
}
// CREATE