[#1675] fixes to populating Group.challenges, and display challenges in party

page. I'm not sure about having challenges as a subdoc anymore, i think
it will be better performance and maintenance if we just reference
Challenge.group/members and remove User.challenges & Group.challenges
This commit is contained in:
Tyler Renelle
2013-10-31 16:20:48 -07:00
parent 4afcd406f5
commit b3804c4d1f
4 changed files with 25 additions and 15 deletions

View File

@@ -156,7 +156,7 @@ api.create = function(req, res){
var challenge = new Challenge(req.body); // FIXME sanitize
challenge.save(function(err, saved){
if (err) return res.json(500, {err:err});
Group.findByIdAndUpdate(saved.group, {$addToSet:{challenges:saved._id}}) // fixme error-check, and also better to do in middleware?
Group.update({_id:saved.group}, {$addToSet:{challenges:saved._id}}) // fixme error-check, and also better to do in middleware?
res.json(saved);
});
});

View File

@@ -19,6 +19,7 @@ var api = module.exports;
var itemFields = 'items.armor items.head items.shield items.weapon items.currentPet';
var partyFields = 'profile preferences stats achievements party backer flags.rest auth.timestamps ' + itemFields;
var nameFields = 'profile.name';
var challengeFields = '_id name';
api.getMember = function(req, res) {
User.findById(req.params.uid).select(partyFields).exec(function(err, user){
@@ -106,18 +107,25 @@ api.get = function(req, res) {
// This will be called for the header, we need extra members' details than usuals
if (gid == 'party') {
Group.findOne({type: 'party', members: {'$in': [user._id]}})
.populate('members invites', partyFields).exec(function(err, group){
.populate('members', partyFields)
.populate('invites', nameFields)
.populate('challenges', challengeFields)
.exec(function(err, group){
if (err) return res.json(500,{err:err});
res.json(group);
});
} else {
Group.findById(gid).populate('members invites', nameFields).exec(function(err, group){
if ( (group.type == 'guild' && group.privacy == 'private') || group.type == 'party') {
if(!_.find(group.members, {_id: user._id}))
return res.json(401, {err: "You don't have access to this group"});
}
res.json(group);
})
Group.findById(gid)
.populate('members', partyFields)
.populate('invites', nameFields)
.populate('challenges', challengeFields)
.exec(function(err, group){
if ( (group.type == 'guild' && group.privacy == 'private') || group.type == 'party') {
if(!_.find(group.members, {_id: user._id}))
return res.json(401, {err: "You don't have access to this group"});
}
res.json(group);
});
}
};

View File

@@ -31,7 +31,7 @@ var GroupSchema = new Schema({
balance: Number,
logo: String,
leaderMessage: String,
challenges: [{type:'String', ref:'Challenge'}]
challenges: [{type:'String', ref:'Challenge'}] // do we need this? could depend on back-ref instead (Challenge.find({group:GID}))
}, {
strict: 'throw',
minimize: false // So empty objects are returned

View File

@@ -84,17 +84,19 @@ a.pull-right.gem-wallet(popover-trigger='mouseenter', popover-title='Guild Bank'
.modal(style='position: relative;top: auto;left: auto;right: auto;margin: 0 auto 20px;z-index: 1;max-width: 100%;')
.modal-header
h3 Challenges
h3
| Challenges
a.pull-right(target='_blank', href='https://trello.com/card/challenges-individual-party-guild-public/50e5d3684fe3a7266b0036d6/58')
i.icon-question-sign
.modal-body
a(target='_blank', href='https://trello.com/card/challenges-individual-party-guild-public/50e5d3684fe3a7266b0036d6/58') Details
div(ng-show='group.challenges')
div(ng-if='group.challenges')
table.table.table-striped
tr(ng-repeat='challenge in group.challenges')
td
| {{challenge.name}}
a(ui-sref='options.social.challenges.detail({cid:challenge._id})') {{challenge.name}}
p.
Visit the <span class=label><i class=icon-bullhorn></i> Challenges</span> for more information.
div(ng-hid='group.challenges')
div(ng-if='!group.challenges')
p.
No challenges yet, visit the <span class=label><i class=icon-bullhorn></i> Challenges</span> tab to create one.