v3: fix crashes when group or leader cannot be populated and fixes challenges migration for tavern challenges

This commit is contained in:
Matteo Pagliazzi
2016-05-07 17:35:13 +02:00
parent 4e3d4c8831
commit 77d2d943ae
3 changed files with 22 additions and 9 deletions

View File

@@ -112,6 +112,15 @@ function processChallenges (afterId) {
if (!oldChallenge.group) throw new Error('challenge.group is required'); if (!oldChallenge.group) throw new Error('challenge.group is required');
if (!oldChallenge.leader) throw new Error('challenge.leader is required'); if (!oldChallenge.leader) throw new Error('challenge.leader is required');
if (oldChallenge.leader === '9') {
oldChallenge.leader = '00000000-0000-4000-9000-000000000000';
}
if (oldChallenge.group === 'habitrpg') {
oldChallenge.group = '00000000-0000-4000-A000-000000000000';
}
delete oldChallenge.id; delete oldChallenge.id;
var newChallenge = new NewChallenge(oldChallenge); var newChallenge = new NewChallenge(oldChallenge);

View File

@@ -61,8 +61,8 @@ api.list = async function(req, res, next) {
User.findById(chal.leader).select(nameFields).exec(), User.findById(chal.leader).select(nameFields).exec(),
Group.findById(chal.group).select(basicGroupFields).exec(), Group.findById(chal.group).select(basicGroupFields).exec(),
]).then(populatedData => { ]).then(populatedData => {
resChals[index].leader = populatedData[0].toJSON({minimize: true}); resChals[index].leader = populatedData[0] ? populatedData[0].toJSON({minimize: true}) : null;
resChals[index].group = populatedData[1].toJSON({minimize: true}); resChals[index].group = populatedData[1] ? populatedData[1].toJSON({minimize: true}) : null;
}); });
})); }));
@@ -88,7 +88,8 @@ api.get = async function(req, res, next) {
let group = await Group.getGroup({user, groupId: challenge.group, optionalMembership: true}); let group = await Group.getGroup({user, groupId: challenge.group, optionalMembership: true});
if (!group || !challenge.canView(user, group)) return res.status(404).json({err: 'Challenge ' + req.params.cid + ' not found'}); if (!group || !challenge.canView(user, group)) return res.status(404).json({err: 'Challenge ' + req.params.cid + ' not found'});
let leaderRes = (await User.findById(challenge.leader).select('profile.name').exec()).toJSON({minimize: true}); let leaderRes = await User.findById(challenge.leader).select('profile.name').exec();
leaderRes = leaderRes ? leaderRes.toJSON({minimize: true}) : null;
challenge.getTransformedData({ challenge.getTransformedData({
populateMembers: 'profile.name', populateMembers: 'profile.name',

View File

@@ -153,7 +153,8 @@ api.joinChallenge = {
type: group.type, type: group.type,
privacy: group.privacy, privacy: group.privacy,
}; };
response.leader = (await User.findById(response.leader).select(nameFields).exec()).toJSON({minimize: true}); let chalLeader = await User.findById(response.leader).select(nameFields).exec();
response.leader = chalLeader ? chalLeader.toJSON({minimize: true}) : null;
res.respond(200, response); res.respond(200, response);
}, },
@@ -233,8 +234,8 @@ api.getUserChallenges = {
User.findById(chal.leader).select(nameFields).exec(), User.findById(chal.leader).select(nameFields).exec(),
Group.findById(chal.group).select(basicGroupFields).exec(), Group.findById(chal.group).select(basicGroupFields).exec(),
]).then(populatedData => { ]).then(populatedData => {
resChals[index].leader = populatedData[0].toJSON({minimize: true}); resChals[index].leader = populatedData[0] ? populatedData[0].toJSON({minimize: true}) : null;
resChals[index].group = populatedData[1].toJSON({minimize: true}); resChals[index].group = populatedData[1] ? populatedData[1].toJSON({minimize: true}) : null;
}); });
})); }));
@@ -278,7 +279,7 @@ api.getGroupChallenges = {
// Instead of populate we make a find call manually because of https://github.com/Automattic/mongoose/issues/3833 // Instead of populate we make a find call manually because of https://github.com/Automattic/mongoose/issues/3833
await Q.all(resChals.map((chal, index) => { await Q.all(resChals.map((chal, index) => {
return User.findById(chal.leader).select(nameFields).exec().then(populatedLeader => { return User.findById(chal.leader).select(nameFields).exec().then(populatedLeader => {
resChals[index].leader = populatedLeader.toJSON({minimize: true}); resChals[index].leader = populatedLeader ? populatedLeader.toJSON({minimize: true}) : null;
}); });
})); }));
@@ -322,7 +323,8 @@ api.getChallenge = {
let chalRes = challenge.toJSON(); let chalRes = challenge.toJSON();
chalRes.group = group.toJSON({minimize: true}); chalRes.group = group.toJSON({minimize: true});
// Instead of populate we make a find call manually because of https://github.com/Automattic/mongoose/issues/3833 // Instead of populate we make a find call manually because of https://github.com/Automattic/mongoose/issues/3833
chalRes.leader = (await User.findById(chalRes.leader).select(nameFields).exec()).toJSON({minimize: true}); let chalLeader = await User.findById(chalRes.leader).select(nameFields).exec();
chalRes.leader = chalLeader ? chalLeader.toJSON({minimize: true}) : null;
res.respond(200, chalRes); res.respond(200, chalRes);
}, },
@@ -441,7 +443,8 @@ api.updateChallenge = {
type: group.type, type: group.type,
privacy: group.privacy, privacy: group.privacy,
}; };
response.leader = (await User.findById(response.leader).select(nameFields).exec()).toJSON({minimize: true}); let chalLeader = await User.findById(response.leader).select(nameFields).exec();
response.leader = chalLeader ? chalLeader.toJSON({minimize: true}) : null;
res.respond(200, response); res.respond(200, response);
}, },
}; };