From 04477f07db66bc8c841604aed37dddb9f47b5402 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Sun, 1 Mar 2026 14:11:48 -0500 Subject: [PATCH] fix: parent cid --- src/categories/index.js | 14 +++++++------- test/search.js | 5 +++-- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/categories/index.js b/src/categories/index.js index 4d772f49f9..df66600794 100644 --- a/src/categories/index.js +++ b/src/categories/index.js @@ -330,7 +330,7 @@ Categories.flattenCategories = function (allCategories, categoryData) { * @param parentCid {number} start from 0 to build full tree */ Categories.getTree = function (categories, parentCid) { - parentCid = parentCid || 0; + parentCid = String(parentCid || 0); const cids = categories.map(category => category && category.cid); const cidToCategory = {}; const parents = {}; @@ -351,15 +351,15 @@ Categories.getTree = function (categories, parentCid) { return; } if (!category.hasOwnProperty('parentCid') || category.parentCid === null) { - category.parentCid = 0; + category.parentCid = '0'; } - if (category.parentCid === parentCid) { + if (String(category.parentCid) === parentCid) { tree.push(category); category.parent = parents[parentCid]; } else { - const parent = cidToCategory[category.parentCid]; + const parent = cidToCategory[String(category.parentCid)]; if (parent && parent.cid !== category.cid) { - category.parent = parents[category.parentCid]; + category.parent = parents[String(category.parentCid)]; parent.children = parent.children || []; parent.children.push(category); } @@ -413,10 +413,10 @@ Categories.buildForSelectCategories = function (categories, fields, parentCid) { category.children.forEach(child => recursive(child, categoriesData, `    ${level}`, depth + 1)); } } - parentCid = parentCid || 0; + parentCid = String(parentCid || 0); const categoriesData = []; - const rootCategories = categories.filter(category => category && category.parentCid === parentCid); + const rootCategories = categories.filter(category => category && String(category.parentCid) === parentCid); rootCategories.sort((a, b) => { if (a.order !== b.order) { diff --git a/test/search.js b/test/search.js index f0e285cb9d..e0b1421786 100644 --- a/test/search.js +++ b/test/search.js @@ -130,9 +130,10 @@ describe('Search', () => { it('should search for categories', async () => { const socketCategories = require('../src/socket.io/categories'); - let data = await socketCategories.categorySearch({ uid: phoebeUid }, { query: 'baz', parentCid: 0 }); + let data = await socketCategories.categorySearch({ uid: phoebeUid }, { search: 'baz', parentCid: 0 }); + assert.strictEqual(data.length, 1); assert.strictEqual(data[0].name, 'baz category'); - data = await socketCategories.categorySearch({ uid: phoebeUid }, { query: '', parentCid: 0 }); + data = await socketCategories.categorySearch({ uid: phoebeUid }, { search: '', parentCid: 0 }); assert.strictEqual(data.length, 5); });