mailboxdb: merge into mail.js

This commit is contained in:
Girish Ramakrishnan
2021-08-17 15:45:57 -07:00
parent 98ef6dfae9
commit fa9938f50a
12 changed files with 727 additions and 1233 deletions
+59 -70
View File
@@ -17,7 +17,7 @@ exports = module.exports = {
getMailbox,
addMailbox,
updateMailbox,
removeMailbox,
delMailbox,
getAliases,
setAliases,
@@ -26,7 +26,7 @@ exports = module.exports = {
getList,
addList,
updateList,
removeList,
delList,
getMailboxCount
};
@@ -133,46 +133,44 @@ function sendTestMail(req, res, next) {
});
}
function listMailboxes(req, res, next) {
async function listMailboxes(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
var page = typeof req.query.page !== 'undefined' ? parseInt(req.query.page) : 1;
const page = typeof req.query.page !== 'undefined' ? parseInt(req.query.page) : 1;
if (!page || page < 0) return next(new HttpError(400, 'page query param has to be a positive number'));
var perPage = typeof req.query.per_page !== 'undefined'? parseInt(req.query.per_page) : 25;
const perPage = typeof req.query.per_page !== 'undefined'? parseInt(req.query.per_page) : 25;
if (!perPage || perPage < 0) return next(new HttpError(400, 'per_page query param has to be a positive number'));
if (req.query.search && typeof req.query.search !== 'string') return next(new HttpError(400, 'search must be a string'));
mail.listMailboxes(req.params.domain, req.query.search || null, page, perPage, function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, mailboxes] = await safe(mail.listMailboxes(req.params.domain, req.query.search || null, page, perPage));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { mailboxes: result }));
});
next(new HttpSuccess(200, { mailboxes }));
}
function getMailboxCount(req, res, next) {
async function getMailboxCount(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
mail.getMailboxCount(req.params.domain, function (error, count) {
if (error) return next(BoxError.toHttpError(error));
const [error, count] = await safe(mail.getMailboxCount(req.params.domain));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { count }));
});
next(new HttpSuccess(200, { count }));
}
function getMailbox(req, res, next) {
async function getMailbox(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.params.name, 'string');
mail.getMailbox(req.params.name, req.params.domain, function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, result] = await safe(mail.getMailbox(req.params.name, req.params.domain));
if (error) return next(BoxError.toHttpError(error));
if (!result) return next(new HttpError(404, 'Mailbox not found'));
next(new HttpSuccess(200, { mailbox: result }));
});
next(new HttpSuccess(200, { mailbox: result }));
}
function addMailbox(req, res, next) {
async function addMailbox(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
if (typeof req.body.name !== 'string') return next(new HttpError(400, 'name must be a string'));
@@ -180,14 +178,13 @@ function addMailbox(req, res, next) {
if (typeof req.body.ownerType !== 'string') return next(new HttpError(400, 'ownerType must be a string'));
if (typeof req.body.active !== 'boolean') return next(new HttpError(400, 'active must be a boolean'));
mail.addMailbox(req.body.name, req.params.domain, req.body, auditSource.fromRequest(req), function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(mail.addMailbox(req.body.name, req.params.domain, req.body, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(201, {}));
});
next(new HttpSuccess(201, {}));
}
function updateMailbox(req, res, next) {
async function updateMailbox(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.params.name, 'string');
@@ -195,38 +192,35 @@ function updateMailbox(req, res, next) {
if (typeof req.body.ownerType !== 'string') return next(new HttpError(400, 'ownerType must be a string'));
if (typeof req.body.active !== 'boolean') return next(new HttpError(400, 'active must be a boolean'));
mail.updateMailbox(req.params.name, req.params.domain, req.body, auditSource.fromRequest(req), function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(mail.updateMailbox(req.params.name, req.params.domain, req.body, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204));
});
next(new HttpSuccess(204));
}
function removeMailbox(req, res, next) {
async function delMailbox(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.params.name, 'string');
if (typeof req.body.deleteMails !== 'boolean') return next(new HttpError(400, 'deleteMails must be a boolean'));
mail.removeMailbox(req.params.name, req.params.domain, req.body, auditSource.fromRequest(req), function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(mail.delMailbox(req.params.name, req.params.domain, req.body, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(201, {}));
});
next(new HttpSuccess(201, {}));
}
function getAliases(req, res, next) {
async function getAliases(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.params.name, 'string');
mail.getAliases(req.params.name, req.params.domain, function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, aliases] = await safe(mail.getAliases(req.params.name, req.params.domain));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { aliases: result }));
});
next(new HttpSuccess(200, { aliases }));
}
function setAliases(req, res, next) {
async function setAliases(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.params.name, 'string');
assert.strictEqual(typeof req.body, 'object');
@@ -239,11 +233,10 @@ function setAliases(req, res, next) {
if (typeof alias.domain !== 'string') return next(new HttpError(400, 'domain must be a string'));
}
mail.setAliases(req.params.name, req.params.domain, req.body.aliases, function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(mail.setAliases(req.params.name, req.params.domain, req.body.aliases));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202));
});
next(new HttpSuccess(202));
}
async function setBanner(req, res, next) {
@@ -259,7 +252,7 @@ async function setBanner(req, res, next) {
next(new HttpSuccess(202));
}
function getLists(req, res, next) {
async function getLists(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
const page = typeof req.query.page !== 'undefined' ? parseInt(req.query.page) : 1;
@@ -270,25 +263,24 @@ function getLists(req, res, next) {
if (req.query.search && typeof req.query.search !== 'string') return next(new HttpError(400, 'search must be a string'));
mail.getLists(req.params.domain, req.query.search || null, page, perPage, function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, lists] = await safe(mail.getLists(req.params.domain, req.query.search || null, page, perPage));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { lists: result }));
});
next(new HttpSuccess(200, { lists }));
}
function getList(req, res, next) {
async function getList(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.params.name, 'string');
mail.getList(req.params.name, req.params.domain, function (error, result) {
if (error) return next(BoxError.toHttpError(error));
const [error, result] = await safe(mail.getList(req.params.name, req.params.domain));
if (error) return next(BoxError.toHttpError(error));
if (!result) return next(new HttpError(404, 'List not found'));
next(new HttpSuccess(200, { list: result }));
});
next(new HttpSuccess(200, { list: result }));
}
function addList(req, res, next) {
async function addList(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.body, 'object');
@@ -296,20 +288,19 @@ function addList(req, res, next) {
if (!Array.isArray(req.body.members)) return next(new HttpError(400, 'members must be a string'));
if (req.body.members.length === 0) return next(new HttpError(400, 'list must have atleast one member'));
for (var i = 0; i < req.body.members.length; i++) {
for (let 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'));
}
if (typeof req.body.membersOnly !== 'boolean') return next(new HttpError(400, 'membersOnly must be a boolean'));
if (typeof req.body.active !== 'boolean') return next(new HttpError(400, 'active must be a boolean'));
mail.addList(req.body.name, req.params.domain, req.body, auditSource.fromRequest(req), function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(mail.addList(req.body.name, req.params.domain, req.body, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(201, {}));
});
next(new HttpSuccess(201, {}));
}
function updateList(req, res, next) {
async function updateList(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.params.name, 'string');
@@ -322,20 +313,18 @@ function updateList(req, res, next) {
if (typeof req.body.membersOnly !== 'boolean') return next(new HttpError(400, 'membersOnly must be a boolean'));
if (typeof req.body.active !== 'boolean') return next(new HttpError(400, 'active must be a boolean'));
mail.updateList(req.params.name, req.params.domain, req.body, auditSource.fromRequest(req), function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(mail.updateList(req.params.name, req.params.domain, req.body, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204));
});
next(new HttpSuccess(204));
}
function removeList(req, res, next) {
async function delList(req, res, next) {
assert.strictEqual(typeof req.params.domain, 'string');
assert.strictEqual(typeof req.params.name, 'string');
mail.removeList(req.params.name, req.params.domain, auditSource.fromRequest(req), function (error) {
if (error) return next(BoxError.toHttpError(error));
const [error] = await safe(mail.delList(req.params.name, req.params.domain, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(204));
});
next(new HttpSuccess(204));
}
+11 -12
View File
@@ -4,8 +4,8 @@ exports = module.exports = {
proxy,
restart,
getLocation,
setLocation
setLocation,
getLocation
};
const assert = require('assert'),
@@ -16,6 +16,7 @@ const assert = require('assert'),
HttpSuccess = require('connect-lastmile').HttpSuccess,
mail = require('../mail.js'),
middleware = require('../middleware/index.js'),
safe = require('safetydance'),
services = require('../services.js'),
url = require('url');
@@ -55,23 +56,21 @@ function proxy(req, res, next) {
});
}
function getLocation(req, res, next) {
mail.getLocation(function (error, result) {
if (error) return next(BoxError.toHttpError(error));
async function getLocation(req, res, next) {
const [error, result] = await safe(mail.getLocation());
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(200, { domain: result.domain, subdomain: result.subdomain }));
});
next(new HttpSuccess(200, { domain: result.domain, subdomain: result.subdomain }));
}
function setLocation(req, res, next) {
async function setLocation(req, res, next) {
assert.strictEqual(typeof req.body, 'object');
if (typeof req.body.domain !== 'string') return next(new HttpError(400, 'domain must be a string'));
if (typeof req.body.subdomain !== 'string') return next(new HttpError(400, 'subdomain must be a string'));
mail.setLocation(req.body.subdomain, req.body.domain, auditSource.fromRequest(req), function (error, taskId) {
if (error) return next(BoxError.toHttpError(error));
const [error, taskId] = await safe(mail.setLocation(req.body.subdomain, req.body.domain, auditSource.fromRequest(req)));
if (error) return next(BoxError.toHttpError(error));
next(new HttpSuccess(202, { taskId }));
});
next(new HttpSuccess(202, { taskId }));
}
+4 -11
View File
@@ -496,11 +496,8 @@ describe('Mail API', function () {
describe('aliases', function () {
const MAILBOX_NAME = 'support';
after(function (done) {
mail._removeMailboxes(dashboardDomain, function (error) {
if (error) return done(error);
done();
});
after(async function () {
await mail._delByDomain(dashboardDomain);
});
it('add the mailbox', async function () {
@@ -565,12 +562,8 @@ describe('Mail API', function () {
describe('mailinglists', function () {
const LIST_NAME = 'people';
after(function (done) {
mail._removeMailboxes(dashboardDomain, function (error) {
if (error) return done(error);
done();
});
after(async function () {
await mail._delByDomain(dashboardDomain);
});
it('add fails without groupId', async function () {