Files
servicio_social_api/server/controller/Servicio/registro.js
T
2020-11-23 13:56:58 -06:00

62 lines
1.9 KiB
JavaScript

const validar = require('../../helper/validar');
const encriptar = require('../../helper/encriptar');
const gmail = require('../../helper/gmail');
const correos = require('../../helper/correos');
const Servicio = require('../../db/tablas/Servicio');
const Usuario = require('../../db/tablas/Usuario');
const registro = async (body) => {
let idServicio = validar.validarId(body.idServicio);
let password = '';
let correo = {};
let idUsuario;
return Servicio.findOne({
where: { idServicio },
include: [{ model: Usuario }],
})
.then((res) => {
if (!res) throw new Error('No existe este Servicio Social.');
switch (res.idStatus) {
case 1:
password = encriptar.generarPassword();
correo = correos.preRegistro(password, res.Usuario.nombre);
idUsuario = res.idUsuario;
return gmail(correo.subject, res.correo, correo.msj);
case 2:
throw new Error('Este Servicio Social ya se encuentra en Registro.');
case 3:
case 4:
case 5:
case 6:
throw new Error('Este Servicio Social ya paso por el Registro.');
case 7:
case 8:
case 9:
throw new Error(
'Este Servicio Social se encuentra rechazado. No se puede aceptar hasta que se corriga lo necesario.'
);
case 10:
throw new Error('Este Servicio Social esta cancelado.');
}
})
.then((res) => {
return Usuario.update(
{ password: encriptar.encriptar(password), activo: true },
{ where: { idUsuario } }
);
})
.then((res) => {
return Servicio.update({ idStatus: 2 }, { where: { idServicio } });
})
.then((res) => {
return {
message:
'Se cambio de estatus correctamente y se envio un correo al alumno con sus credenciales.',
};
});
};
module.exports = registro;