fix: parent cid

This commit is contained in:
Barış Soner Uşaklı
2026-03-01 14:11:48 -05:00
parent 12122637cc
commit 04477f07db
2 changed files with 10 additions and 9 deletions

View File

@@ -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) {

View File

@@ -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);
});