alias
also remove id since it's not useful for mailbox case (not like mailbox can be renamed and we need a fixed it)
This commit is contained in:
+36
-3
@@ -4,14 +4,17 @@ exports = module.exports = {
|
||||
list: list,
|
||||
get: get,
|
||||
remove: remove,
|
||||
create: create
|
||||
create: create,
|
||||
setAliases: setAliases,
|
||||
getAliases: getAliases
|
||||
};
|
||||
|
||||
var assert = require('assert'),
|
||||
HttpError = require('connect-lastmile').HttpError,
|
||||
HttpSuccess = require('connect-lastmile').HttpSuccess,
|
||||
mailboxes = require('../mailboxes.js'),
|
||||
MailboxError = mailboxes.MailboxError;
|
||||
MailboxError = mailboxes.MailboxError,
|
||||
util = require('util');
|
||||
|
||||
function create(req, res, next) {
|
||||
assert.strictEqual(typeof req.body, 'object');
|
||||
@@ -31,7 +34,7 @@ function get(req, res, next) {
|
||||
assert.strictEqual(typeof req.params.mailboxId, 'string');
|
||||
|
||||
mailboxes.get(req.params.mailboxId, function (error, result) {
|
||||
if (error && error.reason === MailboxError.NOT_FOUND) return next(new HttpError(404, 'No such group'));
|
||||
if (error && error.reason === MailboxError.NOT_FOUND) return next(new HttpError(404, 'No such mailbox'));
|
||||
if (error) return next(new HttpError(500, error));
|
||||
|
||||
next(new HttpSuccess(200, result));
|
||||
@@ -56,3 +59,33 @@ function remove(req, res, next) {
|
||||
next(new HttpSuccess(204));
|
||||
});
|
||||
}
|
||||
|
||||
function setAliases(req, res, next) {
|
||||
assert.strictEqual(typeof req.params.mailboxId, 'string');
|
||||
|
||||
if (!util.isArray(req.body.aliases)) return next(new HttpError(400, 'aliases must be an array'));
|
||||
|
||||
for (var i = 0; i < req.body.aliases.length; i++) {
|
||||
if (typeof req.body.aliases[i] !== 'string') return next(new HttpError(400, 'alias must be a string'));
|
||||
}
|
||||
|
||||
mailboxes.setAliases(req.params.mailboxId, req.body.aliases, function (error) {
|
||||
if (error && error.reason === MailboxError.NOT_FOUND) return next(new HttpError(404, 'No such mailbox'));
|
||||
if (error && error.reason === MailboxError.BAD_NAME) return next(new HttpError(400, error.reason));
|
||||
if (error && error.reason === MailboxError.ALREADY_EXISTS) return next(new HttpError(409, 'One or more alias already exist'));
|
||||
if (error) return next(new HttpError(500, error));
|
||||
|
||||
next(new HttpSuccess(200));
|
||||
});
|
||||
}
|
||||
|
||||
function getAliases(req, res, next) {
|
||||
assert.strictEqual(typeof req.params.mailboxId, 'string');
|
||||
|
||||
mailboxes.getAliases(req.params.mailboxId, function (error, aliases) {
|
||||
if (error && error.reason === MailboxError.NOT_FOUND) return next(new HttpError(404, 'No such mailbox'));
|
||||
if (error) return next(new HttpError(500, error));
|
||||
|
||||
next(new HttpSuccess(200, { aliases: aliases }));
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user