Files

65 lines
2.0 KiB
JavaScript
Raw Permalink Normal View History

2021-05-05 18:49:23 -05:00
const helperPath = '../../helper';
2021-05-13 14:06:07 -05:00
const correos = require(`${helperPath}/correos`);
2021-05-05 18:49:23 -05:00
const encriptar = require(`${helperPath}/encriptar`);
const gmail = require(`${helperPath}/gmail`);
2021-05-13 14:06:07 -05:00
const validar = require(`${helperPath}/validar`);
2021-05-05 18:49:23 -05:00
const dbPath = '../../db/tablas';
const Servicio = require(`${dbPath}/Servicio`);
const Usuario = require(`${dbPath}/Usuario`);
2020-11-22 20:36:55 -06:00
const registro = async (body) => {
2022-01-02 15:07:49 -06:00
const idServicio = validar.validarNumeroEntero(
body.idServicio,
'id servicio'
);
2021-01-14 03:13:37 -06:00
const password = encriptar.generarPassword();
2020-11-23 12:43:33 -06:00
let correo = {};
2020-11-23 13:56:58 -06:00
let idUsuario;
2020-11-22 20:36:55 -06:00
return Servicio.findOne({
where: { idServicio },
include: [{ model: Usuario }],
})
2020-11-23 12:43:33 -06:00
.then((res) => {
if (!res) throw new Error('No existe este Servicio Social.');
2020-11-22 20:36:55 -06:00
switch (res.idStatus) {
case 1:
2020-11-23 12:43:33 -06:00
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:
throw new Error('Este Servicio Social ya paso por el Registro.');
case 6:
throw new Error('Este Servicio Social ya finalizó.');
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 fue cancelado.');
default:
throw new Error('Id status no valido.');
}
2020-11-22 20:36:55 -06:00
})
2020-11-23 12:43:33 -06:00
.then((res) => {
return Usuario.update(
2020-11-23 12:56:24 -06:00
{ password: encriptar.encriptar(password), activo: true },
2020-11-23 12:43:33 -06:00
{ where: { idUsuario } }
);
})
2021-01-13 12:00:11 -06:00
.then((res) => Servicio.update({ idStatus: 2 }, { where: { idServicio } }))
2021-01-14 16:02:57 -06:00
.then((res) => ({
message:
'Se cambio de estatus correctamente y se envio un correo al alumno con sus credenciales.',
}));
2020-11-22 20:36:55 -06:00
};
module.exports = registro;