const helperPath = '../../helper'; const { validarNumeroEntero } = require(`${helperPath}/validar`); const gmail = require(`${helperPath}/gmail`); const correos = require(`${helperPath}/correos`); const encriptar = require(`${helperPath}/encriptar`); const Usuario = require('../../db/tablas/Usuario'); const newPasswordResponsable = async (body) => { const idUsuario = validarNumeroEntero(body.idUsuario, 'id usuario'); const password = encriptar.generarPassword(); let correo = {}; return Usuario.findOne({ where: { idUsuario } }) .then((res) => { if (!res) throw new Error('No existe este Usuario.'); if (res.idTipoUsuario != 2) throw new Error('Este usuario no es de tipo responsable.'); correo = correos.enviarSec(password, res.usuario, res.nombre); return gmail(correo.subject, res.usuario, correo.msj); }) .then((res) => Usuario.update( { password: encriptar.encriptar(password) }, { where: { idUsuario } } ) ) .then((res) => ({ message: 'Se envio un correo con una contraseƱa nueva al responsable.', })); }; module.exports = newPasswordResponsable;