async'ify the groups code
This commit is contained in:
+29
-40
@@ -3,88 +3,77 @@
|
||||
exports = module.exports = {
|
||||
get,
|
||||
list,
|
||||
create,
|
||||
add,
|
||||
update,
|
||||
remove,
|
||||
updateMembers
|
||||
};
|
||||
|
||||
var assert = require('assert'),
|
||||
const assert = require('assert'),
|
||||
BoxError = require('../boxerror.js'),
|
||||
groups = require('../groups.js'),
|
||||
HttpError = require('connect-lastmile').HttpError,
|
||||
HttpSuccess = require('connect-lastmile').HttpSuccess;
|
||||
HttpSuccess = require('connect-lastmile').HttpSuccess,
|
||||
safe = require('safetydance');
|
||||
|
||||
function create(req, res, next) {
|
||||
async function add(req, res, next) {
|
||||
assert.strictEqual(typeof req.body, 'object');
|
||||
|
||||
if (typeof req.body.name !== 'string') return next(new HttpError(400, 'name must be string'));
|
||||
|
||||
var source = ''; // means local
|
||||
const [error, group] = await safe(groups.add({ name: req.body.name, source : '' }));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
groups.create(req.body.name, source, function (error, group) {
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
var groupInfo = {
|
||||
id: group.id,
|
||||
name: group.name
|
||||
};
|
||||
|
||||
next(new HttpSuccess(201, groupInfo));
|
||||
});
|
||||
next(new HttpSuccess(201, { id: group.id, name: group.name }));
|
||||
}
|
||||
|
||||
function get(req, res, next) {
|
||||
async function get(req, res, next) {
|
||||
assert.strictEqual(typeof req.params.groupId, 'string');
|
||||
|
||||
groups.getWithMembers(req.params.groupId, function (error, result) {
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
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'));
|
||||
|
||||
next(new HttpSuccess(200, result));
|
||||
});
|
||||
next(new HttpSuccess(200, result));
|
||||
}
|
||||
|
||||
function update(req, res, next) {
|
||||
async function update(req, res, next) {
|
||||
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'));
|
||||
|
||||
groups.update(req.params.groupId, req.body, function (error) {
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
const [error] = await safe(groups.update(req.params.groupId, req.body));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, { }));
|
||||
});
|
||||
next(new HttpSuccess(200, { }));
|
||||
}
|
||||
|
||||
function updateMembers(req, res, next) {
|
||||
async function updateMembers(req, res, next) {
|
||||
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'));
|
||||
if (req.body.userIds.some((u) => typeof u !== 'string')) return next(new HttpError(400, 'userIds array must contain strings'));
|
||||
|
||||
groups.setMembers(req.params.groupId, req.body.userIds, function (error) {
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
const [error] = await safe(groups.setMembers(req.params.groupId, req.body.userIds));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, { }));
|
||||
});
|
||||
next(new HttpSuccess(200, { }));
|
||||
}
|
||||
|
||||
function list(req, res, next) {
|
||||
groups.getAllWithMembers(function (error, result) {
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
async function list(req, res, next) {
|
||||
const [error, groups] = await safe(groups.getAllWithMembers());
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, { groups: result }));
|
||||
});
|
||||
next(new HttpSuccess(200, { groups }));
|
||||
}
|
||||
|
||||
function remove(req, res, next) {
|
||||
async function remove(req, res, next) {
|
||||
assert.strictEqual(typeof req.params.groupId, 'string');
|
||||
|
||||
groups.remove(req.params.groupId, function (error) {
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
const [error] = await safe(groups.remove(req.params.groupId));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(204));
|
||||
});
|
||||
next(new HttpSuccess(204));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user