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-03-20 22:57:43 +01:00
|
|
|
const Sequelize = require('sequelize');
|
|
|
|
|
|
|
|
function helperQueryNumber(value, defaultValue) {
|
|
|
|
if (value && !isNaN(value)) return parseInt(value);
|
|
|
|
return defaultValue;
|
|
|
|
}
|
2020-03-17 12:02:12 +01:00
|
|
|
|
2020-03-20 16:49:45 +01:00
|
|
|
exports.getFunctions = (req, res, next) => {
|
2020-03-20 22:57:43 +01:00
|
|
|
const page = helperQueryNumber(req.query.page, 1);
|
|
|
|
const limit = helperQueryNumber(req.query.limit, 10);
|
|
|
|
const categoryId = helperQueryNumber(req.query.categoryId, 0);
|
|
|
|
let search = req.query.search;
|
|
|
|
try { search = search.toLowerCase(); } catch {}
|
2020-03-20 16:49:45 +01:00
|
|
|
const offset = (page - 1) * limit;
|
|
|
|
Functions.findAndCountAll({
|
|
|
|
limit,
|
|
|
|
offset,
|
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-03-20 16:49:45 +01:00
|
|
|
})
|
|
|
|
.then((result) => {
|
|
|
|
const { count, rows } = result;
|
|
|
|
const hasMore = (page * limit) < count;
|
2020-03-23 16:50:31 +01:00
|
|
|
return res.status(200).json({ totalItems: count, hasMore, rows });
|
2020-03-20 16:49:45 +01:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.log(error);
|
|
|
|
errorHandling(next, serverError);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
errorHandling(next, serverError);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|