40 lines
1.3 KiB
JavaScript
40 lines
1.3 KiB
JavaScript
const helperPath = '../../helper';
|
|
const { validarNumeroEntero } = require(`${helperPath}/validar`);
|
|
const gmail = require(`${helperPath}/gmail`);
|
|
const correos = require(`${helperPath}/correos`);
|
|
const encriptar = require(`${helperPath}/encriptar`);
|
|
const dbPath = '../../db/tablas';
|
|
const Usuario = require(`${dbPath}/Usuario`);
|
|
const Servicio = require(`${dbPath}/Servicio`);
|
|
|
|
const newPasswordAlumno = async (body) => {
|
|
const idServicio = validarNumeroEntero(body.idServicio, 'id servicio');
|
|
const password = encriptar.generarPassword();
|
|
let idUsuario;
|
|
let correo = {};
|
|
|
|
return Servicio.findOne({
|
|
where: { idServicio },
|
|
include: [{ model: Usuario }],
|
|
})
|
|
.then((res) => {
|
|
if (!res) throw new Error('No existe este Servicio Social.');
|
|
if (res.Usuario.idTipoUsuario != 3)
|
|
throw new Error('Este usuario no es de tipo alumno.');
|
|
correo = correos.preRegistro(password, res.Usuario.nombre);
|
|
idUsuario = res.idUsuario;
|
|
return gmail(correo.subject, res.correo, 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 alumno.',
|
|
}));
|
|
};
|
|
|
|
module.exports = newPasswordAlumno;
|