Skip to content

Instantly share code, notes, and snippets.

@aguileraq
Created September 6, 2018 19:34
Show Gist options
  • Save aguileraq/44124520c803894726b57876a12847f3 to your computer and use it in GitHub Desktop.
Save aguileraq/44124520c803894726b57876a12847f3 to your computer and use it in GitHub Desktop.
exports.editarPost = async (req, res, next) => {
let data = req.body;
req.checkBody('respuesta', 'Campo obligatorio, compuesto de minimo 15 carácteres.').optional({ checkFalsy: true }).isLength({min:16});
let errors = req.validationErrors();
if(errors){
res.json({
ok:false,
errors
});
}else{
data.respuesta = req.sanitizeBody('respuesta').trim();
let obj = {};
if (data.respuesta) obj.respuesta = data.respuesta;
if (data.estatus) obj.estatus = data.estatus;
if(data.cupon){
let hoy = moment().format("DD-MM-YYYY");
let cupon = new Cupon({
creado: hoy,
admin: req.user._id,
});
cupon = data.cupon;
}
try{
if(cupon){
let newCupon = await cupon.save();
}
if(newCupon) obj.cupon = newCupon.id;
let result = await Incidencia.findByIdAndUpdate(data.id, obj, {new:true});
if(!result){
console.log(err);
res.status(404).json({
ok:false,
err
});
}else{
res.status(200).json({
ok: true,
title: 'Edición de Incidencia',
msg: 'Registro actualizado',
redirect: '/incidencias'
});
}
}
catch (err) {
if (err.name === 'MongoError' && err.code === 11000) {
return res.status(409).json({
err: 'Duplicate key' + err.message
});
}
return res.status(500).json({
err: err
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment