2021-05-10 15:47:26 +00:00
|
|
|
const asyncWrapper = require('../middleware/asyncWrapper');
|
|
|
|
const ErrorResponse = require('../utils/ErrorResponse');
|
|
|
|
const App = require('../models/App');
|
2021-06-09 08:58:45 +00:00
|
|
|
const Config = require('../models/Config');
|
2021-06-15 10:36:23 +00:00
|
|
|
const { Sequelize } = require('sequelize');
|
2021-05-10 15:47:26 +00:00
|
|
|
|
|
|
|
// @desc Create new app
|
|
|
|
// @route POST /api/apps
|
|
|
|
// @access Public
|
|
|
|
exports.createApp = asyncWrapper(async (req, res, next) => {
|
2021-06-09 08:58:45 +00:00
|
|
|
// Get config from database
|
|
|
|
const pinApps = await Config.findOne({
|
|
|
|
where: { key: 'pinAppsByDefault' }
|
|
|
|
});
|
|
|
|
|
|
|
|
let app;
|
2021-06-23 12:15:14 +00:00
|
|
|
let _body = { ...req.body };
|
|
|
|
|
|
|
|
if (req.file) {
|
|
|
|
_body.icon = req.file.filename;
|
|
|
|
}
|
|
|
|
|
2021-06-09 08:58:45 +00:00
|
|
|
|
|
|
|
if (pinApps) {
|
|
|
|
if (parseInt(pinApps.value)) {
|
|
|
|
app = await App.create({
|
2021-06-23 12:15:14 +00:00
|
|
|
..._body,
|
2021-06-09 08:58:45 +00:00
|
|
|
isPinned: true
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
app = await App.create(req.body);
|
|
|
|
}
|
|
|
|
}
|
2021-05-10 15:47:26 +00:00
|
|
|
|
|
|
|
res.status(201).json({
|
|
|
|
success: true,
|
|
|
|
data: app
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// @desc Get all apps
|
|
|
|
// @route GET /api/apps
|
|
|
|
// @access Public
|
|
|
|
exports.getApps = asyncWrapper(async (req, res, next) => {
|
2021-06-17 08:56:27 +00:00
|
|
|
// Get config from database
|
|
|
|
const useOrdering = await Config.findOne({
|
|
|
|
where: { key: 'useOrdering' }
|
2021-05-13 16:23:12 +00:00
|
|
|
});
|
2021-05-10 15:47:26 +00:00
|
|
|
|
2021-06-17 08:56:27 +00:00
|
|
|
const orderType = useOrdering ? useOrdering.value : 'createdAt';
|
|
|
|
let apps;
|
|
|
|
|
|
|
|
if (orderType == 'name') {
|
|
|
|
apps = await App.findAll({
|
|
|
|
order: [[ Sequelize.fn('lower', Sequelize.col('name')), 'ASC' ]]
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
apps = await App.findAll({
|
|
|
|
order: [[ orderType, 'ASC' ]]
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-05-10 15:47:26 +00:00
|
|
|
res.status(200).json({
|
|
|
|
success: true,
|
|
|
|
data: apps
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// @desc Get single app
|
|
|
|
// @route GET /api/apps/:id
|
|
|
|
// @access Public
|
|
|
|
exports.getApp = asyncWrapper(async (req, res, next) => {
|
|
|
|
const app = await App.findOne({
|
|
|
|
where: { id: req.params.id }
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!app) {
|
|
|
|
return next(new ErrorResponse(`App with id of ${req.params.id} was not found`, 404));
|
|
|
|
}
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
success: true,
|
|
|
|
data: app
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// @desc Update app
|
|
|
|
// @route PUT /api/apps/:id
|
|
|
|
// @access Public
|
|
|
|
exports.updateApp = asyncWrapper(async (req, res, next) => {
|
|
|
|
let app = await App.findOne({
|
|
|
|
where: { id: req.params.id }
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!app) {
|
|
|
|
return next(new ErrorResponse(`App with id of ${req.params.id} was not found`, 404));
|
|
|
|
}
|
|
|
|
|
|
|
|
app = await app.update({ ...req.body });
|
|
|
|
|
|
|
|
res.status(200).json({
|
|
|
|
success: true,
|
|
|
|
data: app
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// @desc Delete app
|
|
|
|
// @route DELETE /api/apps/:id
|
|
|
|
// @access Public
|
|
|
|
exports.deleteApp = asyncWrapper(async (req, res, next) => {
|
|
|
|
await App.destroy({
|
|
|
|
where: { id: req.params.id }
|
|
|
|
})
|
|
|
|
|
2021-06-15 14:02:57 +00:00
|
|
|
res.status(200).json({
|
|
|
|
success: true,
|
|
|
|
data: {}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
// @desc Reorder apps
|
|
|
|
// @route PUT /api/apps/0/reorder
|
|
|
|
// @access Public
|
|
|
|
exports.reorderApps = asyncWrapper(async (req, res, next) => {
|
|
|
|
req.body.apps.forEach(async ({ id, orderId }) => {
|
|
|
|
await App.update({ orderId }, {
|
|
|
|
where: { id }
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-05-10 15:47:26 +00:00
|
|
|
res.status(200).json({
|
|
|
|
success: true,
|
|
|
|
data: {}
|
|
|
|
})
|
|
|
|
})
|