update responsable
This commit is contained in:
@@ -0,0 +1,50 @@
|
||||
const validar = require('../../helper/validar');
|
||||
const Usuario = require('../../db/tablas/Usuario');
|
||||
|
||||
const responsableUpdate = async (body) => {
|
||||
let isObjectEmpty = (obj) => {
|
||||
for (let key in obj) {
|
||||
if (obj.hasOwnProperty(key)) return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
let idUsuario = validar.validarId(body.idUsuario);
|
||||
let dataUpdate = {};
|
||||
|
||||
return Usuario.findOne({ where: { idUsuario } })
|
||||
.then(async (res) => {
|
||||
if (!res)
|
||||
throw new Error('No existe este responsable en la base de datos.');
|
||||
if (res.idTipoUsuario !== 2)
|
||||
throw new Error('No es un usuaior de tipo responasble.');
|
||||
if (body.correo) {
|
||||
let yaUsado = await Usuario.findOne({
|
||||
where: { usuario: body.correo },
|
||||
}).then((res) => res);
|
||||
|
||||
if (body.correo === res.usuario)
|
||||
throw new Error('El responable ya tiene ese correo asignado.');
|
||||
if (yaUsado)
|
||||
throw new Error(
|
||||
'No se puede asignar este correo a esta cuenta porque esta siendo ocupado por otro resposanble.'
|
||||
);
|
||||
dataUpdate.usuario = validar.validarCorreo(body.correo);
|
||||
}
|
||||
if (body.nombre) {
|
||||
if (body.nombre === res.nombre)
|
||||
throw new Error('El responable ya tiene ese nombre asignado.');
|
||||
dataUpdate.nombre = validar.validarTexto(body.nombre, 'el nombre', 70);
|
||||
}
|
||||
if (isObjectEmpty(dataUpdate))
|
||||
throw new Error('No se a envio nada para actualizar.');
|
||||
return Usuario.update(dataUpdate, { where: { idUsuario } });
|
||||
})
|
||||
.then((res) => {
|
||||
return {
|
||||
message:
|
||||
'Se actualizó la información de este responsable de programas correctamente.',
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = responsableUpdate;
|
||||
|
||||
@@ -5,6 +5,7 @@ const route = '/usuario';
|
||||
const escolares = require('../controller/Usuario/escolares');
|
||||
const login = require('../controller/Usuario/login');
|
||||
const responsable = require('../controller/Usuario/responsable');
|
||||
const responsableUpdate = require('../controller/Usuario/responsableUpdate');
|
||||
|
||||
app.post(`${route}/login`, (req, res) => {
|
||||
return login(req.body)
|
||||
@@ -36,4 +37,14 @@ app.get(`${route}/responsable`, verificaToken, (req, res) => {
|
||||
});
|
||||
});
|
||||
|
||||
app.put(`${route}/responsable/update` /*, verificaToken*/, (req, res) => {
|
||||
return responsableUpdate(req.body)
|
||||
.then((data) => {
|
||||
res.status(200).json(data);
|
||||
})
|
||||
.catch((err) => {
|
||||
res.status(400).json({ message: err.message });
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = app;
|
||||
|
||||
Reference in New Issue
Block a user