reasignar programas
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
const { validarId, validarCorreo } = require('../../helper/validar');
|
||||
const dbPath = '../../db/tablas';
|
||||
const Programa = require(`${dbPath}/Programa`);
|
||||
const Usuario = require(`${dbPath}/Usuario`);
|
||||
|
||||
const reasignarProgramas = async (body) => {
|
||||
const idUsuario = validarId(body.idUsuario);
|
||||
const correoOtroResponsable = validarCorreo(body.correoOtroResponsable);
|
||||
let otorResponsable = {};
|
||||
|
||||
return Usuario.findOne({
|
||||
where: { idUsuario },
|
||||
})
|
||||
.then((res) => {
|
||||
if (!res) throw new Error('No existe este usuario.');
|
||||
if (res.idTipoUsuario != 2)
|
||||
throw new Error('No es un usuario de tipo responsable');
|
||||
if (res.usuario === correoOtroResponsable)
|
||||
throw new Error('Son el mismo usuario.');
|
||||
return Usuario.findOne({ where: { usuario: correoOtroResponsable } });
|
||||
})
|
||||
.then((res) => {
|
||||
if (!res) throw new Error('No existe este usuario.');
|
||||
otorResponsable = res;
|
||||
return Programa.update(
|
||||
{ idUsuario },
|
||||
{
|
||||
where: { idUsuario: otorResponsable.idUsuario },
|
||||
}
|
||||
);
|
||||
})
|
||||
.then((res) =>
|
||||
Usuario.destroy({ where: { idUsuario: otorResponsable.idUsuario } })
|
||||
)
|
||||
.then((res) => ({
|
||||
message: `Se elimino correctamente al usuario ${correoOtroResponsable} y se reasigno correctamente sus programas.`,
|
||||
}));
|
||||
};
|
||||
|
||||
module.exports = reasignarProgramas;
|
||||
Reference in New Issue
Block a user