From 77d2d943aec442b1fd89756071f309ca87608d35 Mon Sep 17 00:00:00 2001 From: Matteo Pagliazzi Date: Sat, 7 May 2016 17:35:13 +0200 Subject: [PATCH] v3: fix crashes when group or leader cannot be populated and fixes challenges migration for tavern challenges --- migrations/api_v3/challenges.js | 9 +++++++++ website/src/controllers/api-v2/challenges.js | 7 ++++--- website/src/controllers/api-v3/challenges.js | 15 +++++++++------ 3 files changed, 22 insertions(+), 9 deletions(-) diff --git a/migrations/api_v3/challenges.js b/migrations/api_v3/challenges.js index 85a019cc7d..77972172dd 100644 --- a/migrations/api_v3/challenges.js +++ b/migrations/api_v3/challenges.js @@ -112,6 +112,15 @@ function processChallenges (afterId) { if (!oldChallenge.group) throw new Error('challenge.group 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; var newChallenge = new NewChallenge(oldChallenge); diff --git a/website/src/controllers/api-v2/challenges.js b/website/src/controllers/api-v2/challenges.js index 51f57433c7..404fa379d1 100644 --- a/website/src/controllers/api-v2/challenges.js +++ b/website/src/controllers/api-v2/challenges.js @@ -61,8 +61,8 @@ api.list = async function(req, res, next) { User.findById(chal.leader).select(nameFields).exec(), Group.findById(chal.group).select(basicGroupFields).exec(), ]).then(populatedData => { - resChals[index].leader = populatedData[0].toJSON({minimize: true}); - resChals[index].group = populatedData[1].toJSON({minimize: true}); + resChals[index].leader = populatedData[0] ? populatedData[0].toJSON({minimize: true}) : null; + 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}); 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({ populateMembers: 'profile.name', diff --git a/website/src/controllers/api-v3/challenges.js b/website/src/controllers/api-v3/challenges.js index f7bb61971f..b334dedc41 100644 --- a/website/src/controllers/api-v3/challenges.js +++ b/website/src/controllers/api-v3/challenges.js @@ -153,7 +153,8 @@ api.joinChallenge = { type: group.type, 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); }, @@ -233,8 +234,8 @@ api.getUserChallenges = { User.findById(chal.leader).select(nameFields).exec(), Group.findById(chal.group).select(basicGroupFields).exec(), ]).then(populatedData => { - resChals[index].leader = populatedData[0].toJSON({minimize: true}); - resChals[index].group = populatedData[1].toJSON({minimize: true}); + resChals[index].leader = populatedData[0] ? populatedData[0].toJSON({minimize: true}) : null; + 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 await Q.all(resChals.map((chal, index) => { 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(); chalRes.group = group.toJSON({minimize: true}); // 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); }, @@ -441,7 +443,8 @@ api.updateChallenge = { type: group.type, 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); }, };