1
0
Fork 0
mirror of https://github.com/pawelmalak/flame.git synced 2025-08-03 18:05:18 +02:00

actually filter categories by type when querying

This commit is contained in:
François Darveau 2021-09-06 12:09:41 -04:00
parent 64a9dabdd6
commit 55b32f9ce9

View file

@ -1,27 +1,31 @@
const asyncWrapper = require('../middleware/asyncWrapper'); const asyncWrapper = require('../middleware/asyncWrapper');
const ErrorResponse = require('../utils/ErrorResponse'); const ErrorResponse = require('../utils/ErrorResponse');
const Category = require('../models/Category'); const Category = require('../models/Category');
const App = require('../models/App');
const Bookmark = require('../models/Bookmark'); const Bookmark = require('../models/Bookmark');
const Config = require('../models/Config'); const Config = require('../models/Config');
const { Sequelize } = require('sequelize'); const { Sequelize } = require('sequelize');
exports.dockerDefaultCategory = { exports.dockerDefaultCategory = {
id: -2, id: -2,
name: "Docker", name: 'Docker',
type: "apps", type: 'apps',
isPinned: true, isPinned: true,
orderId: 998, orderId: 998,
}; };
exports.kubernetesDefaultCategory = { exports.kubernetesDefaultCategory = {
id: -3, id: -3,
name: "Kubernetes", name: 'Kubernetes',
type: "apps", type: 'apps',
isPinned: true, isPinned: true,
orderId: 999, orderId: 999,
}; };
const defaultCategories = [exports.dockerDefaultCategory, exports.kubernetesDefaultCategory]; const defaultCategories = [
exports.dockerDefaultCategory,
exports.kubernetesDefaultCategory,
];
// @desc Create new category // @desc Create new category
// @route POST /api/categories // @route POST /api/categories
@ -60,28 +64,39 @@ exports.getCategories = asyncWrapper(async (req, res, next) => {
where: { key: 'useOrdering' }, where: { key: 'useOrdering' },
}); });
const orderType = useOrdering ? useOrdering.value : "createdAt"; const orderType = useOrdering ? useOrdering.value : 'createdAt';
let categories; let categories;
let categoryTypes = []
const query = {
include: [],
order: [],
};
const requestedTypes = [];
if (!req.params.type || req.params.type === 'apps') { if (!req.params.type || req.params.type === 'apps') {
categoryTypes.push({model: App, as: "apps"}); query.include.push({ model: App, as: 'apps' });
requestedTypes.push('apps');
} }
if (!req.params.type || req.params.type === 'bookmarks') { if (!req.params.type || req.params.type === 'bookmarks') {
categoryTypes.push({model: Bookmark, as: "bookmarks"}); query.include.push({ model: Bookmark, as: 'bookmarks' });
requestedTypes.push('bookmarks');
}
query.where = { type: requestedTypes };
if (orderType == 'name') {
query.order.push([
Sequelize.fn('lower', Sequelize.col('Category.name')),
'ASC',
]);
} else {
query.order.push([orderType, 'ASC']);
} }
if (orderType == "name") { categories = await Category.findAll(query);
categories = await Category.findAll({ categories.push(
include: categoryTypes, defaultCategories.filter(
order: [[Sequelize.fn('lower', Sequelize.col('Category.name')), 'ASC']], (category) => requestedTypes.indexOf(category.type) > -1
}); )
} else { );
categories = await Category.findAll({
include: categoryTypes,
order: [[orderType, 'ASC']],
});
}
categories.push(defaultCategories.filter((category) => categoryTypes.findIndex((includedType) => category.type === includedType.as) > -1));
res.status(200).json({ res.status(200).json({
success: true, success: true,