Files

33 lines
1.1 KiB
JavaScript
Raw Permalink Normal View History

2021-05-05 18:49:23 -05:00
const helperPath = '../../helper';
2022-01-02 15:07:49 -06:00
const { validarNumeroEntero } = require(`${helperPath}/validar`);
2021-05-05 18:49:23 -05:00
const gmail = require(`${helperPath}/gmail`);
const correos = require(`${helperPath}/correos`);
const encriptar = require(`${helperPath}/encriptar`);
2021-01-11 06:04:55 -06:00
const Usuario = require('../../db/tablas/Usuario');
const newPasswordResponsable = async (body) => {
2022-01-02 15:07:49 -06:00
const idUsuario = validarNumeroEntero(body.idUsuario, 'id usuario');
2021-01-14 03:13:37 -06:00
const password = encriptar.generarPassword();
let correo = {};
2021-01-11 06:04:55 -06:00
return Usuario.findOne({ where: { idUsuario } })
.then((res) => {
if (!res) throw new Error('No existe este Usuario.');
2021-01-12 21:09:37 -06:00
if (res.idTipoUsuario != 2)
2021-01-14 03:13:37 -06:00
throw new Error('Este usuario no es de tipo responsable.');
2021-05-13 01:23:47 -05:00
correo = correos.enviarSec(password, res.usuario, res.nombre);
2021-01-12 21:09:37 -06:00
return gmail(correo.subject, res.usuario, correo.msj);
2021-01-11 06:04:55 -06:00
})
2021-01-14 03:13:37 -06:00
.then((res) =>
Usuario.update(
2021-01-12 21:09:37 -06:00
{ password: encriptar.encriptar(password) },
2021-01-11 06:04:55 -06:00
{ where: { idUsuario } }
2021-01-14 03:13:37 -06:00
)
)
2021-01-14 16:02:57 -06:00
.then((res) => ({
message: 'Se envio un correo con una contraseña nueva al responsable.',
}));
2021-01-11 06:04:55 -06:00
};
module.exports = newPasswordResponsable;