Made challenge paging optional

This commit is contained in:
Keith Holliday
2018-03-30 11:34:00 -05:00
parent 0d65e5219e
commit 2f69f4039e
3 changed files with 25 additions and 11 deletions

View File

@@ -249,6 +249,8 @@ describe('GET challenges/user', () => {
guild = group; guild = group;
member = members[0]; member = members[0];
await user.update({balance: 20});
for (let i = 0; i < 11; i += 1) { for (let i = 0; i < 11; i += 1) {
await generateChallenge(user, group); // eslint-disable-line await generateChallenge(user, group); // eslint-disable-line
} }
@@ -262,8 +264,14 @@ describe('GET challenges/user', () => {
expect(challenges.length).to.eql(1); expect(challenges.length).to.eql(1);
}); });
it('paginates challenges', async () => { it('does not page challenges if page parameter is absent', async () => {
const challenges = await user.get('/challenges/user'); const challenges = await user.get('/challenges/user');
expect(challenges.length).to.eql(12);
});
it('paginates challenges', async () => {
const challenges = await user.get('/challenges/user?page=0');
const challengesPaged = await user.get('/challenges/user?page=1&owned=owned'); const challengesPaged = await user.get('/challenges/user?page=1&owned=owned');
expect(challenges.length).to.eql(10); expect(challenges.length).to.eql(10);

View File

@@ -45,7 +45,7 @@ export async function getUserChallenges (store, payload) {
let query = {}; let query = {};
if (member) query.member = member; if (member) query.member = member;
if (page) query.page = page; if (page >= 0) query.page = page;
if (search) query.search = search; if (search) query.search = search;
if (categories) query.categories = categories; if (categories) query.categories = categories;
if (owned) query.owned = owned; if (owned) query.owned = owned;

View File

@@ -341,7 +341,7 @@ api.getUserChallenges = {
middlewares: [authWithHeaders()], middlewares: [authWithHeaders()],
async handler (req, res) { async handler (req, res) {
const CHALLENGES_PER_PAGE = 10; const CHALLENGES_PER_PAGE = 10;
const page = req.query.page || 0; const page = req.query.page;
const user = res.locals.user; const user = res.locals.user;
let orOptions = [ let orOptions = [
@@ -385,14 +385,20 @@ api.getUserChallenges = {
query.categories = { $elemMatch: { slug: {$in: categorySlugs} } }; query.categories = { $elemMatch: { slug: {$in: categorySlugs} } };
} }
const challenges = await Challenge.find(query) let mongoQuery = Challenge.find(query)
.sort('-createdAt') .sort('-createdAt');
.limit(CHALLENGES_PER_PAGE)
.skip(CHALLENGES_PER_PAGE * page) if (page) {
// see below why we're not using populate mongoQuery = mongoQuery
// .populate('group', basicGroupFields) .limit(CHALLENGES_PER_PAGE)
// .populate('leader', nameFields) .skip(CHALLENGES_PER_PAGE * page);
.exec(); }
// see below why we're not using populate
// .populate('group', basicGroupFields)
// .populate('leader', nameFields)
const challenges = await mongoQuery.exec();
let resChals = challenges.map(challenge => challenge.toJSON()); let resChals = challenges.map(challenge => challenge.toJSON());