53 lines
1.5 KiB
JavaScript
53 lines
1.5 KiB
JavaScript
const validar = require('../../helper/validar');
|
|
require('../../config/config');
|
|
const axios = require('axios');
|
|
const Usuario = require('../../db/tablas/Usuario');
|
|
const Carrera = require('../../db/tablas/Carrera');
|
|
|
|
const escolares = async (body) => {
|
|
let numeroCuenta = validar.validarNumeroCuenta(body.numeroCuenta);
|
|
let alumno;
|
|
|
|
return axios({
|
|
method: 'post',
|
|
url: `${process.env.ESCOLARES}${numeroCuenta}`,
|
|
data: `${process.env.ESCOLARES_PASS}`,
|
|
})
|
|
.then((res) => {
|
|
let data = res.data;
|
|
|
|
if (!data.nombre || !data.carrconst || !data.avance)
|
|
throw new Error(
|
|
'El alumno no cumple con los requicitos para realizar el Servicio Social. Si cree que esto es erroneo comunicate con COESI.'
|
|
);
|
|
alumno = {
|
|
nombre: data.nombre.trim(),
|
|
creditos: data.avance,
|
|
carrera: data.carrconst.trim(),
|
|
};
|
|
return Carrera.findOne({ where: { carrera: alumno.carrera } });
|
|
})
|
|
.then((res) => {
|
|
if (!res) return Carrera.create({ carrera: alumno.carrera });
|
|
return res;
|
|
})
|
|
.then((res) => {
|
|
alumno.idCarrera = res.idCarrera;
|
|
return Usuario.findOne({ where: { usuario: numeroCuenta } });
|
|
})
|
|
.then((res) => {
|
|
if (!res)
|
|
return Usuario.create({
|
|
usuario: numeroCuenta,
|
|
activo: false,
|
|
idTipoUsuario: 3,
|
|
});
|
|
return res;
|
|
})
|
|
.then((res) => {
|
|
return { ...alumno, idUsuario: res.idUsuario };
|
|
});
|
|
};
|
|
|
|
module.exports = escolares;
|