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
This commit is contained in:
Travis
2018-03-17 14:26:24 -07:00
committed by Matteo Pagliazzi
parent 45eb19e992
commit 8c316d939f
3 changed files with 20 additions and 4 deletions

View File

@@ -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);

View File

@@ -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);

View File

@@ -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