Commit 8535d9c2 authored by Patricio Bruna's avatar Patricio Bruna

Added countAccounts

parent e269a128
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
- [Creating Resources](#creating-resources) - [Creating Resources](#creating-resources)
- [Modify Resources](#modify-resources) - [Modify Resources](#modify-resources)
- [Remove Resources](#remove-resources) - [Remove Resources](#remove-resources)
- [Domains](#domains)
## Example ## Example
...@@ -256,3 +257,25 @@ zimbraApi.removeAccount(zimbraId, callback); ...@@ -256,3 +257,25 @@ zimbraApi.removeAccount(zimbraId, callback);
* If everything goes OK you receive **nothing** as result. * If everything goes OK you receive **nothing** as result.
* You **can't delete** a `Domain` that is not empty. If it has any `Account` or `DistributionList`, You have to delete those first. * You **can't delete** a `Domain` that is not empty. If it has any `Account` or `DistributionList`, You have to delete those first.
## Domains
This are functions especifics to `Domains`.
### Count Accounts
Count number of accounts by `CoS` in a domain.
```javascript
zimbraApi.countAccounts('example.com', callback);
// Object {unknow: 2, default: 33, basic: 28, premium: 31, professional: 31}
```
If you have a `Domain` you can call `countAccounts(callback)` on it:
```javascript
// domain is a Domain, you got it from zimbraApi.getDomain....
domain.countAccounts(callback);
// Object {unknow: 2, default: 33, basic: 28, premium: 31, professional: 31}
```
...@@ -9,10 +9,19 @@ ...@@ -9,10 +9,19 @@
## Domains ## Domains
* [ ] Devolver casillas segun el tipo de plan, cantidad asignadas y cantidad utilizadas para un dominio * [X] Devolver casillas segun el tipo de plan, cantidad asignadas y cantidad utilizadas para un dominio
* [ ] Devolver MX Record * [ ] Devolver MX Record
* [ ] Devolver Tipo de renovación * [ ] Devolver Tipo de renovación
* [ ] Devolver Fecha de la proxima renovación * [ ] Devolver Fecha de la proxima renovación
* [ ] Devolver Empresa (esto entiendo necesitamos otro API) * [ ] Devolver Empresa (esto entiendo necesitamos otro API)
* [ ] Devolver Admins del dominio * [ ] Devolver Admins del dominio
* [ ] Devolver Listas de distribución del dominio * [ ] Devolver Listas de distribución del dominio
## Order Refactor
* [ ] Tiene que estar más claro el código en `index.js` porque hay que subir o bajar mucho para
saber que hacer que hace alguna función.
## parseResponse Class?
* [ ] Refactor `index.js` because there are to many parse shit.
...@@ -115,6 +115,9 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -115,6 +115,9 @@ return /******/ (function(modules) { // webpackBootstrap
return Error; return Error;
}(); }();
// TODO: To many parseResponse types
var ZimbraAdminApi = function () { var ZimbraAdminApi = function () {
function ZimbraAdminApi(auth_object) { function ZimbraAdminApi(auth_object) {
(0, _classCallCheck3.default)(this, ZimbraAdminApi); (0, _classCallCheck3.default)(this, ZimbraAdminApi);
...@@ -126,6 +129,7 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -126,6 +129,7 @@ return /******/ (function(modules) { // webpackBootstrap
this.parseAllResponse = this.parseAllResponse.bind(this); this.parseAllResponse = this.parseAllResponse.bind(this);
this.parseResponse = this.parseResponse.bind(this); this.parseResponse = this.parseResponse.bind(this);
this.parseRemoveResponse = this.parseRemoveResponse.bind(this); this.parseRemoveResponse = this.parseRemoveResponse.bind(this);
this.parseCountAccountResponse = this.parseCountAccountResponse.bind(this);
this.parseSearchResponse = this.parseSearchResponse.bind(this); this.parseSearchResponse = this.parseSearchResponse.bind(this);
this.dictionary = new _dictionary2.default(); this.dictionary = new _dictionary2.default();
} }
...@@ -208,6 +212,18 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -208,6 +212,18 @@ return /******/ (function(modules) { // webpackBootstrap
})(); })();
} }
} }
}, {
key: 'parseCountAccountResponse',
value: function parseCountAccountResponse(data, request_data, callback) {
var result = {};
var coses = data.get().CountAccountResponse.cos;
coses.forEach(function (cos) {
result[cos.name] = parseInt(cos._content);
});
// const counts = Object.values(result);
// result.total = counts.reduce((a,b) => {return(a+b);});
return callback(null, result);
}
}, { }, {
key: 'parseResponse', key: 'parseResponse',
value: function parseResponse(data, request_data, callback) { value: function parseResponse(data, request_data, callback) {
...@@ -215,7 +231,7 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -215,7 +231,7 @@ return /******/ (function(modules) { // webpackBootstrap
var that = this; var that = this;
var response_name = that.dictionary.resourceResponseName(resource); var response_name = that.dictionary.resourceResponseName(resource);
var response_object = data.get()[request_data.response_name][response_name][0]; var response_object = data.get()[request_data.response_name][response_name][0];
var result = that.dictionary.classFactory(resource, response_object); var result = that.dictionary.classFactory(resource, response_object, that);
return callback(null, result); return callback(null, result);
} }
}, { }, {
...@@ -235,7 +251,7 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -235,7 +251,7 @@ return /******/ (function(modules) { // webpackBootstrap
var resources = []; var resources = [];
if (typeof response_object[type] !== 'undefined') { if (typeof response_object[type] !== 'undefined') {
response_object[type].forEach(function (resource) { response_object[type].forEach(function (resource) {
var object = that.dictionary.classFactory(type, resource); var object = that.dictionary.classFactory(type, resource, that);
resources.push(object); resources.push(object);
}); });
result[type] = resources; result[type] = resources;
...@@ -252,7 +268,7 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -252,7 +268,7 @@ return /******/ (function(modules) { // webpackBootstrap
var response_object = data.get()[request_data.response_name][response_name]; var response_object = data.get()[request_data.response_name][response_name];
var response_array = []; var response_array = [];
response_object.forEach(function (r) { response_object.forEach(function (r) {
var element = that.dictionary.classFactory(resource, r); var element = that.dictionary.classFactory(resource, r, that);
response_array.push(element); response_array.push(element);
}); });
return callback(null, response_array); return callback(null, response_array);
...@@ -522,6 +538,27 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -522,6 +538,27 @@ return /******/ (function(modules) { // webpackBootstrap
request_data.parse_response = this.parseSearchResponse; request_data.parse_response = this.parseSearchResponse;
this.performRequest(request_data); this.performRequest(request_data);
} }
// TODO: Fucking ugly code to make it better
// Count Accounts
// Count number of accounts by cos in a domain,
}, {
key: 'countAccounts',
value: function countAccounts(domain_idenfitier, callback) {
var request_data = {};
request_data.params = this.requestParams();
request_data.request_name = 'CountAccount';
request_data.response_name = 'CountAccountResponse';
request_data.params.name = request_data.request_name + 'Request';
request_data.callback = callback;
request_data.parse_response = this.parseCountAccountResponse;
request_data.params.params.domain = {
'by': this.dictionary.byIdOrName(domain_idenfitier),
'_content': domain_idenfitier
};
this.performRequest(request_data);
}
}, { }, {
key: 'token', key: 'token',
set: function set(token) { set: function set(token) {
...@@ -12173,9 +12210,9 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -12173,9 +12210,9 @@ return /******/ (function(modules) { // webpackBootstrap
} }
}, { }, {
key: 'classFactory', key: 'classFactory',
value: function classFactory(resource, object) { value: function classFactory(resource, object, client) {
var class_name = this.resourceToClass(resource.toLowerCase()); var class_name = this.resourceToClass(resource.toLowerCase());
return new class_name(object); return new class_name(object, client);
} }
}, { }, {
key: 'resourceResponseName', key: 'resourceResponseName',
...@@ -12894,6 +12931,10 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -12894,6 +12931,10 @@ return /******/ (function(modules) { // webpackBootstrap
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2); var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = __webpack_require__(69);
var _createClass3 = _interopRequireDefault(_createClass2);
var _possibleConstructorReturn2 = __webpack_require__(101); var _possibleConstructorReturn2 = __webpack_require__(101);
var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2); var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
...@@ -12911,11 +12952,20 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -12911,11 +12952,20 @@ return /******/ (function(modules) { // webpackBootstrap
var Domain = function (_Zimbra) { var Domain = function (_Zimbra) {
(0, _inherits3.default)(Domain, _Zimbra); (0, _inherits3.default)(Domain, _Zimbra);
function Domain(domain_obj) { function Domain(domain_obj, zimbra_api_client) {
(0, _classCallCheck3.default)(this, Domain); (0, _classCallCheck3.default)(this, Domain);
return (0, _possibleConstructorReturn3.default)(this, (0, _getPrototypeOf2.default)(Domain).call(this, domain_obj)); return (0, _possibleConstructorReturn3.default)(this, (0, _getPrototypeOf2.default)(Domain).call(this, domain_obj, zimbra_api_client));
} }
(0, _createClass3.default)(Domain, [{
key: 'countAccounts',
value: function countAccounts(callback) {
this.api.countAccounts(this.id, function (e, d) {
if (e) return callback(e);
return callback(null, d);
});
}
}]);
return Domain; return Domain;
}(_zimbra2.default); // Copyright (c) 2016 ZBox, Spa. All Rights Reserved. }(_zimbra2.default); // Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See LICENSE.txt for license information. // See LICENSE.txt for license information.
...@@ -13188,12 +13238,13 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -13188,12 +13238,13 @@ return /******/ (function(modules) { // webpackBootstrap
// See LICENSE.txt for license information. // See LICENSE.txt for license information.
var Zimbra = function () { var Zimbra = function () {
function Zimbra(resource_obj) { function Zimbra(resource_obj, zimbra_api_client) {
(0, _classCallCheck3.default)(this, Zimbra); (0, _classCallCheck3.default)(this, Zimbra);
this.name = resource_obj.name; this.name = resource_obj.name;
this.id = resource_obj.id; this.id = resource_obj.id;
this.attrs = this.buildAttrsMap(resource_obj.a); this.attrs = this.buildAttrsMap(resource_obj.a);
this.api = zimbra_api_client;
// this.obj = resource_obj; // this.obj = resource_obj;
} }
...@@ -13247,9 +13298,9 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -13247,9 +13298,9 @@ return /******/ (function(modules) { // webpackBootstrap
var Account = function (_Zimbra) { var Account = function (_Zimbra) {
(0, _inherits3.default)(Account, _Zimbra); (0, _inherits3.default)(Account, _Zimbra);
function Account(account_obj) { function Account(account_obj, zimbra_api_client) {
(0, _classCallCheck3.default)(this, Account); (0, _classCallCheck3.default)(this, Account);
return (0, _possibleConstructorReturn3.default)(this, (0, _getPrototypeOf2.default)(Account).call(this, account_obj)); return (0, _possibleConstructorReturn3.default)(this, (0, _getPrototypeOf2.default)(Account).call(this, account_obj, zimbra_api_client));
} }
return Account; return Account;
...@@ -13297,10 +13348,10 @@ return /******/ (function(modules) { // webpackBootstrap ...@@ -13297,10 +13348,10 @@ return /******/ (function(modules) { // webpackBootstrap
var DistributionList = function (_Zimbra) { var DistributionList = function (_Zimbra) {
(0, _inherits3.default)(DistributionList, _Zimbra); (0, _inherits3.default)(DistributionList, _Zimbra);
function DistributionList(dl_obj) { function DistributionList(dl_obj, zimbra_api_client) {
(0, _classCallCheck3.default)(this, DistributionList); (0, _classCallCheck3.default)(this, DistributionList);
var _this = (0, _possibleConstructorReturn3.default)(this, (0, _getPrototypeOf2.default)(DistributionList).call(this, dl_obj)); var _this = (0, _possibleConstructorReturn3.default)(this, (0, _getPrototypeOf2.default)(DistributionList).call(this, dl_obj, zimbra_api_client));
_this.members = _this.parseMembers(dl_obj); _this.members = _this.parseMembers(dl_obj);
return _this; return _this;
This diff is collapsed.
...@@ -24,6 +24,8 @@ class Error { ...@@ -24,6 +24,8 @@ class Error {
} }
// TODO: To many parseResponse types
export default class ZimbraAdminApi { export default class ZimbraAdminApi {
constructor(auth_object) { constructor(auth_object) {
this.url = auth_object.url; this.url = auth_object.url;
...@@ -33,6 +35,7 @@ export default class ZimbraAdminApi { ...@@ -33,6 +35,7 @@ export default class ZimbraAdminApi {
this.parseAllResponse = this.parseAllResponse.bind(this); this.parseAllResponse = this.parseAllResponse.bind(this);
this.parseResponse = this.parseResponse.bind(this); this.parseResponse = this.parseResponse.bind(this);
this.parseRemoveResponse = this.parseRemoveResponse.bind(this); this.parseRemoveResponse = this.parseRemoveResponse.bind(this);
this.parseCountAccountResponse = this.parseCountAccountResponse.bind(this);
this.parseSearchResponse = this.parseSearchResponse.bind(this); this.parseSearchResponse = this.parseSearchResponse.bind(this);
this.dictionary = new Dictionary(); this.dictionary = new Dictionary();
} }
...@@ -117,12 +120,24 @@ export default class ZimbraAdminApi { ...@@ -117,12 +120,24 @@ export default class ZimbraAdminApi {
} }
} }
parseCountAccountResponse(data, request_data, callback) {
const result = {};
const coses = data.get().CountAccountResponse.cos;
coses.forEach((cos) => {
result[cos.name] = parseInt(cos._content);
});
// const counts = Object.values(result);
// result.total = counts.reduce((a,b) => {return(a+b);});
return callback(null, result);
}
parseResponse(data, request_data, callback) { parseResponse(data, request_data, callback) {
const resource = request_data.resource.toLowerCase(); const resource = request_data.resource.toLowerCase();
const that = this; const that = this;
const response_name = that.dictionary.resourceResponseName(resource); const response_name = that.dictionary.resourceResponseName(resource);
const response_object = data.get()[request_data.response_name][response_name][0]; const response_object = data.get()[request_data.response_name][response_name][0];
const result = that.dictionary.classFactory(resource, response_object); const result = that.dictionary.classFactory(resource, response_object, that);
return callback(null, result); return callback(null, result);
} }
...@@ -140,7 +155,7 @@ export default class ZimbraAdminApi { ...@@ -140,7 +155,7 @@ export default class ZimbraAdminApi {
const resources = []; const resources = [];
if (typeof response_object[type] !== 'undefined') { if (typeof response_object[type] !== 'undefined') {
response_object[type].forEach((resource) => { response_object[type].forEach((resource) => {
const object = that.dictionary.classFactory(type, resource); const object = that.dictionary.classFactory(type, resource, that);
resources.push(object); resources.push(object);
}); });
result[type] = resources; result[type] = resources;
...@@ -156,7 +171,7 @@ export default class ZimbraAdminApi { ...@@ -156,7 +171,7 @@ export default class ZimbraAdminApi {
const response_object = data.get()[request_data.response_name][response_name]; const response_object = data.get()[request_data.response_name][response_name];
const response_array = []; const response_array = [];
response_object.forEach((r) => { response_object.forEach((r) => {
let element = that.dictionary.classFactory(resource, r); let element = that.dictionary.classFactory(resource, r, that);
response_array.push(element); response_array.push(element);
}); });
return callback(null, response_array); return callback(null, response_array);
...@@ -380,6 +395,24 @@ export default class ZimbraAdminApi { ...@@ -380,6 +395,24 @@ export default class ZimbraAdminApi {
this.performRequest(request_data); this.performRequest(request_data);
} }
// TODO: Fucking ugly code to make it better
// Count Accounts
// Count number of accounts by cos in a domain,
countAccounts(domain_idenfitier, callback) {
const request_data = { };
request_data.params = this.requestParams();
request_data.request_name = 'CountAccount';
request_data.response_name = 'CountAccountResponse';
request_data.params.name = `${request_data.request_name}Request`;
request_data.callback = callback;
request_data.parse_response = this.parseCountAccountResponse;
request_data.params.params.domain = {
'by': this.dictionary.byIdOrName(domain_idenfitier),
'_content': domain_idenfitier
};
this.performRequest(request_data);
}
} }
if (typeof module === 'object' && typeof module.exports === 'object') { if (typeof module === 'object' && typeof module.exports === 'object') {
......
...@@ -35,9 +35,9 @@ export default class Dictionary { ...@@ -35,9 +35,9 @@ export default class Dictionary {
return result; return result;
} }
classFactory (resource, object) { classFactory (resource, object, client) {
const class_name = this.resourceToClass(resource.toLowerCase()); const class_name = this.resourceToClass(resource.toLowerCase());
return new class_name(object); return new class_name(object, client);
} }
resourceResponseName (resource) { resourceResponseName (resource) {
......
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
import Zimbra from './zimbra.js'; import Zimbra from './zimbra.js';
export default class Account extends Zimbra { export default class Account extends Zimbra {
constructor(account_obj) { constructor(account_obj, zimbra_api_client) {
super(account_obj); super(account_obj, zimbra_api_client);
} }
} }
...@@ -4,8 +4,8 @@ ...@@ -4,8 +4,8 @@
import Zimbra from './zimbra.js'; import Zimbra from './zimbra.js';
export default class DistributionList extends Zimbra { export default class DistributionList extends Zimbra {
constructor(dl_obj) { constructor(dl_obj, zimbra_api_client) {
super(dl_obj); super(dl_obj, zimbra_api_client);
this.members = this.parseMembers(dl_obj); this.members = this.parseMembers(dl_obj);
} }
......
...@@ -4,7 +4,15 @@ ...@@ -4,7 +4,15 @@
import Zimbra from './zimbra.js'; import Zimbra from './zimbra.js';
export default class Domain extends Zimbra { export default class Domain extends Zimbra {
constructor(domain_obj) { constructor(domain_obj, zimbra_api_client) {
super(domain_obj); super(domain_obj, zimbra_api_client);
} }
countAccounts(callback) {
this.api.countAccounts(this.id, function(e,d){
if(e) return callback(e);
return callback(null, d);
});
}
} }
...@@ -2,10 +2,11 @@ ...@@ -2,10 +2,11 @@
// See LICENSE.txt for license information. // See LICENSE.txt for license information.
export default class Zimbra { export default class Zimbra {
constructor(resource_obj) { constructor(resource_obj, zimbra_api_client) {
this.name = resource_obj.name; this.name = resource_obj.name;
this.id = resource_obj.id; this.id = resource_obj.id;
this.attrs = this.buildAttrsMap(resource_obj.a); this.attrs = this.buildAttrsMap(resource_obj.a);
this.api = zimbra_api_client;
// this.obj = resource_obj; // this.obj = resource_obj;
} }
......
...@@ -323,5 +323,27 @@ ...@@ -323,5 +323,27 @@
}); });
}); });
it('should counts of account for the Domain', function(done){
let api = new ZimbraAdminApi(auth_data);
api.countAccounts('zboxapp.dev', function(err, data){
if (err) console.error(err);
expect(data.default).to.be.above(1);
done();
});
});
it('domain.countAccounts() should return the counts', function(done){
let api = new ZimbraAdminApi(auth_data);
api.getDomain('zboxapp.dev', function(err, data){
if (err) console.error(err);
let domain = data;
domain.countAccounts(function(e, d){
expect(d.default).to.be.above(1);
done();
});
});
});
}); });
})(); })();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment