Added quest leave route and initial tests

This commit is contained in:
Keith Holliday
2016-02-04 12:31:38 -06:00
parent e3c7d2834e
commit 5ca663db57
3 changed files with 143 additions and 1 deletions

View File

@@ -8,6 +8,7 @@ import {
NotAuthorized,
} from '../../libs/api-v3/errors';
import { quests as questScrolls } from '../../../../common/script/content';
import Q from 'q';
let api = {};
@@ -65,4 +66,57 @@ api.inviteToQuest = {
},
};
/**
* @api {post} /groups/:groupId/quests/leave Leaves a quest
* @apiVersion 3.0.0
* @apiName LeaveQuest
* @apiGroup Group
*
* @apiParam {string} groupId The group _id (or 'party')
*
* @apiSuccess {Object} Empty Object
*/
api.leaveQuest = {
method: 'POST',
url: '/groups/:groupId/quests/leave',
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, fields: 'type quest'});
if (!group) throw new NotFound(res.t('groupNotFound'));
if (!(group.quest && group.quest.active)) {
throw new NotFound(res.t('noActiveQuestToLeave'));
}
if (group.quest.leader === user._id) {
throw new NotAuthorized(res.t('questLeaderCannotLeaveQuest'));
}
if (!(group.quest.members && group.quest.members[user._id])) {
throw new NotAuthorized(res.t('notPartOfQuest'));
}
group.quest.members[user._id] = false;
group.markModified('quest.members');
user.party.quest = Group.cleanQuestProgress();
user.markModified('party.quest');
let [savedGroup] = await Q.all([
group.save(),
user.save(),
]);
res.respond(200, savedGroup.quest);
},
};
export default api;