Files
pcpuma_acatlan_api/server/controller/Usuario/crear.js
T

40 lines
1.3 KiB
JavaScript
Raw Normal View History

const helperPath = '../../helper';
const gmail = require(`${helperPath}/gmail`);
const encriptar = require(`${helperPath}/encriptar`);
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-01-24 15:58:34 -06:00
let correo = '';
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-01-24 15:58:34 -06:00
// correo = `${res.usuario}@pcpuma.acatlan.unam.mx`;
correo = 'lemuelhelonmarquezrosas@gmail.com';
2021-05-20 13:50:31 -05:00
return Usuario.update(
{
password: encriptar.encriptar(password),
telefono,
},
{ where: { idUsuario } }
);
})
2022-01-24 15:58:34 -06:00
.then((res) => gmail('Contraseña', correo, password))
2021-05-20 13:50:31 -05:00
.then((res) => ({
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;