backend: PUT /users - Modifier le profil
This commit is contained in:
@ -11,68 +11,66 @@ const AdminRouter = Router();
|
||||
|
||||
// Permet de créé une fonction
|
||||
AdminRouter.post('/functions', isAuth, isAdmin,
|
||||
fileUpload({
|
||||
useTempFiles: true,
|
||||
safeFileNames: true,
|
||||
preserveExtension: Number,
|
||||
limits: { fileSize: 5 * 1024 * 1024 }, // 5mb,
|
||||
parseNested: true
|
||||
}),
|
||||
[
|
||||
body('title')
|
||||
.not()
|
||||
.isEmpty()
|
||||
.withMessage("La fonction doit avoir un titre.")
|
||||
.isLength({ max: 100 })
|
||||
.withMessage("Le titre est trop long."),
|
||||
body('slug')
|
||||
.not()
|
||||
.isEmpty()
|
||||
.withMessage("La fonction doit avoir un slug.")
|
||||
.isLength({ max: 100 })
|
||||
.withMessage("Le slug est trop long.")
|
||||
.custom((async (slug) => {
|
||||
try {
|
||||
const FunctionSlug = await Functions.findOne({ where: { slug } });
|
||||
if (FunctionSlug) {
|
||||
return Promise.reject("Le slug existe déjà...");
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
fileUpload({
|
||||
useTempFiles: true,
|
||||
safeFileNames: true,
|
||||
preserveExtension: Number,
|
||||
limits: { fileSize: 5 * 1024 * 1024 }, // 5mb,
|
||||
parseNested: true
|
||||
}),
|
||||
[
|
||||
body('title')
|
||||
.not()
|
||||
.isEmpty()
|
||||
.withMessage("La fonction doit avoir un titre.")
|
||||
.isLength({ max: 100 })
|
||||
.withMessage("Le titre est trop long."),
|
||||
body('slug')
|
||||
.not()
|
||||
.isEmpty()
|
||||
.withMessage("La fonction doit avoir un slug.")
|
||||
.isLength({ max: 100 })
|
||||
.withMessage("Le slug est trop long.")
|
||||
.custom((async (slug) => {
|
||||
try {
|
||||
const FunctionSlug = await Functions.findOne({ where: { slug } });
|
||||
if (FunctionSlug) {
|
||||
return Promise.reject("Le slug existe déjà...");
|
||||
}
|
||||
return true;
|
||||
})),
|
||||
body('description')
|
||||
.not()
|
||||
.isEmpty()
|
||||
.withMessage("La fonction doit avoir une description.")
|
||||
.isLength({ max: 255 })
|
||||
.withMessage("La description est trop longue."),
|
||||
body('categorieId')
|
||||
.not()
|
||||
.isEmpty()
|
||||
.withMessage("La fonction doit avoir une catégorie.")
|
||||
.custom(async (categorieId) => {
|
||||
try {
|
||||
const categorieFound = await Categories.findOne({ where: { id: categorieId } });
|
||||
if (!categorieFound) {
|
||||
return Promise.reject("La catégorie n'existe pas!");
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
return true;
|
||||
})),
|
||||
body('description')
|
||||
.not()
|
||||
.isEmpty()
|
||||
.withMessage("La fonction doit avoir une description.")
|
||||
.isLength({ max: 255 })
|
||||
.withMessage("La description est trop longue."),
|
||||
body('categorieId')
|
||||
.not()
|
||||
.isEmpty()
|
||||
.withMessage("La fonction doit avoir une catégorie.")
|
||||
.custom(async (categorieId) => {
|
||||
try {
|
||||
const categorieFound = await Categories.findOne({ where: { id: categorieId } });
|
||||
if (!categorieFound) {
|
||||
return Promise.reject("La catégorie n'existe pas!");
|
||||
}
|
||||
return true;
|
||||
}),
|
||||
body('type')
|
||||
.custom((type) => {
|
||||
if (!(type === 'article' || type === 'form' || type === 'page')) {
|
||||
return Promise.reject('Le type de la fonction peut être : article, form ou page.');
|
||||
}
|
||||
return true;
|
||||
})
|
||||
],
|
||||
adminController.postFunction
|
||||
);
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
return true;
|
||||
}),
|
||||
body('type')
|
||||
.custom((type) => {
|
||||
if (!(type === 'article' || type === 'form' || type === 'page')) {
|
||||
return Promise.reject('Le type de la fonction peut être : article, form ou page.');
|
||||
}
|
||||
return true;
|
||||
})
|
||||
], adminController.postFunction);
|
||||
|
||||
// Supprime une fonction avec son id
|
||||
AdminRouter.delete('/functions/:id', isAuth, isAdmin, adminController.deleteFunction);
|
||||
|
@ -1,8 +1,10 @@
|
||||
const { Router } = require('express');
|
||||
const { body } = require('express-validator');
|
||||
const fileUpload = require('express-fileupload');
|
||||
const usersController = require('../controllers/users');
|
||||
const { requiredFields } = require('../assets/config/errors');
|
||||
const Users = require('../models/users');
|
||||
const isAuth = require('../middlewares/isAuth');
|
||||
|
||||
const UsersRouter = Router();
|
||||
|
||||
@ -21,6 +23,59 @@ UsersRouter.post('/login', [
|
||||
// Récupère les informations public d'un profil
|
||||
UsersRouter.get('/:name', usersController.getUserInfo);
|
||||
|
||||
// Permet de modifier son profil
|
||||
UsersRouter.put('/', isAuth,
|
||||
fileUpload({
|
||||
useTempFiles: true,
|
||||
safeFileNames: true,
|
||||
preserveExtension: Number,
|
||||
limits: { fileSize: 5 * 1024 * 1024 }, // 5mb,
|
||||
parseNested: true
|
||||
}),
|
||||
[
|
||||
body('email')
|
||||
.isEmail()
|
||||
.withMessage("Veuillez rentré une adresse mail valide.")
|
||||
.custom((async (email) => {
|
||||
try {
|
||||
const user = await Users.findOne({ where: { email } });
|
||||
if (user && user.email !== email) {
|
||||
return Promise.reject("L'adresse email existe déjà...");
|
||||
}
|
||||
} catch (error) {
|
||||
return console.log(error);
|
||||
}
|
||||
return true;
|
||||
}))
|
||||
.normalizeEmail(),
|
||||
body('name')
|
||||
.trim()
|
||||
.not()
|
||||
.isEmpty()
|
||||
.withMessage("Vous devez avoir un nom (ou pseudo).")
|
||||
.isAlphanumeric()
|
||||
.withMessage("Votre nom ne peut contenir que des lettres ou/et des nombres.")
|
||||
.isLength({ max: 30 })
|
||||
.withMessage("Votre nom est trop long")
|
||||
.custom(async (name) => {
|
||||
try {
|
||||
const user = await Users.findOne({ where: { name } });
|
||||
if (user && user.name !== name) {
|
||||
return Promise.reject("Le nom existe déjà...");
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
}
|
||||
return true;
|
||||
}),
|
||||
body('isPublicEmail')
|
||||
.isBoolean()
|
||||
.withMessage("L'adresse email peut être public ou privé, rien d'autre."),
|
||||
body('biography')
|
||||
.trim()
|
||||
.escape()
|
||||
], usersController.putUser);
|
||||
|
||||
// Permet de s'inscrire
|
||||
UsersRouter.post('/register', [
|
||||
body('email')
|
||||
|
Reference in New Issue
Block a user