2020-04-22 20:44:06 +02:00
|
|
|
const path = require('path');
|
|
|
|
const fs = require('fs');
|
|
|
|
const { validationResult } = require('express-validator');
|
|
|
|
const errorHandling = require('../assets/utils/errorHandling');
|
|
|
|
const { serverError } = require('../assets/config/errors');
|
|
|
|
const Functions = require('../models/functions');
|
|
|
|
const Categories = require('../models/categories');
|
|
|
|
const Quotes = require('../models/quotes');
|
|
|
|
const Users = require('../models/users');
|
|
|
|
const helperQueryNumber = require('../assets/utils/helperQueryNumber');
|
|
|
|
const Sequelize = require('sequelize');
|
|
|
|
const deleteFilesNameStartWith = require('../assets/utils/deleteFilesNameStartWith');
|
|
|
|
const { EMAIL_INFO, FRONT_END_HOST } = require('../assets/config/config');
|
|
|
|
const transporter = require('../assets/config/transporter');
|
|
|
|
const { emailQuoteTemplate } = require('../assets/config/emails');
|
2020-04-13 22:11:34 +02:00
|
|
|
|
2020-04-15 21:32:25 +02:00
|
|
|
const handleEditFunction = async (res, resultFunction, { title, slug, description, type, categorieId, isOnline }, imageName = false) => {
|
2020-04-13 22:11:34 +02:00
|
|
|
resultFunction.title = title;
|
|
|
|
resultFunction.slug = slug;
|
|
|
|
resultFunction.description = description;
|
|
|
|
resultFunction.type = type;
|
|
|
|
resultFunction.categorieId = categorieId;
|
2020-04-15 21:32:25 +02:00
|
|
|
resultFunction.isOnline = isOnline;
|
2020-04-13 22:11:34 +02:00
|
|
|
if (imageName) {
|
|
|
|
resultFunction.image = `/images/functions/${imageName}`;
|
|
|
|
}
|
|
|
|
const result = await resultFunction.save();
|
|
|
|
res.status(200).json({ message: "La fonction a bien été modifié!", result });
|
|
|
|
}
|
2020-04-11 23:29:22 +02:00
|
|
|
|
|
|
|
exports.getFunctions = (req, res, next) => {
|
|
|
|
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 {}
|
|
|
|
const offset = (page - 1) * limit;
|
|
|
|
Functions.findAndCountAll({
|
|
|
|
limit,
|
|
|
|
offset,
|
|
|
|
where: {
|
|
|
|
// 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}%`) }
|
|
|
|
]
|
|
|
|
}
|
|
|
|
},
|
|
|
|
include: [
|
|
|
|
{ model: Categories, attributes: ["name", "color"] }
|
|
|
|
],
|
|
|
|
attributes: {
|
|
|
|
exclude: ["updatedAt", "utilizationForm", "article", "isOnline"]
|
|
|
|
},
|
|
|
|
order: [['createdAt', 'DESC']]
|
|
|
|
})
|
|
|
|
.then((result) => {
|
|
|
|
const { count, rows } = result;
|
|
|
|
const hasMore = (page * limit) < count;
|
|
|
|
return res.status(200).json({ totalItems: count, hasMore, rows });
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.log(error);
|
|
|
|
return errorHandling(next, serverError);
|
|
|
|
});
|
|
|
|
}
|
2020-03-25 16:23:43 +01:00
|
|
|
|
2020-04-15 16:40:09 +02:00
|
|
|
exports.getFunctionBySlug = (req, res, next) => {
|
|
|
|
const { slug } = req.params;
|
2020-04-15 14:17:57 +02:00
|
|
|
Functions.findOne({
|
2020-04-15 16:40:09 +02:00
|
|
|
where: { slug },
|
2020-04-15 14:17:57 +02:00
|
|
|
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);
|
|
|
|
return errorHandling(next, serverError);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-25 16:23:43 +01:00
|
|
|
exports.postFunction = (req, res, next) => {
|
2020-03-25 22:59:08 +01:00
|
|
|
const { title, slug, description, type, categorieId } = req.body;
|
|
|
|
const image = req.files.image;
|
|
|
|
const errors = validationResult(req);
|
|
|
|
if (!errors.isEmpty()) {
|
|
|
|
return errorHandling(next, { message: errors.array()[0].msg, statusCode: 400 });
|
|
|
|
}
|
|
|
|
if (!image || image.truncated && (
|
|
|
|
image.mimetype !== 'image/png' ||
|
|
|
|
image.mimetype !== 'image/jpg' ||
|
|
|
|
image.mimetype !== 'image/jpeg'
|
|
|
|
)) {
|
|
|
|
return errorHandling(next, { message:"La fonction doit avoir une image valide.", statusCode: 400 });
|
|
|
|
}
|
2020-04-11 23:29:22 +02:00
|
|
|
const splitedImageName = image.name.split('.');
|
|
|
|
if (splitedImageName.length !== 2) return errorHandling(next, serverError);
|
|
|
|
const imageName = slug + '.' + splitedImageName[1];
|
2020-03-25 22:59:08 +01:00
|
|
|
image.mv(path.join(__dirname, '..', 'assets', 'images', 'functions') + '/' + imageName, async (error) => {
|
|
|
|
if (error) return errorHandling(next, serverError);
|
|
|
|
try {
|
2020-04-12 22:38:13 +02:00
|
|
|
const result = await Functions.create({ title, slug, description, type, categorieId, image: `/images/functions/${imageName}` });
|
|
|
|
return res.status(201).json({ message: "La fonction a été correctement ajouté!", result });
|
2020-03-25 22:59:08 +01:00
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
2020-04-07 16:21:48 +02:00
|
|
|
return errorHandling(next, serverError);
|
2020-03-25 22:59:08 +01:00
|
|
|
}
|
|
|
|
});
|
2020-03-25 23:30:33 +01:00
|
|
|
}
|
|
|
|
|
2020-04-13 22:11:34 +02:00
|
|
|
exports.putFunction = async (req, res, next) => {
|
2020-04-15 21:32:25 +02:00
|
|
|
const { id } = req.params;
|
|
|
|
const { title, slug, description, type, categorieId, isOnline } = req.body;
|
|
|
|
const image = req.files.image;
|
2020-04-13 22:11:34 +02:00
|
|
|
const errors = validationResult(req);
|
|
|
|
if (!errors.isEmpty()) {
|
|
|
|
return errorHandling(next, { message: errors.array()[0].msg, statusCode: 400 });
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
// Vérifie si la fonction existe
|
|
|
|
const resultFunction = await Functions.findOne({ where: { id } });
|
|
|
|
if (!resultFunction) {
|
|
|
|
return errorHandling(next, { message: "La fonction n'existe pas.", statusCode: 404 });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Vérifie si le slug existe déjà
|
|
|
|
const FunctionSlug = await Functions.findOne({ where: { slug } });
|
|
|
|
if (!FunctionSlug && FunctionSlug.id != resultFunction.id) {
|
|
|
|
return errorHandling(next, { message: "Le slug existe déjà...", statusCode: 404 });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sauvegarde de la fonction
|
|
|
|
if (image != undefined) {
|
|
|
|
if (image.truncated && (
|
|
|
|
image.mimetype !== 'image/png' ||
|
|
|
|
image.mimetype !== 'image/jpg' ||
|
|
|
|
image.mimetype !== 'image/jpeg'
|
|
|
|
)) {
|
|
|
|
return errorHandling(next, { message:"La fonction doit avoir une image valide.", statusCode: 400 });
|
|
|
|
}
|
|
|
|
const splitedImageName = image.name.split('.');
|
|
|
|
if (splitedImageName.length !== 2) return errorHandling(next, serverError);
|
|
|
|
const imageName = slug + '.' + splitedImageName[1];
|
|
|
|
// Supprime les anciennes images
|
2020-04-15 13:55:07 +02:00
|
|
|
const functionPath = path.join(__dirname, '..', 'assets', 'images', 'functions');
|
|
|
|
deleteFilesNameStartWith(slug, functionPath, () => {
|
|
|
|
image.mv(path.join(functionPath, imageName), async (error) => {
|
|
|
|
if (error) return errorHandling(next, serverError);
|
2020-04-15 21:32:25 +02:00
|
|
|
return await handleEditFunction(res, resultFunction, { title, slug, description, type, categorieId, isOnline }, imageName);
|
2020-04-15 13:55:07 +02:00
|
|
|
});
|
2020-04-13 22:11:34 +02:00
|
|
|
});
|
|
|
|
} else {
|
2020-04-15 21:32:25 +02:00
|
|
|
return await handleEditFunction(res, resultFunction, { title, slug, description, type, categorieId, isOnline });
|
2020-04-13 22:11:34 +02:00
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
return errorHandling(next, serverError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 14:17:57 +02:00
|
|
|
exports.putFunctionArticle = async (req, res, next) => {
|
|
|
|
const { id } = req.params;
|
|
|
|
const { article } = req.body;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Vérifie si la fonction existe
|
|
|
|
const resultFunction = await Functions.findOne({ where: { id } });
|
|
|
|
if (!resultFunction) {
|
|
|
|
return errorHandling(next, { message: "La fonction n'existe pas.", statusCode: 404 });
|
|
|
|
}
|
|
|
|
resultFunction.article = article;
|
|
|
|
const result = await resultFunction.save();
|
|
|
|
return res.status(200).json(result);
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
return errorHandling(next, serverError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.putFunctionForm = async (req, res, next) => {
|
|
|
|
const { id } = req.params;
|
|
|
|
const { form } = req.body;
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Vérifie si la fonction existe
|
|
|
|
const resultFunction = await Functions.findOne({ where: { id } });
|
|
|
|
if (!resultFunction) {
|
|
|
|
return errorHandling(next, { message: "La fonction n'existe pas.", statusCode: 404 });
|
|
|
|
}
|
2020-04-16 19:44:19 +02:00
|
|
|
resultFunction.utilizationForm = form;
|
2020-04-15 14:17:57 +02:00
|
|
|
const result = await resultFunction.save();
|
|
|
|
return res.status(200).json(result);
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
return errorHandling(next, serverError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-25 23:30:33 +01:00
|
|
|
exports.deleteFunction = async (req, res, next) => {
|
|
|
|
const { id } = req.params;
|
|
|
|
try {
|
|
|
|
const result = await Functions.findOne({ where: { id } });
|
|
|
|
if (!result) {
|
|
|
|
return errorHandling(next, { message: "La fonction n'existe pas.", statusCode: 404 });
|
|
|
|
}
|
|
|
|
if (result.image !== "/images/functions/default.png") {
|
|
|
|
const filePath = path.join(__dirname, '..', 'assets', result.image);
|
|
|
|
fs.unlinkSync(filePath); // supprime le fichier
|
|
|
|
}
|
2020-04-10 00:01:39 +02:00
|
|
|
await result.destroy();
|
2020-03-25 23:30:33 +01:00
|
|
|
res.status(200).json({ message: "La fonction a été correctement supprimé!"});
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
2020-04-07 16:21:48 +02:00
|
|
|
return errorHandling(next, serverError);
|
2020-03-25 23:30:33 +01:00
|
|
|
}
|
2020-04-12 22:38:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.postCategory = async (req, res, next) => {
|
|
|
|
const { name, color } = req.body;
|
|
|
|
if (!(name && color)) {
|
|
|
|
return errorHandling(next, { message: "La catégorie doit avoir un nom et une couleur." });
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const result = await Categories.create({ name, color });
|
|
|
|
return res.status(201).json({ message: "La catégorie a bien été crée!", result });
|
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
return errorHandling(next, serverError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.putCategory = async (req, res, next) => {
|
|
|
|
const { name, color } = req.body;
|
|
|
|
const { id } = req.params;
|
|
|
|
if (!(name && color && id)) {
|
|
|
|
return errorHandling(next, { message: "La catégorie doit avoir un nom, une couleur et un id." });
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const category = await Categories.findOne({ where: { id } });
|
|
|
|
if (!category) {
|
|
|
|
return errorHandling(next, { message: "La catégorie n'existe pas." });
|
|
|
|
}
|
|
|
|
category.name = name;
|
|
|
|
category.color = color;
|
|
|
|
const result = await category.save();
|
2020-04-22 12:21:02 +02:00
|
|
|
return res.status(200).json({ message: "La catégorie a bien été modifiée!", result });
|
2020-04-12 22:38:13 +02:00
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
return errorHandling(next, serverError);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.deleteCategory = async (req, res, next) => {
|
|
|
|
const { id } = req.params;
|
|
|
|
try {
|
|
|
|
const category = await Categories.findOne({ where: { id } });
|
|
|
|
if (!category) {
|
|
|
|
return errorHandling(next, { message: "La catégorie n'existe pas." });
|
|
|
|
}
|
|
|
|
await category.destroy();
|
2020-04-22 12:21:02 +02:00
|
|
|
return res.status(200).json({ message: "La catégorie a bien été supprimée!" });
|
2020-04-12 22:38:13 +02:00
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
return errorHandling(next, serverError);
|
|
|
|
}
|
2020-04-22 12:21:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.getQuotes = (req, res, next) => {
|
|
|
|
const page = helperQueryNumber(req.query.page, 1);
|
|
|
|
const limit = helperQueryNumber(req.query.limit, 10);
|
|
|
|
const offset = (page - 1) * limit;
|
|
|
|
Quotes.findAndCountAll({
|
|
|
|
limit,
|
|
|
|
offset,
|
|
|
|
where: {
|
|
|
|
isValidated: 0,
|
|
|
|
},
|
|
|
|
include: [
|
|
|
|
{ model: Users, attributes: ["name", "logo"] }
|
|
|
|
],
|
|
|
|
order: [['createdAt', 'DESC']]
|
|
|
|
})
|
|
|
|
.then((result) => {
|
|
|
|
const { count, rows } = result;
|
|
|
|
const hasMore = (page * limit) < count;
|
|
|
|
return res.status(200).json({ totalItems: count, hasMore, rows });
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
console.log(error);
|
|
|
|
return errorHandling(next, serverError);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.putQuote = async (req, res, next) => {
|
|
|
|
const { id } = req.params;
|
|
|
|
const { isValid } = req.body;
|
|
|
|
try {
|
|
|
|
if (typeof isValid !== 'boolean') {
|
|
|
|
return errorHandling(next, { message: "isValid doit être un booléen.", statusCode: 400 });
|
|
|
|
}
|
2020-04-22 20:44:06 +02:00
|
|
|
const quote = await Quotes.findOne({
|
|
|
|
where: {
|
|
|
|
id,
|
|
|
|
isValidated: 0
|
|
|
|
},
|
|
|
|
include: [
|
|
|
|
{ model: Users, attributes: ["name", "email"] }
|
|
|
|
]
|
|
|
|
});
|
2020-04-22 12:21:02 +02:00
|
|
|
if (!quote) {
|
|
|
|
return errorHandling(next, { message: "La citation n'existe pas (ou est déjà validé).", statusCode: 404 });
|
|
|
|
}
|
2020-04-22 20:44:06 +02:00
|
|
|
|
|
|
|
await transporter.sendMail({
|
|
|
|
from: `"FunctionProject" <${EMAIL_INFO.auth.user}>`,
|
|
|
|
to: quote.user.email,
|
|
|
|
subject: "FunctionProject - Citation proposée",
|
|
|
|
html: emailQuoteTemplate(isValid, quote, FRONT_END_HOST)
|
|
|
|
});
|
|
|
|
|
2020-04-22 12:21:02 +02:00
|
|
|
if (isValid) {
|
|
|
|
quote.isValidated = true;
|
2020-04-22 20:44:06 +02:00
|
|
|
await quote.save();
|
|
|
|
return res.status(200).json({ message: "La citation a bien été validée!" });
|
|
|
|
} else {
|
|
|
|
await quote.destroy();
|
|
|
|
return res.status(200).json({ imessage: "La citation a bien été supprimée!" });
|
|
|
|
}
|
|
|
|
|
2020-04-22 12:21:02 +02:00
|
|
|
} catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
return errorHandling(next, serverError);
|
|
|
|
}
|
2020-03-25 16:23:43 +01:00
|
|
|
}
|