From 66e7cdac7a61726c80ed5a4311c70b39d570533c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Thu, 4 Nov 2021 11:45:06 -0400 Subject: [PATCH] fix: #9973, ignore if assigning to same parent --- src/categories/update.js | 3 +++ test/categories.js | 20 ++++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/categories/update.js b/src/categories/update.js index cdca138635..04650a4c46 100644 --- a/src/categories/update.js +++ b/src/categories/update.js @@ -71,6 +71,9 @@ module.exports = function (Categories) { } const categoryData = await Categories.getCategoryFields(cid, ['parentCid', 'order']); const oldParent = categoryData.parentCid; + if (oldParent === newParent) { + return; + } await Promise.all([ db.sortedSetRemove(`cid:${oldParent}:children`, cid), db.sortedSetAdd(`cid:${newParent}:children`, categoryData.order, cid), diff --git a/test/categories.js b/test/categories.js index d1e5cbf746..e91a51f619 100644 --- a/test/categories.js +++ b/test/categories.js @@ -432,6 +432,26 @@ describe('Categories', () => { }); }); + it('should not remove category from parent if parent is set again to same category', async () => { + const parentCat = await Categories.create({ name: 'parent', description: 'poor parent' }); + const updateData = {}; + updateData[cid] = { + parentCid: parentCat.cid, + }; + await Categories.update(updateData); + let data = await Categories.getCategoryData(cid); + assert.equal(data.parentCid, updateData[cid].parentCid); + let childrenCids = await db.getSortedSetRange(`cid:${parentCat.cid}:children`, 0, -1); + assert(childrenCids.includes(String(cid))); + + // update again to same parent + await Categories.update(updateData); + data = await Categories.getCategoryData(cid); + assert.equal(data.parentCid, updateData[cid].parentCid); + childrenCids = await db.getSortedSetRange(`cid:${parentCat.cid}:children`, 0, -1); + assert(childrenCids.includes(String(cid))); + }); + it('should purge category', (done) => { Categories.create({ name: 'purge me',