make 1 call for parentCids

This commit is contained in:
Barış Soner Uşaklı
2018-11-22 18:21:43 -05:00
parent dc670a7bb5
commit d5af39ca5d
2 changed files with 16 additions and 15 deletions

View File

@@ -235,19 +235,21 @@ Categories.getParentsAndChildren = function (categoryData, uid, callback) {
};
Categories.getChildren = function (cids, uid, callback) {
var categories = cids.map(cid => ({ cid: cid }));
async.each(categories, function (category, next) {
Categories.getCategoryField(category.cid, 'parentCid', function (err, parentCid) {
if (err) {
return next(err);
}
category.parentCid = parentCid;
getChildrenRecursive(category, uid, next);
});
}, function (err) {
callback(err, categories.map(c => c && c.children));
});
var categories;
async.waterfall([
function (next) {
Categories.getCategoriesFields(cids, ['parentCid'], next);
},
function (categoryData, next) {
categories = categoryData.map((category, index) => ({ cid: cids[index], parentCid: category.parentCid }));
async.each(categories, function (category, next) {
getChildrenRecursive(category, uid, next);
}, next);
},
function (next) {
next(null, categories.map(c => c && c.children));
},
], callback);
};
function getChildrenRecursive(category, uid, callback) {