reasignar programas

This commit is contained in:
xXpuma99Xx
2021-11-30 12:18:12 -06:00
parent b30b7ec57b
commit 3cfff170e8
2 changed files with 52 additions and 0 deletions
@@ -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;