Commit 5d7846b0 authored by pdg16's avatar pdg16
Browse files

fix error messages

parent 0ee54beb
Pipeline #20854 failed with stage
in 1 minute and 18 seconds
......@@ -324,76 +324,101 @@ userApp.put('/', passport.authenticate('bearer', { session: false }), (req, res,
userApp.put('/:id', passport.authenticate('bearer', { session: false }), checkAdmin(), (req, res, next) => {
if(!mongoose.Types.ObjectId.isValid(req.params.id))
return res.json({msg: 'Invalid id'});
User.findById(req.params.id, (err, user) => {
if (err) {
log.error(err);
return next({err});
}
if(!user || user.origin != req.user.origin) {
res.statusCode = 404;
return next({err: {
message: 'Usuário não encontrado'
}});
}
console.log(req.body);
user.email = req.body.email || user.email;
user.name = req.body.name || user.name;
user.nickname = req.body.nickname || user.nickname || user.name;
user.cep = req.body.cep || user.cep;
user.complement = req.body.complement || user.complement;
user.address = req.body.address || user.address;
user.phone = req.body.phone || user.phone;
user.schooling = req.body.schooling || user.schooling;
user.course = req.body.course || user.course;
user.segment = req.body.segment || user.segment;
user.role = req.body.role || user.role;
user.institutionName = req.body.institutionName || user.institutionName;
user.state = req.body.state || user.state;
user.city = req.body.city || user.city;
user.receiveEmails = req.body.receiveEmails || user.receiveEmails;
user.citesegment = req.body.citesegment || user.citesegment;
user.citerole = req.body.citerole || user.citerole;
if(req.user.adminLDE)
user.adminLDE = req.body.adminLDE || user.adminLDE;
if(req.user.adminSimcaqNode)
user.adminSimcaqNode = req.body.adminSimcaqNode || user.adminSimcaqNode;
// console.log(user.checkPassword(req.body.password));
if ((req.body.password) && (req.body.newpassword)) {
if (req.body.password != req.body.newpassword) {
if (user.checkPassword(req.body.password)) {
user.password = req.body.newpassword;
} else {
res.statusCode = 400;
return res.json({error: {
message: 'A senha atual está incorreta'
}});
}
} else {
res.statusCode = 400;
return res.json({error: {
message: 'A nova senha é a mesma da senha atual'
}});
}
}
user.save(err => {
if(err) {
log.error(err);
res.statusCode = 400;
return res.json({error: {
message: 'Email já cadastrado'
}});
}
let u = user.toObject();
delete u.hashedPassword;
delete u.salt;
res.json({user: u});
})
req.checkBody("email","Campo email vazio").notEmpty();
req.checkBody("name","Campo nome vazio").notEmpty();
req.checkBody("nickname","Campo apelido vazio").notEmpty();
req.checkBody("cep","Campo cep vazio").notEmpty();
req.checkBody("complement","Campo complemento vazio");
req.checkBody("address","Campo endereço vazio");
req.checkBody("phone","Campo telefone vazio");
req.checkBody("schooling","Campo formação vazio").notEmpty();
req.checkBody("course","Campo course vazio");
req.checkBody("segment","Campo segmento vazio").notEmpty();
req.checkBody("role","Campo função vazio").notEmpty();
req.checkBody("institutionName","Campo nome da instituição vazio").notEmpty();
req.checkBody("state","Campo estado vazio").notEmpty();
req.checkBody("city","Campo cidade vazio").notEmpty();
req.checkBody("receiveEmails","Campo receber email vazio").notEmpty();
req.checkBody("citesegment","Campo citar segmento vazio");
req.checkBody("citerole","Campo citar função vazio");
req.checkBody("adminLDE","Campo admin LDE vazio").notEmpty();
req.checkBody("adminSimcaqNode","Campo email vazio").notEmpty();
req.getValidationResult().then(function(result) {
if(!result.isEmpty()) {
console.log(result.array());
res.statusCode = 400;
return res.json({errors: result.array()});
}
User.findById(req.params.id, (err, user) => {
if (err) {
log.error(err);
return next({err});
}
if(!user || user.origin != req.user.origin) {
res.statusCode = 404;
return next({err: {
message: 'Usuário não encontrado'
}});
}
console.log(req.body);
user.email = req.body.email;
user.name = req.body.name;
user.nickname = req.body.nickname ;
user.cep = req.body.cep;
user.complement = req.body.complement;
user.address = req.body.address;
user.phone = req.body.phone;
user.schooling = req.body.schooling;
user.course = req.body.course;
user.segment = req.body.segment ;
user.role = req.body.role;
user.institutionName = req.body.institutionName;
user.state = req.body.state;
user.city = req.body.city;
user.receiveEmails = req.body.receiveEmails;
user.citesegment = req.body.citesegment;
user.citerole = req.body.citerole;
if(req.user.adminLDE)
user.adminLDE = req.body.adminLDE;
if(req.user.adminSimcaqNode)
user.adminSimcaqNode = req.body.adminSimcaqNode;
// console.log(user.checkPassword(req.body.password));
if ((req.body.password) && (req.body.newpassword)) {
if (req.body.password != req.body.newpassword) {
if (user.checkPassword(req.body.password)) {
user.password = req.body.newpassword;
} else {
res.statusCode = 400;
return res.json({error: {
message: 'A senha atual está incorreta'
}});
}
} else {
res.statusCode = 400;
return res.json({error: {
message: 'A nova senha é a mesma da senha atual'
}});
}
}
user.save(err => {
if(err) {
log.error(err);
res.statusCode = 400;
return res.json({error: {
message: 'Email já cadastrado'
}});
}
let u = user.toObject();
delete u.hashedPassword;
delete u.salt;
res.json({user: u});
})
})
})
});
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment