From 87f6ac59b111bb0d1d01ca82bf8009d6d7ee731a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 20 Sep 2019 19:04:47 -0400 Subject: [PATCH] fix: returned data --- src/categories/index.js | 8 +++++++- src/controllers/helpers.js | 16 +++++----------- src/controllers/search.js | 4 ++-- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/categories/index.js b/src/categories/index.js index 37eae5c874..388f5c5cdd 100644 --- a/src/categories/index.js +++ b/src/categories/index.js @@ -350,7 +350,13 @@ Categories.buildForSelectCategories = function (categories) { categories = categories.filter(category => category && !category.parentCid); categories.forEach(category => recursive(category, categoriesData, '', 0)); - return categoriesData; + const pickFields = [ + 'name', 'level', 'disabledClass', 'icon', 'value', 'text', + 'cid', 'parentCid', 'color', 'bgColor', 'backgroundImage', 'imageClass', + 'disabled', 'depth', + ]; + + return categoriesData.map(category => _.pick(category, pickFields)); }; Categories.async = require('../promisify')(Categories); diff --git a/src/controllers/helpers.js b/src/controllers/helpers.js index ae2f638cf0..c0f0fd56ae 100644 --- a/src/controllers/helpers.js +++ b/src/controllers/helpers.js @@ -4,7 +4,6 @@ const nconf = require('nconf'); const validator = require('validator'); const winston = require('winston'); const querystring = require('querystring'); -const _ = require('lodash'); const user = require('../user'); const privileges = require('../privileges'); @@ -12,7 +11,6 @@ const categories = require('../categories'); const plugins = require('../plugins'); const meta = require('../meta'); const middleware = require('../middleware'); -const utils = require('../utils'); const helpers = module.exports; @@ -233,11 +231,13 @@ async function getCategoryData(cids, uid, selectedCid) { let categoryData = await categories.getCategoriesFields(cids, categoryFields); categoryData = categoryData.filter(category => category && !category.link); + categories.getTree(categoryData); + const categoriesData = categories.buildForSelectCategories(categoryData); + let selectedCategory = []; const selectedCids = []; - categoryData.forEach(function (category) { + categoriesData.forEach(function (category) { category.selected = selectedCid ? selectedCid.includes(String(category.cid)) : false; - category.parentCid = category.hasOwnProperty('parentCid') && utils.isNumber(category.parentCid) ? category.parentCid : 0; if (category.selected) { selectedCategory.push(category); selectedCids.push(category.cid); @@ -257,14 +257,8 @@ async function getCategoryData(cids, uid, selectedCid) { selectedCategory = undefined; } - - categories.getTree(categoryData); - const categoriesData = categories.buildForSelectCategories(categoryData); - return { - categories: categoriesData.map(category => _.pick(category, [ - 'cid', 'name', 'icon', 'color', 'bgColor', 'parentCid', 'backgroundImage', 'imageClass', 'selected', 'level', - ])), + categories: categoriesData, selectedCategory: selectedCategory, selectedCids: selectedCids, }; diff --git a/src/controllers/search.js b/src/controllers/search.js index 1b46e121a8..4649bd4aef 100644 --- a/src/controllers/search.js +++ b/src/controllers/search.js @@ -87,11 +87,11 @@ async function buildCategories(uid, searchOnly) { const cids = await categories.getCidsByPrivilege('categories:cid', uid, 'read'); let categoriesData = await categories.getCategoriesData(cids); + categoriesData = categoriesData.filter(category => category && !category.link); categoriesData = categories.getTree(categoriesData); categoriesData = categories.buildForSelectCategories(categoriesData); + categoriesData = categoriesData.map(category => ({ value: category.value, text: category.text })); - categoriesData = categoriesData.filter(category => category && !category.link) - .map(category => ({ value: category.value, text: category.text })); return [ { value: 'all', text: '[[unread:all_categories]]' }, { value: 'watched', text: '[[category:watched-categories]]' },