Merge pull request #6590 from TheHollidayInn/api-v3-get-group-challenges

Added get group challenges route and initial tests
This commit is contained in:
Matteo Pagliazzi
2016-02-02 19:06:37 +01:00
3 changed files with 203 additions and 5 deletions
+43 -5
View File
@@ -121,7 +121,8 @@ api.joinChallenge = {
let challenge = await Challenge.findOne({ _id: req.params.challengeId });
if (!challenge) throw new NotFound(res.t('challengeNotFound'));
if (!challenge.hasAccess(user)) throw new NotFound(res.t('challengeNotFound'));
let group = await Group.getGroup({user, groupId: challenge.groupId, fields: '_id type privacy', optionalMembership: true});
if (!group || !challenge.canView(user, group)) throw new NotFound(res.t('challengeNotFound'));
if (_.contains(user.challenges, challenge._id)) throw new NotAuthorized(res.t('userAlreadyInChallenge'));
@@ -172,16 +173,16 @@ api.leaveChallenge = {
};
/**
* @api {get} /challenges Get challenges for a user
* @api {get} /challenges/user Get challenges for a user
* @apiVersion 3.0.0
* @apiName GetChallenges
* @apiName GetUserChallenges
* @apiGroup Challenge
*
* @apiSuccess {Array} challenges An array of challenges
*/
api.getChallenges = {
api.getUserChallenges = {
method: 'GET',
url: '/challenges',
url: '/challenges/user',
middlewares: [authWithHeaders(), cron],
async handler (req, res) {
let user = res.locals.user;
@@ -208,6 +209,43 @@ api.getChallenges = {
},
};
/**
* @api {get} /challenges/group/group:Id Get challenges for a group
* @apiVersion 3.0.0
* @apiName GetGroupChallenges
* @apiGroup Challenge
*
* @apiParam {groupId} groupId The group _id
*
* @apiSuccess {Array} challenges An array of challenges
*/
api.getGroupChallenges = {
method: 'GET',
url: '/challenges/groups/:groupId',
middlewares: [authWithHeaders(), cron],
async handler (req, res) {
let user = res.locals.user;
let groupId = req.params.groupId;
req.checkParams('groupId', res.t('groupIdRequired')).notEmpty();
let validationErrors = req.validationErrors();
if (validationErrors) throw validationErrors;
let group = await Group.getGroup({user, groupId});
if (!group) throw new NotFound(res.t('groupNotFound'));
let challenges = await Challenge.find({groupId})
.sort('-official -timestamp')
// TODO populate
// .populate('group', '_id name type')
// .populate('leader', 'profile.name')
.exec();
res.respond(200, challenges);
},
};
/**
* @api {get} /challenges/:challengeId Get a challenge given its id
* @apiVersion 3.0.0