mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-13 04:37:36 +01:00
Made challenge paging optional
This commit is contained in:
@@ -249,6 +249,8 @@ describe('GET challenges/user', () => {
|
||||
guild = group;
|
||||
member = members[0];
|
||||
|
||||
await user.update({balance: 20});
|
||||
|
||||
for (let i = 0; i < 11; i += 1) {
|
||||
await generateChallenge(user, group); // eslint-disable-line
|
||||
}
|
||||
@@ -262,8 +264,14 @@ describe('GET challenges/user', () => {
|
||||
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');
|
||||
|
||||
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');
|
||||
|
||||
expect(challenges.length).to.eql(10);
|
||||
|
||||
@@ -45,7 +45,7 @@ export async function getUserChallenges (store, payload) {
|
||||
|
||||
let query = {};
|
||||
if (member) query.member = member;
|
||||
if (page) query.page = page;
|
||||
if (page >= 0) query.page = page;
|
||||
if (search) query.search = search;
|
||||
if (categories) query.categories = categories;
|
||||
if (owned) query.owned = owned;
|
||||
|
||||
@@ -341,7 +341,7 @@ api.getUserChallenges = {
|
||||
middlewares: [authWithHeaders()],
|
||||
async handler (req, res) {
|
||||
const CHALLENGES_PER_PAGE = 10;
|
||||
const page = req.query.page || 0;
|
||||
const page = req.query.page;
|
||||
|
||||
const user = res.locals.user;
|
||||
let orOptions = [
|
||||
@@ -385,14 +385,20 @@ api.getUserChallenges = {
|
||||
query.categories = { $elemMatch: { slug: {$in: categorySlugs} } };
|
||||
}
|
||||
|
||||
const challenges = await Challenge.find(query)
|
||||
.sort('-createdAt')
|
||||
let mongoQuery = Challenge.find(query)
|
||||
.sort('-createdAt');
|
||||
|
||||
if (page) {
|
||||
mongoQuery = mongoQuery
|
||||
.limit(CHALLENGES_PER_PAGE)
|
||||
.skip(CHALLENGES_PER_PAGE * page)
|
||||
.skip(CHALLENGES_PER_PAGE * page);
|
||||
}
|
||||
|
||||
// see below why we're not using populate
|
||||
// .populate('group', basicGroupFields)
|
||||
// .populate('leader', nameFields)
|
||||
.exec();
|
||||
const challenges = await mongoQuery.exec();
|
||||
|
||||
|
||||
let resChals = challenges.map(challenge => challenge.toJSON());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user