2020-03-19 22:59:06 +01:00
|
|
|
const errorHandling = require('../assets/utils/errorHandling');
|
2020-03-20 16:49:45 +01:00
|
|
|
const { serverError } = require('../assets/config/errors');
|
|
|
|
const Functions = require('../models/functions');
|
|
|
|
const Categories = require('../models/categories');
|
2020-03-17 12:02:12 +01:00
|
|
|
const functionToExecute = require('../assets/functions/functionObject');
|
2020-04-09 14:31:33 +02:00
|
|
|
const helperQueryNumber = require('../assets/utils/helperQueryNumber');
|
2020-05-01 20:41:59 +02:00
|
|
|
const getPagesHelper = require('../assets/utils/getPagesHelper');
|
2020-03-20 22:57:43 +01:00
|
|
|
const Sequelize = require('sequelize');
|
|
|
|
|
2020-05-01 20:41:59 +02:00
|
|
|
exports.getFunctions = async (req, res, next) => {
|
2020-03-20 22:57:43 +01:00
|
|
|
const categoryId = helperQueryNumber(req.query.categoryId, 0);
|
2020-05-02 15:51:51 +02:00
|
|
|
let { search } = req.query;
|
2020-05-01 20:41:59 +02:00
|
|
|
try { search = search.toLowerCase(); } catch {};
|
|
|
|
const options = {
|
2020-03-20 22:57:43 +01:00
|
|
|
where: {
|
|
|
|
isOnline: 1,
|
|
|
|
// Trie par catégorie
|
|
|
|
... (categoryId !== 0) && { categorieId: categoryId },
|
|
|
|
// Recherche
|
|
|
|
... (search != undefined) && {
|
|
|
|
[Sequelize.Op.or]: [
|
|
|
|
{ title: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('title')), 'LIKE', `%${search}%`) },
|
|
|
|
{ slug: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('slug')), 'LIKE', `%${search}%`) },
|
|
|
|
{ description: Sequelize.where(Sequelize.fn('LOWER', Sequelize.col('description')), 'LIKE', `%${search}%`) }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
2020-03-20 16:49:45 +01:00
|
|
|
include: [
|
|
|
|
{ model: Categories, attributes: ["name", "color"] }
|
2020-03-23 14:42:26 +01:00
|
|
|
],
|
|
|
|
attributes: {
|
|
|
|
exclude: ["updatedAt", "utilizationForm", "article", "isOnline"]
|
2020-04-07 16:21:48 +02:00
|
|
|
},
|
|
|
|
order: [['createdAt', 'DESC']]
|
2020-05-01 20:41:59 +02:00
|
|
|
};
|
|
|
|
return await getPagesHelper({ req, res, next }, Functions, options);
|
2020-03-20 16:49:45 +01:00
|
|
|
}
|
|
|
|
|
2020-03-23 16:50:31 +01:00
|
|
|
exports.getFunctionBySlug = (req, res, next) => {
|
|
|
|
const { slug } = req.params;
|
|
|
|
Functions.findOne({
|
|
|
|
where: { slug, isOnline: 1 },
|
|
|
|
attributes: {
|
|
|
|
exclude: ["updatedAt", "isOnline"]
|
|
|
|
},
|
|
|
|
include: [
|
|
|
|
{ model: Categories, attributes: ["name", "color"] }
|
|
|
|
]
|
|
|
|
})
|
|
|
|
.then((result) => {
|
|
|
|
if (!result) {
|
|
|
|
return errorHandling(next, { message: "La fonction n'existe pas.", statusCode: 404 });
|
|
|
|
}
|
|
|
|
return res.status(200).json(result);
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.log(error);
|
2020-04-07 16:21:48 +02:00
|
|
|
return errorHandling(next, serverError);
|
2020-03-23 16:50:31 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.executeFunctionBySlug = (req, res, next) => {
|
|
|
|
const functionOutput = functionToExecute(req.params.slug);
|
2020-03-17 18:35:03 +01:00
|
|
|
if (functionOutput !== undefined) {
|
2020-03-19 22:59:06 +01:00
|
|
|
return functionOutput({ res, next }, req.body);
|
2020-03-17 12:02:12 +01:00
|
|
|
}
|
2020-03-19 22:59:06 +01:00
|
|
|
return errorHandling(next, { message: "La fonction n'existe pas.", statusCode: 404 });
|
2020-03-17 12:02:12 +01:00
|
|
|
}
|