55 lines
1.3 KiB
JavaScript
55 lines
1.3 KiB
JavaScript
import { authWithHeaders } from '../../middlewares/api-v3/auth';
|
|
import cron from '../../middlewares/api-v3/cron';
|
|
import { model as Group } from '../../models/group';
|
|
import {
|
|
NotFound,
|
|
} from '../../libs/api-v3/errors';
|
|
|
|
let api = {};
|
|
|
|
/**
|
|
* @api {get} /groups/:groupId Get group
|
|
* @apiVersion 3.0.0
|
|
* @apiName GetGroup
|
|
* @apiGroup Group
|
|
*
|
|
* @apiParam {UUID} groupId The group _id
|
|
*
|
|
* @apiSuccess {Object} group The group object
|
|
*/
|
|
api.getGroup = {
|
|
method: 'GET',
|
|
url: '/groups/:groupId',
|
|
middlewares: [authWithHeaders(), cron],
|
|
handler (req, res, next) {
|
|
let user = res.locals.user;
|
|
let groupId = req.params.groupId;
|
|
|
|
req.checkParams('groupId', res.t('groupIdRequired')).notEmpty();
|
|
|
|
let validationErrors = req.validationErrors();
|
|
if (validationErrors) return next(validationErrors);
|
|
|
|
let query;
|
|
|
|
if (groupId === 'party' || user.party._id === groupId) {
|
|
query = {type: 'party', _id: user.party._id};
|
|
} else if (user.guilds.indexOf(groupId) !== -1) {
|
|
query = {type: 'guild', _id: groupId};
|
|
} else {
|
|
query = {type: 'guild', privacy: 'public', _id: groupId};
|
|
}
|
|
|
|
Group
|
|
.findOne(query).exec()
|
|
.then(group => {
|
|
if (!group) throw new NotFound(res.t('groupNotFound'));
|
|
|
|
res.respond(200, group);
|
|
})
|
|
.catch(next);
|
|
},
|
|
};
|
|
|
|
export default api;
|