mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-19 07:37:25 +01:00
add updateGroup route
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user