Challenges join route and tests
This commit is contained in:
committed by
Blade Barringer
parent
b24ff233f2
commit
f37b5a7fac
@@ -1,4 +1,5 @@
|
||||
import { authWithHeaders } from '../../middlewares/api-v3/auth';
|
||||
import _ from 'lodash';
|
||||
import cron from '../../middlewares/api-v3/cron';
|
||||
import { model as Challenge } from '../../models/challenge';
|
||||
import { model as Group } from '../../models/group';
|
||||
@@ -92,6 +93,42 @@ api.createChallenge = {
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* @api {post} /challenges/:challengeId/join Joins a challenge
|
||||
* @apiVersion 3.0.0
|
||||
* @apiName JoinChallenge
|
||||
* @apiGroup Challenge
|
||||
* @apiParam {UUID} challengeId The challenge _id
|
||||
*
|
||||
* @apiSuccess {object} challenge The challenge the user joined
|
||||
*/
|
||||
api.joinChallenge = {
|
||||
method: 'POST',
|
||||
url: '/challenges/:challengeId/join',
|
||||
middlewares: [authWithHeaders(), cron],
|
||||
async handler (req, res) {
|
||||
let user = res.locals.user;
|
||||
|
||||
req.checkParams('challengeId', res.t('challengeIdRequired')).notEmpty().isUUID();
|
||||
|
||||
let validationErrors = req.validationErrors();
|
||||
if (validationErrors) throw validationErrors;
|
||||
|
||||
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'));
|
||||
|
||||
if (_.contains(user.challenges, challenge._id)) throw new NotAuthorized(res.t('userAlreadyInChallenge'));
|
||||
|
||||
challenge.memberCount += 1;
|
||||
|
||||
// Add all challenge's tasks to user's tasks and save the challenge
|
||||
await Q.all([challenge.syncToUser(user), challenge.save()]);
|
||||
res.respond(200, challenge);
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* @api {get} /challenges Get challenges for a user
|
||||
* @apiVersion 3.0.0
|
||||
@@ -144,7 +181,7 @@ api.getChallenge = {
|
||||
url: '/challenges/:challengeId',
|
||||
middlewares: [authWithHeaders(), cron],
|
||||
async handler (req, res) {
|
||||
req.checkQuery('challengeId', res.t('challengeIdRequired')).notEmpty().isUUID();
|
||||
req.checkParams('challengeId', res.t('challengeIdRequired')).notEmpty().isUUID();
|
||||
|
||||
let validationErrors = req.validationErrors();
|
||||
if (validationErrors) throw validationErrors;
|
||||
|
||||
@@ -22,7 +22,7 @@ let schema = new Schema({
|
||||
leader: {type: String, ref: 'User', validate: [validator.isUUID, 'Invalid uuid.'], required: true},
|
||||
groupId: {type: String, ref: 'Group', validate: [validator.isUUID, 'Invalid uuid.'], required: true}, // TODO no update, no set?
|
||||
timestamp: {type: Date, default: Date.now, required: true}, // TODO what is this? use timestamps from plugin? not settable?
|
||||
memberCount: {type: Number, default: 0},
|
||||
memberCount: {type: Number, default: 1},
|
||||
prize: {type: Number, default: 0, min: 0}, // TODO no update?
|
||||
});
|
||||
|
||||
@@ -65,6 +65,13 @@ function _syncableAttrs (task) {
|
||||
return _.omit(t, omitAttrs);
|
||||
}
|
||||
|
||||
schema.methods.hasAccess = function hasAccessToChallenge (user) {
|
||||
let userGroups = user.guilds.slice(0);
|
||||
if (user.party._id) userGroups.push(user.party._id);
|
||||
userGroups.push('habitrpg'); // tavern challenges
|
||||
return this.leader === user._id || userGroups.indexOf(this.groupId) !== -1;
|
||||
};
|
||||
|
||||
// Sync challenge to user, including tasks and tags.
|
||||
// Used when user joins the challenge or to force sync.
|
||||
schema.methods.syncToUser = async function syncChallengeToUser (user) {
|
||||
|
||||
Reference in New Issue
Block a user