add updateGroup route

This commit is contained in:
Matteo Pagliazzi
2015-12-28 19:45:34 +01:00
parent ceb20742dc
commit 9748ffb0a6
2 changed files with 45 additions and 0 deletions

View File

@@ -160,6 +160,45 @@ api.getGroup = {
},
};
/**
* @api {put} /groups/:groupId Update group
* @apiVersion 3.0.0
* @apiName UpdateGroup
* @apiGroup Group
*
* @apiParam {string} groupId The group _id (or 'party')
*
* @apiSuccess {Object} group The updated group object
*/
api.updateGroup = {
method: 'PUT',
url: '/groups/:groupId',
middlewares: [authWithHeaders(), cron],
handler (req, res, next) {
let user = res.locals.user;
req.checkParams('groupId', res.t('groupIdRequired')).notEmpty();
let validationErrors = req.validationErrors();
if (validationErrors) return next(validationErrors);
Group.getGroup(user, req.params.groupId)
.then(group => {
if (!group) throw new NotFound(res.t('groupNotFound'));
if (group.leader !== user._id) throw new NotAuthorized(res.t('messageGroupOnlyLeaderCanUpdate'));
_.assign(group, _.merge(group.toObject(), Group.sanitizeUpdate(req.body)));
return group.save();
}).then(savedGroup => {
res.respond(200, savedGroup);
firebase.updateGroupData(savedGroup);
})
.catch(next);
},
};
/**
* @api {post} /groups/:groupId/join Join a group
* @apiVersion 3.0.0

View File

@@ -73,6 +73,12 @@ schema.plugin(baseModel, {
noSet: ['_id', 'balance', 'quest', 'memberCount', 'chat', 'challengeCount'],
});
// A list of additional fields that cannot be updated (but can be set on creation)
let noUpdate = ['privacy', 'type'];
schema.statics.sanitizeUpdate = function sanitizeUpdate (updateObj) {
return model.sanitize(updateObj, noUpdate); // eslint-disable-line no-use-before-define
};
// TODO migration
/**
* Derby duplicated stuff. This is a temporary solution, once we're completely off derby we'll run an mongo migration