From a0cd4b49f5cb7e75de0ac65a2da66fac7e0da302 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Fri, 28 Aug 2015 13:04:02 -0400 Subject: [PATCH] when a category is deleted, set the children's parent to 0 --- src/categories/delete.js | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/categories/delete.js b/src/categories/delete.js index 8483efaf72..7bfe03a005 100644 --- a/src/categories/delete.js +++ b/src/categories/delete.js @@ -49,13 +49,30 @@ module.exports = function(Categories) { function removeFromParent(cid, callback) { async.waterfall([ function(next) { - Categories.getCategoryField(cid, 'parentCid', next); + async.parallel({ + parentCid: function(next) { + Categories.getCategoryField(cid, 'parentCid', next); + }, + children: function(next) { + db.getSortedSetRange('cid:' + cid + ':children', 0, -1, next); + } + }, next); }, - function(parentCid, next) { - parentCid = parseInt(parentCid, 10) || 0; - db.sortedSetRemove('cid:' + parentCid + ':children', cid, next); + function(results, next) { + async.parallel([ + function(next) { + results.parentCid = parseInt(results.parentCid, 10) || 0; + db.sortedSetRemove('cid:' + results.parentCid + ':children', cid, next); + }, + function(next) { + async.each(results.children, function(cid, next) { + db.setObjectField('category:' + cid, 'parentCid', 0, next); + }, next); + } + ], next); } - ], callback); - + ], function(err, results) { + callback(err); + }); } }; \ No newline at end of file