2021-05-22 19:04:34 +02:00
|
|
|
const asyncWrapper = require('../middleware/asyncWrapper');
|
|
|
|
const ErrorResponse = require('../utils/ErrorResponse');
|
|
|
|
const Category = require('../models/Category');
|
2021-09-06 12:09:41 -04:00
|
|
|
const App = require('../models/App');
|
2021-05-22 19:04:34 +02:00
|
|
|
const Bookmark = require('../models/Bookmark');
|
2021-06-09 10:58:45 +02:00
|
|
|
const Config = require('../models/Config');
|
2021-10-04 12:11:41 +02:00
|
|
|
const { Sequelize } = require('sequelize');
|
2021-05-22 19:04:34 +02:00
|
|
|
|
2021-08-07 12:31:41 -04:00
|
|
|
exports.dockerDefaultCategory = {
|
|
|
|
id: -2,
|
2021-09-06 12:09:41 -04:00
|
|
|
name: 'Docker',
|
|
|
|
type: 'apps',
|
2021-08-07 12:31:41 -04:00
|
|
|
isPinned: true,
|
2021-08-26 10:46:38 -04:00
|
|
|
orderId: 998,
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.kubernetesDefaultCategory = {
|
|
|
|
id: -3,
|
2021-09-06 12:09:41 -04:00
|
|
|
name: 'Kubernetes',
|
|
|
|
type: 'apps',
|
2021-08-26 10:46:38 -04:00
|
|
|
isPinned: true,
|
2021-08-07 14:01:33 -04:00
|
|
|
orderId: 999,
|
2021-08-07 12:31:41 -04:00
|
|
|
};
|
|
|
|
|
2021-09-06 12:09:41 -04:00
|
|
|
const defaultCategories = [
|
|
|
|
exports.dockerDefaultCategory,
|
|
|
|
exports.kubernetesDefaultCategory,
|
|
|
|
];
|
2021-09-06 11:45:10 -04:00
|
|
|
|
2021-05-22 19:04:34 +02:00
|
|
|
// @desc Create new category
|
|
|
|
// @route POST /api/categories
|
|
|
|
// @access Public
|
|
|
|
exports.createCategory = asyncWrapper(async (req, res, next) => {
|
2021-06-09 10:58:45 +02:00
|
|
|
// Get config from database
|
|
|
|
const pinCategories = await Config.findOne({
|
2021-10-04 12:11:41 +02:00
|
|
|
where: { key: 'pinCategoriesByDefault' },
|
2021-06-09 10:58:45 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
let category;
|
|
|
|
|
|
|
|
if (pinCategories) {
|
|
|
|
if (parseInt(pinCategories.value)) {
|
|
|
|
category = await Category.create({
|
|
|
|
...req.body,
|
2021-10-04 12:11:41 +02:00
|
|
|
isPinned: true,
|
|
|
|
});
|
2021-06-09 10:58:45 +02:00
|
|
|
} else {
|
|
|
|
category = await Category.create(req.body);
|
|
|
|
}
|
|
|
|
}
|
2021-05-22 19:04:34 +02:00
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
success: true,
|
2021-10-04 12:11:41 +02:00
|
|
|
data: category,
|
|
|
|
});
|
|
|
|
});
|
2021-05-22 19:04:34 +02:00
|
|
|
|
|
|
|
// @desc Get all categories
|
|
|
|
// @route GET /api/categories
|
|
|
|
// @access Public
|
|
|
|
exports.getCategories = asyncWrapper(async (req, res, next) => {
|
2021-06-18 13:42:55 +02:00
|
|
|
// Get config from database
|
|
|
|
const useOrdering = await Config.findOne({
|
2021-10-04 12:11:41 +02:00
|
|
|
where: { key: 'useOrdering' },
|
2021-05-23 17:18:04 +02:00
|
|
|
});
|
2021-05-22 19:04:34 +02:00
|
|
|
|
2021-09-06 12:09:41 -04:00
|
|
|
const orderType = useOrdering ? useOrdering.value : 'createdAt';
|
2021-06-18 13:42:55 +02:00
|
|
|
let categories;
|
2021-09-06 12:09:41 -04:00
|
|
|
|
|
|
|
const query = {
|
|
|
|
include: [],
|
|
|
|
order: [],
|
|
|
|
};
|
|
|
|
const requestedTypes = [];
|
2021-09-06 11:45:10 -04:00
|
|
|
if (!req.params.type || req.params.type === 'apps') {
|
2021-09-06 12:09:41 -04:00
|
|
|
query.include.push({ model: App, as: 'apps' });
|
|
|
|
requestedTypes.push('apps');
|
2021-09-06 11:45:10 -04:00
|
|
|
}
|
|
|
|
if (!req.params.type || req.params.type === 'bookmarks') {
|
2021-09-06 12:09:41 -04:00
|
|
|
query.include.push({ model: Bookmark, as: 'bookmarks' });
|
|
|
|
requestedTypes.push('bookmarks');
|
2021-09-06 11:45:10 -04:00
|
|
|
}
|
2021-09-06 12:09:41 -04:00
|
|
|
query.where = { type: requestedTypes };
|
2021-06-18 13:42:55 +02:00
|
|
|
|
2021-09-06 12:09:41 -04:00
|
|
|
if (orderType == 'name') {
|
|
|
|
query.order.push([
|
|
|
|
Sequelize.fn('lower', Sequelize.col('Category.name')),
|
|
|
|
'ASC',
|
|
|
|
]);
|
2021-06-18 13:42:55 +02:00
|
|
|
} else {
|
2021-09-06 12:09:41 -04:00
|
|
|
query.order.push([orderType, 'ASC']);
|
2021-06-18 13:42:55 +02:00
|
|
|
}
|
2021-09-06 12:17:52 -04:00
|
|
|
|
2021-09-06 12:09:41 -04:00
|
|
|
categories = await Category.findAll(query);
|
2021-09-06 12:17:52 -04:00
|
|
|
defaultCategories
|
|
|
|
.filter((category) => requestedTypes.indexOf(category.type) > -1)
|
|
|
|
.forEach((category) => categories.push(category));
|
2021-06-18 13:42:55 +02:00
|
|
|
|
2021-05-22 19:04:34 +02:00
|
|
|
res.status(200).json({
|
|
|
|
success: true,
|
2021-10-04 12:11:41 +02:00
|
|
|
data: categories,
|
|
|
|
});
|
|
|
|
});
|
2021-05-22 19:04:34 +02:00
|
|
|
|
|
|
|
// @desc Get single category
|
|
|
|
// @route GET /api/categories/:id
|
|
|
|
// @access Public
|
|
|
|
exports.getCategory = asyncWrapper(async (req, res, next) => {
|
|
|
|
const category = await Category.findOne({
|
2021-05-23 17:18:04 +02:00
|
|
|
where: { id: req.params.id },
|
2021-10-04 12:11:41 +02:00
|
|
|
include: [
|
2021-06-26 15:06:47 -04:00
|
|
|
{
|
|
|
|
model: App,
|
|
|
|
as: 'apps',
|
|
|
|
},
|
2021-10-04 12:11:41 +02:00
|
|
|
{
|
|
|
|
model: Bookmark,
|
|
|
|
as: 'bookmarks',
|
|
|
|
},
|
|
|
|
],
|
2021-05-22 19:04:34 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!category) {
|
2021-08-07 12:31:41 -04:00
|
|
|
if (req.params.id === exports.dockerDefaultCategory.id) {
|
|
|
|
category = exports.dockerDefaultCategory;
|
2021-09-06 11:45:10 -04:00
|
|
|
} else if (req.params.id === exports.kubernetesDefaultCategory.id) {
|
|
|
|
category = exports.kubernetesDefaultCategory;
|
2021-08-07 12:31:41 -04:00
|
|
|
} else {
|
|
|
|
return next(
|
|
|
|
new ErrorResponse(
|
|
|
|
`Category with id of ${req.params.id} was not found`,
|
|
|
|
404
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2021-05-22 19:04:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
success: true,
|
2021-10-04 12:11:41 +02:00
|
|
|
data: category,
|
|
|
|
});
|
|
|
|
});
|
2021-05-22 19:04:34 +02:00
|
|
|
|
|
|
|
// @desc Update category
|
|
|
|
// @route PUT /api/categories/:id
|
|
|
|
// @access Public
|
|
|
|
exports.updateCategory = asyncWrapper(async (req, res, next) => {
|
|
|
|
let category = await Category.findOne({
|
2021-10-04 12:11:41 +02:00
|
|
|
where: { id: req.params.id },
|
2021-05-22 19:04:34 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!category) {
|
2021-10-04 12:11:41 +02:00
|
|
|
return next(
|
|
|
|
new ErrorResponse(
|
|
|
|
`Category with id of ${req.params.id} was not found`,
|
|
|
|
404
|
|
|
|
)
|
|
|
|
);
|
2021-05-22 19:04:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
category = await category.update({ ...req.body });
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
success: true,
|
2021-10-04 12:11:41 +02:00
|
|
|
data: category,
|
|
|
|
});
|
|
|
|
});
|
2021-05-22 19:04:34 +02:00
|
|
|
|
|
|
|
// @desc Delete category
|
|
|
|
// @route DELETE /api/categories/:id
|
|
|
|
// @access Public
|
|
|
|
exports.deleteCategory = asyncWrapper(async (req, res, next) => {
|
2021-05-25 14:05:53 +02:00
|
|
|
const category = await Category.findOne({
|
|
|
|
where: { id: req.params.id },
|
2021-10-04 12:11:41 +02:00
|
|
|
include: [
|
2021-06-26 15:06:47 -04:00
|
|
|
{
|
|
|
|
model: App,
|
|
|
|
as: 'apps',
|
|
|
|
},
|
2021-10-04 12:11:41 +02:00
|
|
|
{
|
|
|
|
model: Bookmark,
|
|
|
|
as: 'bookmarks',
|
|
|
|
},
|
|
|
|
],
|
2021-05-25 14:05:53 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (!category) {
|
2021-10-04 12:11:41 +02:00
|
|
|
return next(
|
|
|
|
new ErrorResponse(
|
|
|
|
`Category with id of ${req.params.id} was not found`,
|
|
|
|
404
|
|
|
|
)
|
|
|
|
);
|
2021-05-25 14:05:53 +02:00
|
|
|
}
|
|
|
|
|
2021-06-26 15:06:47 -04:00
|
|
|
category.apps.forEach(async (app) => {
|
|
|
|
await App.destroy({
|
|
|
|
where: { id: app.id },
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-05-25 14:05:53 +02:00
|
|
|
category.bookmarks.forEach(async (bookmark) => {
|
|
|
|
await Bookmark.destroy({
|
2021-10-04 12:11:41 +02:00
|
|
|
where: { id: bookmark.id },
|
|
|
|
});
|
|
|
|
});
|
2021-05-25 14:05:53 +02:00
|
|
|
|
2021-05-22 19:04:34 +02:00
|
|
|
await Category.destroy({
|
2021-10-04 12:11:41 +02:00
|
|
|
where: { id: req.params.id },
|
|
|
|
});
|
2021-05-22 19:04:34 +02:00
|
|
|
|
2021-06-18 13:42:55 +02:00
|
|
|
res.status(200).json({
|
|
|
|
success: true,
|
2021-10-04 12:11:41 +02:00
|
|
|
data: {},
|
|
|
|
});
|
|
|
|
});
|
2021-06-18 13:42:55 +02:00
|
|
|
|
|
|
|
// @desc Reorder categories
|
|
|
|
// @route PUT /api/categories/0/reorder
|
|
|
|
// @access Public
|
|
|
|
exports.reorderCategories = asyncWrapper(async (req, res, next) => {
|
|
|
|
req.body.categories.forEach(async ({ id, orderId }) => {
|
2021-10-04 12:11:41 +02:00
|
|
|
await Category.update(
|
|
|
|
{ orderId },
|
|
|
|
{
|
|
|
|
where: { id },
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2021-06-18 13:42:55 +02:00
|
|
|
|
2021-05-22 19:04:34 +02:00
|
|
|
res.status(200).json({
|
|
|
|
success: true,
|
2021-10-04 12:11:41 +02:00
|
|
|
data: {},
|
|
|
|
});
|
|
|
|
});
|