2018-01-20 18:30:14 -08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
exports = module.exports = {
|
2018-04-03 14:37:52 -07:00
|
|
|
getDomain: getDomain,
|
|
|
|
|
addDomain: addDomain,
|
|
|
|
|
getDomainStats: getDomainStats,
|
|
|
|
|
updateDomain: updateDomain,
|
|
|
|
|
removeDomain: removeDomain,
|
2018-01-24 21:15:58 -08:00
|
|
|
|
2018-01-20 18:56:17 -08:00
|
|
|
getStatus: getStatus,
|
|
|
|
|
|
|
|
|
|
setMailFromValidation: setMailFromValidation,
|
|
|
|
|
setCatchAllAddress: setCatchAllAddress,
|
|
|
|
|
setMailRelay: setMailRelay,
|
2018-01-23 16:10:23 -08:00
|
|
|
setMailEnabled: setMailEnabled,
|
|
|
|
|
|
2018-01-24 13:11:35 +01:00
|
|
|
sendTestMail: sendTestMail,
|
|
|
|
|
|
|
|
|
|
getMailboxes: getMailboxes,
|
2018-04-03 12:18:26 -07:00
|
|
|
getMailbox: getMailbox,
|
|
|
|
|
addMailbox: addMailbox,
|
2018-04-03 14:12:43 -07:00
|
|
|
updateMailbox: updateMailbox,
|
2018-04-03 12:18:26 -07:00
|
|
|
removeMailbox: removeMailbox,
|
2018-01-25 18:03:02 +01:00
|
|
|
|
2018-04-03 12:18:26 -07:00
|
|
|
listAliases: listAliases,
|
2018-01-25 18:03:02 +01:00
|
|
|
getAliases: getAliases,
|
2018-04-03 12:18:26 -07:00
|
|
|
setAliases: setAliases,
|
2018-01-26 10:22:50 +01:00
|
|
|
|
|
|
|
|
getLists: getLists,
|
|
|
|
|
getList: getList,
|
|
|
|
|
addList: addList,
|
2018-04-03 14:12:43 -07:00
|
|
|
updateList: updateList,
|
2018-01-26 10:22:50 +01:00
|
|
|
removeList: removeList
|
2018-01-20 18:30:14 -08:00
|
|
|
};
|
|
|
|
|
|
2018-01-20 18:56:17 -08:00
|
|
|
var assert = require('assert'),
|
|
|
|
|
mail = require('../mail.js'),
|
|
|
|
|
MailError = mail.MailError,
|
2018-01-20 18:30:14 -08:00
|
|
|
HttpError = require('connect-lastmile').HttpError,
|
2018-03-22 15:01:28 -07:00
|
|
|
HttpSuccess = require('connect-lastmile').HttpSuccess,
|
|
|
|
|
middleware = require('../middleware/index.js'),
|
|
|
|
|
url = require('url');
|
|
|
|
|
|
|
|
|
|
var mailProxy = middleware.proxy(url.parse('http://127.0.0.1:2020'));
|
2018-01-20 18:30:14 -08:00
|
|
|
|
2018-04-03 14:37:52 -07:00
|
|
|
function getDomain(req, res, next) {
|
2018-01-20 22:56:45 -08:00
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
|
|
|
|
|
2018-04-03 14:37:52 -07:00
|
|
|
mail.getDomain(req.params.domain, function (error, result) {
|
2018-01-20 22:56:45 -08:00
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
2018-01-20 18:30:14 -08:00
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
2018-01-20 22:56:45 -08:00
|
|
|
next(new HttpSuccess(200, result));
|
2018-01-20 18:30:14 -08:00
|
|
|
});
|
|
|
|
|
}
|
2018-01-20 18:56:17 -08:00
|
|
|
|
2018-04-03 14:37:52 -07:00
|
|
|
function addDomain(req, res, next) {
|
2018-01-25 10:46:15 -08:00
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
|
|
|
|
if (typeof req.body.domain !== 'string') return next(new HttpError(400, 'domain must be a string'));
|
2018-01-24 21:15:58 -08:00
|
|
|
|
2018-04-03 14:37:52 -07:00
|
|
|
mail.addDomain(req.body.domain, function (error) {
|
2018-01-24 21:15:58 -08:00
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
2018-01-29 17:17:47 +01:00
|
|
|
if (error && error.reason === MailError.ALREADY_EXISTS) return next(new HttpError(409, 'domain already exists'));
|
2018-01-24 21:15:58 -08:00
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
2018-01-25 10:46:15 -08:00
|
|
|
next(new HttpSuccess(201, { domain: req.body.domain }));
|
2018-01-24 21:15:58 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-03 14:37:52 -07:00
|
|
|
function getDomainStats(req, res, next) {
|
2018-03-22 15:01:28 -07:00
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
|
|
|
|
|
|
|
|
|
var parsedUrl = url.parse(req.url, true /* parseQueryString */);
|
|
|
|
|
delete parsedUrl.query['access_token'];
|
|
|
|
|
delete req.headers['authorization'];
|
|
|
|
|
delete req.headers['cookies'];
|
|
|
|
|
|
|
|
|
|
req.url = url.format({ pathname: req.params.domain, query: parsedUrl.query });
|
|
|
|
|
|
|
|
|
|
mailProxy(req, res, next);
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-03 14:37:52 -07:00
|
|
|
function updateDomain(req, res, next) {
|
2018-03-08 17:26:07 -08:00
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
|
|
|
|
|
2018-04-03 14:37:52 -07:00
|
|
|
mail.updateDomain(req.params.domain, function (error) {
|
2018-03-08 17:26:07 -08:00
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(202));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-03 14:37:52 -07:00
|
|
|
function removeDomain(req, res, next) {
|
2018-01-24 21:15:58 -08:00
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
|
|
|
|
|
2018-04-03 14:37:52 -07:00
|
|
|
mail.removeDomain(req.params.domain, function (error) {
|
2018-01-24 21:15:58 -08:00
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
2018-02-10 22:49:35 -08:00
|
|
|
if (error && error.reason === MailError.IN_USE) return next(new HttpError(409, 'Mail domain is still in use. Remove existing mailboxes'));
|
2018-01-24 21:15:58 -08:00
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
2018-01-25 10:46:15 -08:00
|
|
|
next(new HttpSuccess(204));
|
2018-01-24 21:15:58 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-20 22:56:45 -08:00
|
|
|
function getStatus(req, res, next) {
|
2018-01-20 23:17:39 -08:00
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
|
|
|
|
|
2018-03-08 09:32:06 -08:00
|
|
|
// can take a while to query all the DNS entries
|
|
|
|
|
req.clearTimeout();
|
|
|
|
|
|
2018-01-20 23:17:39 -08:00
|
|
|
mail.getStatus(req.params.domain, function (error, records) {
|
|
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
2018-01-20 18:56:17 -08:00
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
2018-01-20 22:56:45 -08:00
|
|
|
next(new HttpSuccess(200, records));
|
2018-01-20 18:56:17 -08:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setMailFromValidation(req, res, next) {
|
2018-01-20 23:17:39 -08:00
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
2018-01-20 18:56:17 -08:00
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
|
|
|
|
if (typeof req.body.enabled !== 'boolean') return next(new HttpError(400, 'enabled is required'));
|
|
|
|
|
|
2018-01-20 23:17:39 -08:00
|
|
|
mail.setMailFromValidation(req.params.domain, req.body.enabled, function (error) {
|
|
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
2018-01-20 18:56:17 -08:00
|
|
|
if (error && error.reason === MailError.BAD_FIELD) return next(new HttpError(400, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(202));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setCatchAllAddress(req, res, next) {
|
2018-01-20 23:17:39 -08:00
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
2018-01-20 18:56:17 -08:00
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
2018-04-12 12:31:54 +02:00
|
|
|
if (!req.body.address) return next(new HttpError(400, 'address is required'));
|
|
|
|
|
if (!Array.isArray(req.body.address)) return next(new HttpError(400, 'address must be an array of strings'));
|
2018-01-20 18:56:17 -08:00
|
|
|
|
|
|
|
|
for (var i = 0; i < req.body.address.length; i++) {
|
2018-04-12 12:31:54 +02:00
|
|
|
if (typeof req.body.address[i] !== 'string') return next(new HttpError(400, 'address must be an array of strings'));
|
2018-01-20 18:56:17 -08:00
|
|
|
}
|
|
|
|
|
|
2018-01-20 23:17:39 -08:00
|
|
|
mail.setCatchAllAddress(req.params.domain, req.body.address, function (error) {
|
|
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
2018-01-20 18:56:17 -08:00
|
|
|
if (error && error.reason === MailError.BAD_FIELD) return next(new HttpError(400, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(202));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setMailRelay(req, res, next) {
|
2018-01-20 23:17:39 -08:00
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
2018-01-20 18:56:17 -08:00
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
|
|
|
|
if (typeof req.body.provider !== 'string') return next(new HttpError(400, 'provider is required'));
|
|
|
|
|
if ('host' in req.body && typeof req.body.host !== 'string') return next(new HttpError(400, 'host must be a string'));
|
|
|
|
|
if ('port' in req.body && typeof req.body.port !== 'number') return next(new HttpError(400, 'port must be a string'));
|
|
|
|
|
if ('username' in req.body && typeof req.body.username !== 'string') return next(new HttpError(400, 'username must be a string'));
|
|
|
|
|
if ('password' in req.body && typeof req.body.password !== 'string') return next(new HttpError(400, 'password must be a string'));
|
|
|
|
|
|
2018-01-20 23:17:39 -08:00
|
|
|
mail.setMailRelay(req.params.domain, req.body, function (error) {
|
|
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
2018-01-20 18:56:17 -08:00
|
|
|
if (error && error.reason === MailError.BAD_FIELD) return next(new HttpError(400, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(202));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-20 23:17:39 -08:00
|
|
|
function setMailEnabled(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
2018-01-20 18:56:17 -08:00
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
|
|
|
|
if (typeof req.body.enabled !== 'boolean') return next(new HttpError(400, 'enabled is required'));
|
|
|
|
|
|
2018-01-21 00:40:30 -08:00
|
|
|
mail.setMailEnabled(req.params.domain, !!req.body.enabled, function (error) {
|
2018-01-20 23:17:39 -08:00
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
2018-01-20 18:56:17 -08:00
|
|
|
if (error && error.reason === MailError.BAD_FIELD) return next(new HttpError(400, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(202));
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-01-23 16:10:23 -08:00
|
|
|
|
|
|
|
|
function sendTestMail(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
|
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
|
|
|
|
if (!req.body.to || typeof req.body.to !== 'string') return next(new HttpError(400, 'to must be a non-empty string'));
|
|
|
|
|
|
|
|
|
|
mail.sendTestMail(req.params.domain, req.body.to, function (error) {
|
|
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(202));
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-01-24 13:11:35 +01:00
|
|
|
|
|
|
|
|
function getMailboxes(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
|
|
|
|
|
|
|
|
|
mail.getMailboxes(req.params.domain, function (error, result) {
|
|
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(200, { mailboxes: result }));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-03 12:18:26 -07:00
|
|
|
function getMailbox(req, res, next) {
|
2018-01-24 13:11:35 +01:00
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
2018-04-03 12:18:26 -07:00
|
|
|
assert.strictEqual(typeof req.params.name, 'string');
|
2018-01-24 13:11:35 +01:00
|
|
|
|
2018-04-03 12:18:26 -07:00
|
|
|
mail.getMailbox(req.params.name, req.params.domain, function (error, result) {
|
2018-01-24 13:11:35 +01:00
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(200, { mailbox: result }));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-03 12:18:26 -07:00
|
|
|
function addMailbox(req, res, next) {
|
2018-01-24 13:11:35 +01:00
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
|
|
|
|
|
2018-04-03 12:18:26 -07:00
|
|
|
if (typeof req.body.name !== 'string') return next(new HttpError(400, 'name must be a string'));
|
|
|
|
|
if (typeof req.body.userId !== 'string') return next(new HttpError(400, 'userId must be a string'));
|
|
|
|
|
|
|
|
|
|
mail.addMailbox(req.body.name, req.params.domain, req.body.userId, function (error) {
|
2018-01-24 13:11:35 +01:00
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
2018-04-06 16:51:37 +02:00
|
|
|
if (error && error.reason === MailError.ALREADY_EXISTS) return next(new HttpError(409, error.message));
|
2018-04-05 16:07:51 -07:00
|
|
|
if (error && error.reason === MailError.BAD_FIELD) return next(new HttpError(400, error.message));
|
2018-01-24 13:11:35 +01:00
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(201, {}));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-03 14:12:43 -07:00
|
|
|
function updateMailbox(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
|
|
|
|
assert.strictEqual(typeof req.params.name, 'string');
|
|
|
|
|
|
|
|
|
|
if (typeof req.body.userId !== 'string') return next(new HttpError(400, 'userId must be a string'));
|
|
|
|
|
|
|
|
|
|
mail.updateMailbox(req.params.name, req.params.domain, req.body.userId, function (error) {
|
|
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
2018-04-05 16:07:51 -07:00
|
|
|
if (error && error.reason === MailError.BAD_FIELD) return next(new HttpError(400, error.message));
|
2018-04-03 14:12:43 -07:00
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(204));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-03 12:18:26 -07:00
|
|
|
function removeMailbox(req, res, next) {
|
2018-01-24 13:11:35 +01:00
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
2018-04-03 12:18:26 -07:00
|
|
|
assert.strictEqual(typeof req.params.name, 'string');
|
2018-01-24 13:11:35 +01:00
|
|
|
|
2018-04-03 12:18:26 -07:00
|
|
|
mail.removeMailbox(req.params.name, req.params.domain, function (error) {
|
|
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
2018-01-24 13:11:35 +01:00
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(201, {}));
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-01-25 18:03:02 +01:00
|
|
|
|
2018-04-03 12:18:26 -07:00
|
|
|
function listAliases(req, res, next) {
|
2018-01-25 18:03:02 +01:00
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
2018-04-01 18:23:54 +02:00
|
|
|
|
2018-04-03 12:18:26 -07:00
|
|
|
mail.listAliases(req.params.domain, function (error, result) {
|
|
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
2018-04-01 18:23:54 +02:00
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(200, { aliases: result }));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-03 12:18:26 -07:00
|
|
|
function getAliases(req, res, next) {
|
2018-04-01 18:23:54 +02:00
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
2018-04-03 12:18:26 -07:00
|
|
|
assert.strictEqual(typeof req.params.name, 'string');
|
2018-01-25 18:03:02 +01:00
|
|
|
|
2018-04-03 12:18:26 -07:00
|
|
|
mail.getAliases(req.params.name, req.params.domain, function (error, result) {
|
2018-01-25 18:03:02 +01:00
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(200, { aliases: result }));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-03 12:18:26 -07:00
|
|
|
function setAliases(req, res, next) {
|
2018-01-25 18:03:02 +01:00
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
2018-04-03 12:18:26 -07:00
|
|
|
assert.strictEqual(typeof req.params.name, 'string');
|
2018-01-25 18:03:02 +01:00
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
|
|
|
|
if (!Array.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'));
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-03 12:18:26 -07:00
|
|
|
mail.setAliases(req.params.name, req.params.domain, req.body.aliases, function (error) {
|
2018-01-25 18:03:02 +01:00
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
2018-04-01 19:29:47 +02:00
|
|
|
if (error && error.reason === MailError.ALREADY_EXISTS) return next(new HttpError(409, error.message));
|
2018-03-25 21:08:15 -07:00
|
|
|
if (error && error.reason === MailError.BAD_FIELD) return next(new HttpError(400, error.message));
|
2018-01-25 18:03:02 +01:00
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(202));
|
|
|
|
|
});
|
|
|
|
|
}
|
2018-01-26 10:22:50 +01:00
|
|
|
|
|
|
|
|
function getLists(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
|
|
|
|
|
|
|
|
|
mail.getLists(req.params.domain, function (error, result) {
|
|
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(200, { lists: result }));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getList(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
2018-04-03 09:34:33 -07:00
|
|
|
assert.strictEqual(typeof req.params.name, 'string');
|
2018-01-26 10:22:50 +01:00
|
|
|
|
2018-04-03 09:34:33 -07:00
|
|
|
mail.getList(req.params.domain, req.params.name, function (error, result) {
|
2018-01-26 10:22:50 +01:00
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(200, { list: result }));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function addList(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
|
|
|
|
assert.strictEqual(typeof req.body, 'object');
|
|
|
|
|
|
2018-04-03 09:34:33 -07:00
|
|
|
if (typeof req.body.name !== 'string') return next(new HttpError(400, 'name must be a string'));
|
2018-04-05 15:46:13 -07:00
|
|
|
if (!Array.isArray(req.body.members)) return next(new HttpError(400, 'members must be a string'));
|
2018-01-26 10:22:50 +01:00
|
|
|
|
2018-04-05 15:46:13 -07:00
|
|
|
for (var i = 0; i < req.body.members.length; i++) {
|
|
|
|
|
if (typeof req.body.members[i] !== 'string') return next(new HttpError(400, 'member must be a string'));
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-05 16:07:51 -07:00
|
|
|
mail.addList(req.body.name, req.params.domain, req.body.members, function (error) {
|
2018-01-26 10:22:50 +01:00
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
2018-01-30 12:14:08 +01:00
|
|
|
if (error && error.reason === MailError.ALREADY_EXISTS) return next(new HttpError(409, 'list already exists'));
|
2018-04-05 16:07:51 -07:00
|
|
|
if (error && error.reason === MailError.BAD_FIELD) return next(new HttpError(400, error.message));
|
2018-01-26 10:22:50 +01:00
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(201, {}));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-04-03 14:12:43 -07:00
|
|
|
function updateList(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
|
|
|
|
assert.strictEqual(typeof req.params.name, 'string');
|
|
|
|
|
|
2018-04-05 15:46:13 -07:00
|
|
|
if (!Array.isArray(req.body.members)) return next(new HttpError(400, 'members must be a string'));
|
|
|
|
|
|
|
|
|
|
for (var i = 0; i < req.body.members.length; i++) {
|
|
|
|
|
if (typeof req.body.members[i] !== 'string') return next(new HttpError(400, 'member must be a string'));
|
|
|
|
|
}
|
2018-04-03 14:12:43 -07:00
|
|
|
|
2018-04-05 15:46:13 -07:00
|
|
|
mail.updateList(req.params.name, req.params.domain, req.body.members, function (error) {
|
2018-04-03 14:12:43 -07:00
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
2018-04-05 16:07:51 -07:00
|
|
|
if (error && error.reason === MailError.BAD_FIELD) return next(new HttpError(400, error.message));
|
2018-04-03 14:12:43 -07:00
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
|
|
|
|
next(new HttpSuccess(204));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-26 10:22:50 +01:00
|
|
|
function removeList(req, res, next) {
|
|
|
|
|
assert.strictEqual(typeof req.params.domain, 'string');
|
2018-04-03 09:34:33 -07:00
|
|
|
assert.strictEqual(typeof req.params.name, 'string');
|
2018-01-26 10:22:50 +01:00
|
|
|
|
2018-04-03 09:34:33 -07:00
|
|
|
mail.removeList(req.params.domain, req.params.name, function (error) {
|
2018-01-26 10:22:50 +01:00
|
|
|
if (error && error.reason === MailError.NOT_FOUND) return next(new HttpError(404, error.message));
|
|
|
|
|
if (error) return next(new HttpError(500, error));
|
|
|
|
|
|
2018-01-30 12:22:55 +01:00
|
|
|
next(new HttpSuccess(204));
|
2018-01-26 10:22:50 +01:00
|
|
|
});
|
|
|
|
|
}
|