Commit d2d21704 authored by Patricio Bruna's avatar Patricio Bruna

Replaced all getAllxx Functions with directorySearch()

parent d85d3949
This diff is collapsed.
This diff is collapsed.
{ {
"name": "zimbra-admin-api-js", "name": "zimbra-admin-api-js",
"version": "0.0.6", "version": "0.0.7",
"private": true, "private": true,
"main": "lib/zimbra-admin-api.js", "main": "lib/zimbra-admin-api.js",
"dependencies": { "dependencies": {
......
...@@ -32,6 +32,7 @@ export default class ZimbraAdminApi { ...@@ -32,6 +32,7 @@ export default class ZimbraAdminApi {
this._client = new jszimbra.Communication({url: auth_object.url}); this._client = new jszimbra.Communication({url: auth_object.url});
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.parseSearchResponse = this.parseSearchResponse.bind(this);
this.dictionary = new Dictionary(); this.dictionary = new Dictionary();
} }
...@@ -115,25 +116,38 @@ export default class ZimbraAdminApi { ...@@ -115,25 +116,38 @@ export default class ZimbraAdminApi {
} }
} }
parseResponse(data, obj, callback) { parseResponse(data, request_data, callback) {
const resource = obj.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()[obj.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);
return callback(null, result); return callback(null, result);
} }
parseRawResponse(data, obj, callback) { parseSearchResponse(data, request_data, callback) {
const response_types = this.dictionary.searchResponseTypes();
const response_object = data.get()[request_data.response_name];
const result = { total: response_object.searchTotal, more: response_object.more };
const that = this;
response_types.forEach((type) => {
const resources = [];
if (typeof response_object[type] !== 'undefined') {
response_object[type].forEach((resource) => {
const object = that.dictionary.classFactory(type, resource);
resources.push(object);
});
result[type] = resources;
}
});
return callback(null, result);
} }
parseAllResponse(data, request_data, callback){
parseAllResponse(data, obj, callback){ const resource = request_data.resource.toLowerCase();
const resource = obj.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()[obj.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);
...@@ -216,16 +230,24 @@ export default class ZimbraAdminApi { ...@@ -216,16 +230,24 @@ export default class ZimbraAdminApi {
this.get('DistributionList', identifier, callback); this.get('DistributionList', identifier, callback);
} }
getAllDomains(callback) { getAllDomains(callback, query_object = {}) {
this.getAll('Domain', callback); query_object.types = 'domains';
this.directorySearch(query_object, callback);
}
getAllAccounts(callback, query_object = {}) {
query_object.types = 'accounts';
this.directorySearch(query_object, callback);
} }
getAllAccounts(callback) { getAllDistributionLists(callback, query_object = {}) {
this.getAll('Account', callback); query_object.types = 'distributionlists';
this.directorySearch(query_object, callback);
} }
getAllDistributionLists(callback) { getAllAliases(callback, query_object = {}) {
this.getAll('DistributionList', callback); query_object.types = 'aliases';
this.directorySearch(query_object, callback);
} }
// Get current logged account information // Get current logged account information
...@@ -242,23 +264,6 @@ export default class ZimbraAdminApi { ...@@ -242,23 +264,6 @@ export default class ZimbraAdminApi {
this.performRequest(request_data); this.performRequest(request_data);
} }
// const req_params =
// const that = this;
// this.client.getRequest({}, function(err, req) {
// if (err) return callback(this.handleError(err));
//
// req.addRequest(req_params, function(err){
// if (err) return callback(that.handleError(err));
// that.client.send(req, function(err, data){
// if (err) return callback(that.handleError(err));
// const result = data.response[0].GetInfoResponse
// return callback(null, result);
// });
// });
// });
// }
// TODO: Fix this fucking code // TODO: Fix this fucking code
// Search the Directory // Search the Directory
// search_object = { // search_object = {
...@@ -276,7 +281,15 @@ export default class ZimbraAdminApi { ...@@ -276,7 +281,15 @@ export default class ZimbraAdminApi {
// attrs: Comma separated list of attributes // attrs: Comma separated list of attributes
// } // }
directorySearch(search_object, callback) { directorySearch(search_object, callback) {
const request_data = { };
request_data.params = this.requestParams();
request_data.params.params = search_object;
request_data.request_name = "SearchDirectory";
request_data.response_name = "SearchDirectoryResponse";
request_data.params.name = `${request_data.request_name}Request`;
request_data.callback = callback;
request_data.parse_response = this.parseSearchResponse;
this.performRequest(request_data);
} }
} }
......
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
import Domain from './../zimbra/domain.js'; import Domain from './../zimbra/domain.js';
import Account from './../zimbra/account.js'; import Account from './../zimbra/account.js';
import Alias from './../zimbra/alias.js';
import DistributionList from './../zimbra/distribution_list.js'; import DistributionList from './../zimbra/distribution_list.js';
export default class Dictionary { export default class Dictionary {
...@@ -26,7 +27,7 @@ export default class Dictionary { ...@@ -26,7 +27,7 @@ export default class Dictionary {
// {size: 20, age: 30} => [ {n: size, _content: 20}, {n: age, _content: 30}] // {size: 20, age: 30} => [ {n: size, _content: 20}, {n: age, _content: 30}]
attributesToArray (attributes) { attributesToArray (attributes) {
if ($.isEmptyObject(attributes)) return []; if ($.isEmptyObject(attributes)) return [];
const result = [] const result = [];
const map = new Map(Object.entries(attributes)); const map = new Map(Object.entries(attributes));
map.forEach((key, value) => { map.forEach((key, value) => {
result.push({ 'n': value, '_content': key }); result.push({ 'n': value, '_content': key });
...@@ -43,6 +44,15 @@ export default class Dictionary { ...@@ -43,6 +44,15 @@ export default class Dictionary {
return this.zimbra_resources[resource.toLowerCase()].response_name; return this.zimbra_resources[resource.toLowerCase()].response_name;
} }
searchResponseTypes () {
const result = [];
const that = this;
Object.keys(this.zimbra_resources).forEach((k) => {
result.push(that.zimbra_resources[k].response_name);
});
return result;
}
ZimbraResources() { ZimbraResources() {
return { return {
domain: { domain: {
...@@ -56,7 +66,16 @@ export default class Dictionary { ...@@ -56,7 +66,16 @@ export default class Dictionary {
distributionlist: { distributionlist: {
class_name: DistributionList, class_name: DistributionList,
response_name: 'dl' response_name: 'dl'
} },
dl: {
class_name: DistributionList,
response_name: 'dl'
},
alias: {
response_name: 'alias',
class_name: Alias,
},
}; };
} }
......
// Copyright (c) 2016 ZBox, Spa. All Rights Reserved.
// See LICENSE.txt for license information.
export default class Alias {
constructor(resource_obj) {
this.name = resource_obj.name;
this.id = resource_obj.id;
this.targetName = resource_obj.targetName;
}
}
...@@ -6,7 +6,7 @@ export default class Zimbra { ...@@ -6,7 +6,7 @@ export default class 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.obj = resource_obj; // this.obj = resource_obj;
} }
buildAttrsMap(obj_ary) { buildAttrsMap(obj_ary) {
......
...@@ -74,7 +74,7 @@ ...@@ -74,7 +74,7 @@
let api = new ZimbraAdminApi(auth_data); let api = new ZimbraAdminApi(auth_data);
api.getAllDomains(function(err, data){ api.getAllDomains(function(err, data){
if (err) console.log(err); if (err) console.log(err);
expect(data[0].constructor.name).to.equal('Domain'); expect(data.domain[0].constructor.name).to.equal('Domain');
done(); done();
}); });
}); });
...@@ -85,7 +85,7 @@ ...@@ -85,7 +85,7 @@
// var proxy = api.getAllAccounts(callback); // var proxy = api.getAllAccounts(callback);
api.getAllAccounts(function(err, data){ api.getAllAccounts(function(err, data){
if (err) console.log(err); if (err) console.log(err);
expect(data[0].constructor.name).to.equal('Account'); expect(data.account[0].constructor.name).to.equal('Account');
done(); done();
}); });
}); });
...@@ -94,7 +94,7 @@ ...@@ -94,7 +94,7 @@
let api = new ZimbraAdminApi(auth_data); let api = new ZimbraAdminApi(auth_data);
api.getAllDistributionLists(function(err, data){ api.getAllDistributionLists(function(err, data){
if (err) console.log(err); if (err) console.log(err);
expect(data[0].constructor.name).to.equal('DistributionList'); expect(data.dl[0].constructor.name).to.equal('DistributionList');
done(); done();
}); });
}); });
...@@ -145,7 +145,7 @@ ...@@ -145,7 +145,7 @@
}); });
it('should create and return an account', function(done){ it('should create and return an account', function(done){
let account_name = Date.now() + '@zboxapp.dev'; let account_name = Date.now() + '@big.com';
let account_password = Date.now(); let account_password = Date.now();
let account_attributes = {}; let account_attributes = {};
let api = new ZimbraAdminApi(auth_data); let api = new ZimbraAdminApi(auth_data);
...@@ -195,7 +195,7 @@ ...@@ -195,7 +195,7 @@
it('should create and return an account with extra attributes', function(done){ it('should create and return an account with extra attributes', function(done){
let account_name = Date.now() + '@zboxapp.dev'; let account_name = Date.now() + '@big.com';
let account_password = Date.now(); let account_password = Date.now();
let account_attributes = { 'sn': 'Bruna', 'givenName': 'Patricio' }; let account_attributes = { 'sn': 'Bruna', 'givenName': 'Patricio' };
let api = new ZimbraAdminApi(auth_data); let api = new ZimbraAdminApi(auth_data);
...@@ -220,6 +220,17 @@ ...@@ -220,6 +220,17 @@
api.login(callback); api.login(callback);
}); });
it('should return directorySearch with total info', function(done){
let api = new ZimbraAdminApi(auth_data);
let query_object = {limit: 10, domain: 'customer.dev', types: "accounts,distributionlists,aliases"};
api.directorySearch(query_object, function(err, data){
expect(data.more).to.equal(true);
expect(data.total).to.be.above(1);
expect(data.account.length).to.be.at.least(2);
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