From 8f64afe9df5803c3215a282051fa25570c5dfb3a Mon Sep 17 00:00:00 2001 From: SabreCat Date: Tue, 15 Aug 2023 14:54:22 -0500 Subject: [PATCH] fix(challenges): leave chal from invalid group --- website/server/controllers/api-v3/challenges.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/website/server/controllers/api-v3/challenges.js b/website/server/controllers/api-v3/challenges.js index 109422080c..150293c1ed 100644 --- a/website/server/controllers/api-v3/challenges.js +++ b/website/server/controllers/api-v3/challenges.js @@ -577,11 +577,13 @@ api.getChallenge = { const group = await Group.getGroup({ user, groupId: challenge.group, fields: `${basicGroupFields} purchased`, optionalMembership: true, }); - if (!group || !challenge.canView(user, group)) throw new NotFound(res.t('challengeNotFound')); - group.purchased = undefined; - + if (!group && !challenge.canView(user, group)) throw new NotFound(res.t('challengeNotFound')); const chalRes = challenge.toJSON(); - chalRes.group = group.toJSON({ minimize: true }); + if (group) { + group.purchased = undefined; + chalRes.group = group.toJSON({ minimize: true }); + } + // Instead of populate we make a find call manually because of https://github.com/Automattic/mongoose/issues/3833 const chalLeader = await User.findById(chalRes.leader).select(nameFields).exec(); chalRes.leader = chalLeader ? chalLeader.toJSON({ minimize: true }) : null;