async'ify avatar and apppassword code

This commit is contained in:
Girish Ramakrishnan
2021-06-25 22:11:17 -07:00
parent 31d742fa67
commit 147c8df6e3
11 changed files with 385 additions and 354 deletions
+21 -22
View File
@@ -7,55 +7,54 @@ exports = module.exports = {
add
};
var assert = require('assert'),
const appPasswords = require('../apppasswords.js'),
assert = require('assert'),
BoxError = require('../boxerror.js'),
HttpError = require('connect-lastmile').HttpError,
HttpSuccess = require('connect-lastmile').HttpSuccess,
users = require('../users.js');
safe = require('safetydance');
function get(req, res, next) {
async function get(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
assert.strictEqual(typeof req.params.id, 'string');
users.getAppPassword(req.params.id, function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, result] = await safe(appPasswords.get(req.params.id));
if (error) return next(BoxError.toHttpError(error));
if (!result) return next(new HttpError(404, 'appPassword not found'));
next(new HttpSuccess(200, result));
});
next(new HttpSuccess(200, appPasswords.removePrivateFields(result)));
}
function add(req, res, next) {
async function add(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.name !== 'string') return next(new HttpError(400, 'name must be string'));
if (typeof req.body.identifier !== 'string') return next(new HttpError(400, 'identifier must be string'));
users.addAppPassword(req.user.id, req.body.identifier, req.body.name, function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, result] = await safe(appPasswords.add(req.user.id, req.body.identifier, req.body.name));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(201, result));
});
next(new HttpSuccess(201, { id: result.id, password: result.password }));
}
function list(req, res, next) {
async function list(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
users.getAppPasswords(req.user.id, function (error, result) {
if (error) return next(BoxError.toHttpError(error));
let [error, result] = await safe(appPasswords.list(req.user.id));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { appPasswords: result }));
});
result = result.map(appPasswords.removePrivateFields);
next(new HttpSuccess(200, { appPasswords: result }));
}
function del(req, res, next) {
async function del(req, res, next) {
assert.strictEqual(typeof req.user, 'object');
assert.strictEqual(typeof req.params.id, 'string');
// TODO: verify userId owns the id ?
users.delAppPassword(req.params.id, function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(appPasswords.del(req.params.id));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204, {}));
});
next(new HttpSuccess(204, {}));
}