Files
cloudron-box/src/routes/groups.js
T

80 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-02-09 13:34:01 -08:00
'use strict';
exports = module.exports = {
2021-05-01 11:21:09 -07:00
get,
list,
2021-06-28 15:15:28 -07:00
add,
2021-05-01 11:21:09 -07:00
update,
remove,
updateMembers
2016-02-09 13:34:01 -08:00
};
2021-06-28 15:15:28 -07:00
const 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,
2021-06-28 15:15:28 -07:00
HttpSuccess = require('connect-lastmile').HttpSuccess,
safe = require('safetydance');
2019-10-22 16:34:17 -07:00
2021-06-28 15:15:28 -07:00
async function add(req, res, next) {
2016-02-09 13:34:01 -08:00
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
2021-06-28 15:15:28 -07:00
const [error, group] = await safe(groups.add({ name: req.body.name, source : '' }));
if (error) return next(BoxError.toHttpError(error));
2020-06-04 14:17:56 +02:00
2021-06-28 15:15:28 -07:00
next(new HttpSuccess(201, { id: group.id, name: group.name }));
2016-02-09 13:34:01 -08:00
}
2021-06-28 15:15:28 -07:00
async function get(req, res, next) {
2016-02-09 13:34:01 -08:00
assert.strictEqual(typeof req.params.groupId, 'string');
2021-06-28 15:15:28 -07:00
const [error, result] = await safe(groups.getWithMembers(req.params.groupId));
if (error) return next(BoxError.toHttpError(error));
if (!result) return next(new HttpError(404, 'Group not found'));
2016-02-09 13:34:01 -08:00
2021-06-28 15:15:28 -07:00
next(new HttpSuccess(200, result));
2016-02-09 13:34:01 -08:00
}
2021-06-28 15:15:28 -07:00
async 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');
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
2021-06-28 15:15:28 -07:00
const [error] = await safe(groups.update(req.params.groupId, req.body));
if (error) return next(BoxError.toHttpError(error));
2018-06-14 22:42:40 -07:00
2021-06-28 15:15:28 -07:00
next(new HttpSuccess(200, { }));
2018-06-14 22:42:40 -07:00
}
2021-06-28 15:15:28 -07:00
async 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
2021-06-28 15:15:28 -07:00
const [error] = await safe(groups.setMembers(req.params.groupId, req.body.userIds));
if (error) return next(BoxError.toHttpError(error));
2016-09-29 14:44:12 -07:00
2021-06-28 15:15:28 -07:00
next(new HttpSuccess(200, { }));
2016-09-29 14:44:12 -07:00
}
2021-06-28 15:15:28 -07:00
async function list(req, res, next) {
2021-06-29 09:44:16 -07:00
const [error, result] = await safe(groups.listWithMembers());
2021-06-28 15:15:28 -07:00
if (error) return next(BoxError.toHttpError(error));
2016-02-09 15:26:34 -08:00
2021-06-29 09:44:16 -07:00
next(new HttpSuccess(200, { groups: result }));
2016-02-09 13:34:01 -08:00
}
2021-06-28 15:15:28 -07:00
async function remove(req, res, next) {
2016-02-09 13:34:01 -08:00
assert.strictEqual(typeof req.params.groupId, 'string');
2021-06-28 15:15:28 -07:00
const [error] = await safe(groups.remove(req.params.groupId));
if (error) return next(BoxError.toHttpError(error));
2016-02-09 13:34:01 -08:00
2021-06-28 15:15:28 -07:00
next(new HttpSuccess(204));
2016-02-09 13:34:01 -08:00
}