backend: PUT /users - Modifier le profil

This commit is contained in:
Divlo
2020-04-07 16:21:48 +02:00
parent de28cbe706
commit 5bf9a2ade6
6 changed files with 180 additions and 75 deletions

View File

@ -10,7 +10,6 @@ exports.postFunction = (req, res, next) => {
const image = req.files.image;
const errors = validationResult(req);
if (!errors.isEmpty()) {
console.log(errors.array())
return errorHandling(next, { message: errors.array()[0].msg, statusCode: 400 });
}
if (!image || image.truncated && (
@ -28,7 +27,7 @@ exports.postFunction = (req, res, next) => {
return res.status(201).json({ message: "La fonction a été correctement ajouté!"});
} catch (error) {
console.log(error);
errorHandling(next, serverError);
return errorHandling(next, serverError);
}
});
}
@ -48,6 +47,6 @@ exports.deleteFunction = async (req, res, next) => {
res.status(200).json({ message: "La fonction a été correctement supprimé!"});
} catch (error) {
console.log(error);
errorHandling(next, serverError);
return errorHandling(next, serverError);
}
}

View File

@ -8,6 +8,6 @@ exports.getCategories = (_req, res, next) => {
})
.catch((error) => {
console.log(error);
errorHandling(next, serverError);
return errorHandling(next, serverError);
});
}

View File

@ -37,7 +37,8 @@ exports.getFunctions = (req, res, next) => {
],
attributes: {
exclude: ["updatedAt", "utilizationForm", "article", "isOnline"]
}
},
order: [['createdAt', 'DESC']]
})
.then((result) => {
const { count, rows } = result;
@ -46,7 +47,7 @@ exports.getFunctions = (req, res, next) => {
})
.catch((error) => {
console.log(error);
errorHandling(next, serverError);
return errorHandling(next, serverError);
});
}
@ -69,7 +70,7 @@ exports.getFunctionBySlug = (req, res, next) => {
})
.catch((error) => {
console.log(error);
errorHandling(next, serverError);
return errorHandling(next, serverError);
});
}

View File

@ -1,3 +1,4 @@
const path = require('path');
const { validationResult } = require('express-validator');
const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');
@ -14,6 +15,55 @@ const Functions = require('../models/functions');
const Categories = require('../models/categories');
const Comments = require('../models/comments');
async function handleEditUser(res, { name, email, biography, isPublicEmail }, userId, logoName) {
const user = await Users.findOne({ where: { id: userId } });
user.name = name;
user.email = email;
user.biography = biography;
user.isPublicEmail = isPublicEmail;
if (logoName != undefined) {
user.logo = `/images/users/${logoName}`;
}
await user.save();
return res.status(200).json({ message: "Le profil a bien été modifié!" });
}
exports.putUser = (req, res, next) => {
const { name, email, biography, isPublicEmail } = req.body;
const logo = req.files.logo;
const errors = validationResult(req);
if (!errors.isEmpty()) {
return errorHandling(next, { message: errors.array()[0].msg, statusCode: 400 });
}
if (logo != undefined) {
if (!logo || logo.truncated && (
logo.mimetype !== 'image/png' ||
logo.mimetype !== 'image/jpg' ||
logo.mimetype !== 'image/jpeg' ||
logo.mimetype !== 'image/gif'
)) {
return errorHandling(next, { message:"Le profil doit avoir une image valide.", statusCode: 400 });
}
const logoName = name + uuid.v4() + logo.name;
logo.mv(path.join(__dirname, '..', 'assets', 'images', 'users') + '/' + logoName, async (error) => {
if (error) return errorHandling(next, serverError);
try {
return handleEditUser(res, { name, email, biography, isPublicEmail }, req.userId, logoName);
} catch (error) {
console.log(error);
return errorHandling(next, serverError);
}
});
} else {
try {
return handleEditUser(res, { name, email, biography, isPublicEmail }, req.userId, null);
} catch (error) {
console.log(error);
return errorHandling(next, serverError);
}
}
}
exports.register = async (req, res, next) => {
const { name, email, password } = req.body;
const errors = validationResult(req);
@ -33,7 +83,7 @@ exports.register = async (req, res, next) => {
return res.status(201).json({ result: "Vous y êtes presque, veuillez vérifier vos emails pour confirmer l'inscription." });
} catch (error) {
console.log(error);
errorHandling(next, serverError);
return errorHandling(next, serverError);
}
}
@ -57,11 +107,11 @@ exports.login = async (req, res, next) => {
}
const token = jwt.sign({
email: user.email, userId: user.id
}, JWT_SECRET, { expiresIn: '1h' });
}, JWT_SECRET, { expiresIn: '3h' });
return res.status(200).json({ token, id: user.id, name: user.name, email: user.email, biography: user.biography, logo: user.logo, isPublicEmail: user.isPublicEmail, isAdmin: user.isAdmin, createdAt: user.createdAt });
} catch (error) {
console.log(error);
errorHandling(next, serverError);
return errorHandling(next, serverError);
}
}
@ -81,7 +131,7 @@ exports.confirmEmail = async (req, res, next) => {
return res.redirect(`${FRONT_END_HOST}/login?isConfirmed=true`);
} catch (error) {
console.log(error);
errorHandling(next, serverError);
return errorHandling(next, serverError);
}
}
@ -109,7 +159,7 @@ exports.resetPassword = async (req, res, next) => {
return res.status(200).json({ result: "Demande de réinitialisation du mot de passe réussi, veuillez vérifier vos emails!" });
} catch (error) {
console.log(error);
errorHandling(next, serverError);
return errorHandling(next, serverError);
}
}
@ -132,7 +182,7 @@ exports.newPassword = async (req, res, next) => {
return res.status(200).json({ result: "Le mot de passe a bien été modifié!" });
} catch (error) {
console.log(error);
errorHandling(next, serverError);
return errorHandling(next, serverError);
}
}
@ -156,10 +206,12 @@ exports.getUserInfo = async (req, res, next) => {
});
const favoritesArray = favorites.map((favorite) => favorite.function);
const comments = await Comments.findAll({
limit: 10,
where: { userId: user.id },
include: [
{ model: Functions, attributes: { exclude: ["updatedAt", "utilizationForm", "article", "isOnline"] } }
]
],
order: [['createdAt', 'DESC']]
});
const commentsArray = comments.map((commentObject) => {
return {
@ -182,6 +234,6 @@ exports.getUserInfo = async (req, res, next) => {
return res.status(200).json(userObject);
} catch (error) {
console.log(error);
errorHandling(next, serverError);
return errorHandling(next, serverError);
}
}