2016-02-09 13:34:01 -08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
|
|
|
|
get: get,
|
|
|
|
|
list: list,
|
|
|
|
|
create: create,
|
2018-06-18 13:41:27 -07:00
|
|
|
update: update,
|
2016-09-29 14:44:12 -07:00
|
|
|
remove: remove,
|
2016-10-02 18:28:50 -07:00
|
|
|
updateMembers: updateMembers
|
2016-02-09 13:34:01 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var assert = require('assert'),
|
2019-10-22 16:34:17 -07:00
|
|
|
BoxError = require('../boxerror.js'),
|
2016-02-09 13:34:01 -08:00
|
|
|
groups = require('../groups.js'),
|
|
|
|
|
HttpError = require('connect-lastmile').HttpError,
|
2019-10-22 16:34:17 -07:00
|
|
|
HttpSuccess = require('connect-lastmile').HttpSuccess;
|
|
|
|
|
|
2016-02-09 13:34:01 -08:00
|
|
|
function create(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
|
|
|
|
if (typeof req.body.name !== 'string') return next(new HttpError(400, 'name must be string'));
|
2018-07-26 10:20:19 -07:00
|
|
|
|
2020-06-04 14:17:56 +02:00
|
|
|
var source = ''; // means local
|
|
|
|
|
|
|
|
|
|
groups.create(req.body.name, source, function (error, group) {
|
2019-10-24 18:05:14 -07:00
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
2016-02-09 13:34:01 -08:00
|
|
|
|
|
|
|
|
var groupInfo = {
|
|
|
|
|
id: group.id,
|
|
|
|
|
name: group.name
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(201, groupInfo));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function get(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.params.groupId, 'string');
|
|
|
|
|
|
2016-02-09 15:26:34 -08:00
|
|
|
groups.getWithMembers(req.params.groupId, function (error, result) {
|
2019-10-24 18:05:14 -07:00
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
2016-02-09 13:34:01 -08:00
|
|
|
|
|
|
|
|
next(new HttpSuccess(200, result));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-06-18 13:41:27 -07:00
|
|
|
function update(req, res, next) {
|
2018-06-14 22:42:40 -07:00
|
|
|
assert.strictEqual(typeof req.params.groupId, 'string');
|
|
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
2018-06-20 09:31:50 -07:00
|
|
|
if ('name' in req.body && typeof req.body.name !== 'string') return next(new HttpError(400, 'name must be a string'));
|
2018-06-18 18:26:50 -07:00
|
|
|
|
2018-06-20 09:31:50 -07:00
|
|
|
groups.update(req.params.groupId, req.body, function (error) {
|
2019-10-24 18:05:14 -07:00
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
2018-06-14 22:42:40 -07:00
|
|
|
|
|
|
|
|
next(new HttpSuccess(200, { }));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-02 18:28:50 -07:00
|
|
|
function updateMembers(req, res, next) {
|
2016-09-29 14:44:12 -07:00
|
|
|
assert.strictEqual(typeof req.params.groupId, 'string');
|
|
|
|
|
|
|
|
|
|
if (!req.body.userIds) return next(new HttpError(404, 'missing or invalid userIds fields'));
|
|
|
|
|
if (!Array.isArray(req.body.userIds)) return next(new HttpError(404, 'userIds must be an array'));
|
2020-11-22 21:42:08 -08:00
|
|
|
if (req.body.userIds.some((u) => typeof u !== 'string')) return next(new HttpError(400, 'userIds array must contain strings'));
|
2016-09-29 14:44:12 -07:00
|
|
|
|
|
|
|
|
groups.setMembers(req.params.groupId, req.body.userIds, function (error) {
|
2019-10-24 18:05:14 -07:00
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
2016-09-29 14:44:12 -07:00
|
|
|
|
2018-07-24 22:19:34 -07:00
|
|
|
next(new HttpSuccess(200, { }));
|
2016-09-29 14:44:12 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2016-02-09 13:34:01 -08:00
|
|
|
function list(req, res, next) {
|
2020-08-10 13:50:18 -07:00
|
|
|
groups.getAllWithMembers(function (error, result) {
|
2019-10-24 18:05:14 -07:00
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
2016-02-09 15:26:34 -08:00
|
|
|
|
2016-02-09 13:34:01 -08:00
|
|
|
next(new HttpSuccess(200, { groups: result }));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function remove(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.params.groupId, 'string');
|
|
|
|
|
|
|
|
|
|
groups.remove(req.params.groupId, function (error) {
|
2019-10-24 18:05:14 -07:00
|
|
|
if (error) return next(BoxError.toHttpError(error));
|
2016-02-09 13:34:01 -08:00
|
|
|
|
|
|
|
|
next(new HttpSuccess(204));
|
|
|
|
|
});
|
|
|
|
|
}
|