From 8c316d939f1833c72964a665eb0dccb1e9b6c3e0 Mon Sep 17 00:00:00 2001 From: Travis Date: Sat, 17 Mar 2018 14:26:24 -0700 Subject: [PATCH] Updated get challenges api to return challenges sorted by which challenges include the habitica_official category (#10079) and removed sorting on the official flag of the challenges object. fixes #9955 --- .../GET-challenges_group_groupid.test.js | 5 ++++- .../challenges/GET-challenges_user.test.js | 5 ++++- website/server/controllers/api-v3/challenges.js | 14 ++++++++++++-- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/test/api/v3/integration/challenges/GET-challenges_group_groupid.test.js b/test/api/v3/integration/challenges/GET-challenges_group_groupid.test.js index 5c290efc64..f1c73907bf 100644 --- a/test/api/v3/integration/challenges/GET-challenges_group_groupid.test.js +++ b/test/api/v3/integration/challenges/GET-challenges_group_groupid.test.js @@ -151,7 +151,10 @@ describe('GET challenges/groups/:groupId', () => { }); officialChallenge = await generateChallenge(user, group, { - official: true, + categories: [{ + name: 'habitica_official', + slug: 'habitica_official', + }], }); challenge = await generateChallenge(user, group); diff --git a/test/api/v3/integration/challenges/GET-challenges_user.test.js b/test/api/v3/integration/challenges/GET-challenges_user.test.js index 977574807f..946a703851 100644 --- a/test/api/v3/integration/challenges/GET-challenges_user.test.js +++ b/test/api/v3/integration/challenges/GET-challenges_user.test.js @@ -193,7 +193,10 @@ describe('GET challenges/user', () => { }); officialChallenge = await generateChallenge(user, group, { - official: true, + categories: [{ + name: 'habitica_official', + slug: 'habitica_official', + }], }); challenge = await generateChallenge(user, group); diff --git a/website/server/controllers/api-v3/challenges.js b/website/server/controllers/api-v3/challenges.js index 2b9aacdcc9..fa89accff0 100644 --- a/website/server/controllers/api-v3/challenges.js +++ b/website/server/controllers/api-v3/challenges.js @@ -355,13 +355,18 @@ api.getUserChallenges = { let challenges = await Challenge.find({ $or: orOptions, }) - .sort('-official -createdAt') + .sort('-createdAt') // see below why we're not using populate // .populate('group', basicGroupFields) // .populate('leader', nameFields) .exec(); let resChals = challenges.map(challenge => challenge.toJSON()); + + resChals = _.orderBy(resChals, [challenge => { + return challenge.categories.map(category => category.slug).includes('habitica_official'); + }], ['desc']); + // Instead of populate we make a find call manually because of https://github.com/Automattic/mongoose/issues/3833 await Promise.all(resChals.map((chal, index) => { return Promise.all([ @@ -412,11 +417,16 @@ api.getGroupChallenges = { if (!group) throw new NotFound(res.t('groupNotFound')); let challenges = await Challenge.find({group: groupId}) - .sort('-official -createdAt') + .sort('-createdAt') // .populate('leader', nameFields) // Only populate the leader as the group is implicit .exec(); let resChals = challenges.map(challenge => challenge.toJSON()); + + resChals = _.orderBy(resChals, [challenge => { + return challenge.categories.map(category => category.slug).includes('habitica_official'); + }], ['desc']); + // Instead of populate we make a find call manually because of https://github.com/Automattic/mongoose/issues/3833 await Promise.all(resChals.map((chal, index) => { return User