Files
pcpuma_acatlan_api/server/controller/Usuario/crear.js
T
2022-02-05 21:22:51 -06:00

42 lines
1.4 KiB
JavaScript

const helperPath = '../../helper';
const correoPcPuma = require(`${helperPath}/correo`);
const encriptar = require(`${helperPath}/encriptar`);
const gmail = require(`${helperPath}/gmail`);
const validar = require(`${helperPath}/validar`);
const Usuario = require('../../db/tablas/Usuario');
const crear = async (body) => {
const idUsuario = validar.validarNumeroEntero(
body.idUsuario,
'id usuario',
true
);
const telefono = validar.validarNumero(body.telefono, 'teléfono', true, 15);
const password = encriptar.generarPassword();
let correo = {};
let email = '';
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.');
email = `${res.usuario}@pcpuma.acatlan.unam.mx`;
correo = correoPcPuma(password);
return Usuario.update(
{
password: encriptar.encriptar(password),
telefono,
},
{ where: { idUsuario } }
);
})
.then(() => gmail(correo.subject, email, correo.msj))
.then(() => ({
message:
'Se creó correctamente tu cuenta, ingresa a tu correo @pcpuma para consultar tu contraseña para este servicio.',
}));
};
module.exports = crear;