diff --git a/src/mail.js b/src/mail.js index bc23b680b..cf0c74f69 100644 --- a/src/mail.js +++ b/src/mail.js @@ -1,52 +1,53 @@ 'use strict'; exports = module.exports = { - getStatus: getStatus, - checkConfiguration: checkConfiguration, + getStatus, + checkConfiguration, - getDomains: getDomains, + getDomains, - getDomain: getDomain, - clearDomains: clearDomains, + getDomain, + clearDomains, - onDomainAdded: onDomainAdded, - onDomainRemoved: onDomainRemoved, + onDomainAdded, + onDomainRemoved, + onMailFqdnChanged, - removePrivateFields: removePrivateFields, + removePrivateFields, - setDnsRecords: setDnsRecords, - onMailFqdnChanged: onMailFqdnChanged, + setDnsRecords, - validateName: validateName, + validateName, - setMailFromValidation: setMailFromValidation, - setCatchAllAddress: setCatchAllAddress, - setMailRelay: setMailRelay, - setMailEnabled: setMailEnabled, + setMailFromValidation, + setCatchAllAddress, + setMailRelay, + setMailEnabled, startMail: restartMail, - restartMail: restartMail, - handleCertChanged: handleCertChanged, - getMailAuth: getMailAuth, + restartMail, + handleCertChanged, + getMailAuth, - sendTestMail: sendTestMail, + sendTestMail, - listMailboxes: listMailboxes, - removeMailboxes: removeMailboxes, - getMailbox: getMailbox, - addMailbox: addMailbox, - updateMailboxOwner: updateMailboxOwner, - removeMailbox: removeMailbox, + getMailboxCount, + listMailboxes, + removeMailboxes, + getMailbox, + addMailbox, + updateMailboxOwner, + removeMailbox, - getAliases: getAliases, - setAliases: setAliases, + getAliases, + setAliases, - getLists: getLists, - getList: getList, - addList: addList, - updateList: updateList, - removeList: removeList, - resolveList: resolveList, + getLists, + getList, + addList, + updateList, + removeList, + resolveList, _readDkimPublicKeySync: readDkimPublicKeySync }; @@ -1046,6 +1047,17 @@ function listMailboxes(domain, search, page, perPage, callback) { }); } +function getMailboxCount(domain, callback) { + assert.strictEqual(typeof domain, 'string'); + assert.strictEqual(typeof callback, 'function'); + + mailboxdb.getMailboxCount(domain, function (error, result) { + if (error) return callback(error); + + callback(null, result); + }); +} + function removeMailboxes(domain, callback) { assert.strictEqual(typeof domain, 'string'); assert.strictEqual(typeof callback, 'function'); diff --git a/src/mailboxdb.js b/src/mailboxdb.js index c9903f982..12f9914bf 100644 --- a/src/mailboxdb.js +++ b/src/mailboxdb.js @@ -1,31 +1,32 @@ 'use strict'; exports = module.exports = { - addMailbox: addMailbox, - addList: addList, + addMailbox, + addList, - updateMailboxOwner: updateMailboxOwner, - updateList: updateList, - del: del, + updateMailboxOwner, + updateList, + del, - listMailboxes: listMailboxes, - getLists: getLists, + getMailboxCount, + listMailboxes, + getLists, - listAllMailboxes: listAllMailboxes, + listAllMailboxes, - get: get, - getMailbox: getMailbox, - getList: getList, - getAlias: getAlias, + get, + getMailbox, + getList, + getAlias, - getAliasesForName: getAliasesForName, - setAliasesForName: setAliasesForName, + getAliasesForName, + setAliasesForName, - getByOwnerId: getByOwnerId, - delByOwnerId: delByOwnerId, - delByDomain: delByDomain, + getByOwnerId, + delByOwnerId, + delByDomain, - updateName: updateName, + updateName, _clear: clear, @@ -204,6 +205,17 @@ function getMailbox(name, domain, callback) { }); } +function getMailboxCount(domain, callback) { + assert.strictEqual(typeof domain, 'string'); + assert.strictEqual(typeof callback, 'function'); + + database.query('SELECT COUNT(*) AS total FROM mailboxes WHERE type = ? AND domain = ?', [ exports.TYPE_MAILBOX, domain ], function (error, results) { + if (error) return callback(new BoxError(BoxError.DATABASE_ERROR, error)); + + callback(null, results[0].total); + }); +} + function listMailboxes(domain, search, page, perPage, callback) { assert.strictEqual(typeof domain, 'string'); assert(typeof search === 'string' || search === null); diff --git a/src/routes/mail.js b/src/routes/mail.js index c3c67c983..a84eb30ef 100644 --- a/src/routes/mail.js +++ b/src/routes/mail.js @@ -1,33 +1,35 @@ 'use strict'; exports = module.exports = { - getDomain: getDomain, + getDomain, - setDnsRecords: setDnsRecords, + setDnsRecords, - getStatus: getStatus, + getStatus, - setMailFromValidation: setMailFromValidation, - setCatchAllAddress: setCatchAllAddress, - setMailRelay: setMailRelay, - setMailEnabled: setMailEnabled, + setMailFromValidation, + setCatchAllAddress, + setMailRelay, + setMailEnabled, - sendTestMail: sendTestMail, + sendTestMail, - listMailboxes: listMailboxes, - getMailbox: getMailbox, - addMailbox: addMailbox, - updateMailbox: updateMailbox, - removeMailbox: removeMailbox, + listMailboxes, + getMailbox, + addMailbox, + updateMailbox, + removeMailbox, - getAliases: getAliases, - setAliases: setAliases, + getAliases, + setAliases, - getLists: getLists, - getList: getList, - addList: addList, - updateList: updateList, - removeList: removeList, + getLists, + getList, + addList, + updateList, + removeList, + + getMailboxCount }; var assert = require('assert'), @@ -168,6 +170,16 @@ function listMailboxes(req, res, next) { }); } +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)); + + next(new HttpSuccess(200, { count })); + }); +} + function getMailbox(req, res, next) { assert.strictEqual(typeof req.params.domain, 'string'); assert.strictEqual(typeof req.params.name, 'string'); diff --git a/src/server.js b/src/server.js index 7103fb691..4e99e6a6b 100644 --- a/src/server.js +++ b/src/server.js @@ -261,6 +261,7 @@ function initializeExpressSync() { router.post('/api/v1/mail/:domain/enable', json, token, authorizeAdmin, routes.mail.setMailEnabled); router.post('/api/v1/mail/:domain/dns', json, token, authorizeAdmin, routes.mail.setDnsRecords); router.post('/api/v1/mail/:domain/send_test_mail', json, token, authorizeAdmin, routes.mail.sendTestMail); + router.get ('/api/v1/mail/:domain/mailbox_count', token, authorizeAdmin, routes.mail.getMailboxCount); router.get ('/api/v1/mail/:domain/mailboxes', token, authorizeAdmin, routes.mail.listMailboxes); router.get ('/api/v1/mail/:domain/mailboxes/:name', token, authorizeAdmin, routes.mail.getMailbox); router.post('/api/v1/mail/:domain/mailboxes', json, token, authorizeAdmin, routes.mail.addMailbox);