2021-05-20 14:32:48 -05:00
|
|
|
const helperPath = '../../helper';
|
2022-02-05 21:22:51 -06:00
|
|
|
const correoPcPuma = require(`${helperPath}/correo`);
|
2021-05-20 14:32:48 -05:00
|
|
|
const encriptar = require(`${helperPath}/encriptar`);
|
2022-02-05 21:22:51 -06:00
|
|
|
const gmail = require(`${helperPath}/gmail`);
|
2021-05-20 14:32:48 -05:00
|
|
|
const validar = require(`${helperPath}/validar`);
|
2021-04-27 23:11:09 -05:00
|
|
|
const Usuario = require('../../db/tablas/Usuario');
|
|
|
|
|
|
2021-05-20 13:50:31 -05:00
|
|
|
const crear = async (body) => {
|
2022-01-03 16:47:25 -06:00
|
|
|
const idUsuario = validar.validarNumeroEntero(
|
|
|
|
|
body.idUsuario,
|
|
|
|
|
'id usuario',
|
|
|
|
|
true
|
|
|
|
|
);
|
2022-01-03 17:05:29 -06:00
|
|
|
const telefono = validar.validarNumero(body.telefono, 'teléfono', true, 15);
|
2021-05-20 13:50:31 -05:00
|
|
|
const password = encriptar.generarPassword();
|
2022-02-05 21:22:51 -06:00
|
|
|
let correo = {};
|
|
|
|
|
let email = '';
|
2021-05-20 13:50:31 -05:00
|
|
|
|
|
|
|
|
return Usuario.findOne({ where: { idUsuario } })
|
|
|
|
|
.then((res) => {
|
|
|
|
|
if (!res) throw new Error('No existe este usuario.');
|
|
|
|
|
if (res.password) throw new Error('Este usuario ya fue registrado.');
|
|
|
|
|
if (!res.activo) throw new Error('Este usuario no esta activo.');
|
2022-02-05 21:22:51 -06:00
|
|
|
email = `${res.usuario}@pcpuma.acatlan.unam.mx`;
|
|
|
|
|
correo = correoPcPuma(password);
|
2021-05-20 13:50:31 -05:00
|
|
|
return Usuario.update(
|
|
|
|
|
{
|
|
|
|
|
password: encriptar.encriptar(password),
|
|
|
|
|
telefono,
|
|
|
|
|
},
|
|
|
|
|
{ where: { idUsuario } }
|
|
|
|
|
);
|
|
|
|
|
})
|
2022-02-05 21:22:51 -06:00
|
|
|
.then(() => gmail(correo.subject, email, correo.msj))
|
|
|
|
|
.then(() => ({
|
2021-05-20 13:50:31 -05:00
|
|
|
message:
|
2021-09-17 16:57:38 -05:00
|
|
|
'Se creó correctamente tu cuenta, ingresa a tu correo @pcpuma para consultar tu contraseña para este servicio.',
|
2021-05-20 13:50:31 -05:00
|
|
|
}));
|
|
|
|
|
};
|
2021-04-27 23:11:09 -05:00
|
|
|
|
2021-04-28 00:21:24 -05:00
|
|
|
module.exports = crear;
|