56 lines
1.7 KiB
JavaScript
56 lines
1.7 KiB
JavaScript
require('../../config/config');
|
|
const axios = require('axios');
|
|
const { validarNumeroCuenta } = require('../../helper/validar');
|
|
const dbPath = '../../db/tablas';
|
|
const Usuario = require(`${dbPath}/Usuario`);
|
|
const Carrera = require(`${dbPath}/Carrera`);
|
|
|
|
const escolares = async (body) => {
|
|
const numeroCuenta = validarNumeroCuenta(body.numeroCuenta);
|
|
let alumno;
|
|
|
|
return axios({
|
|
method: 'post',
|
|
url: `${process.env.ESCOLARES}${numeroCuenta}`,
|
|
data: `${process.env.ESCOLARES_PASS}`,
|
|
})
|
|
.then((res) => {
|
|
if (!res.data.nombre || !res.data.carrconst || !res.data.avance)
|
|
throw new Error(
|
|
'El alumno no cumple con los requisitos para realizar el Servicio Social. Si cree que esto es erroneo comunicate con COESI.'
|
|
);
|
|
alumno = {
|
|
nombre: res.data.nombre.trim(),
|
|
creditos: res.data.avance,
|
|
carrera: res.data.carrconst.trim(),
|
|
};
|
|
while (alumno.nombre.search('') != -1)
|
|
alumno.nombre = alumno.nombre.replace('', '');
|
|
while (alumno.nombre.search('Ã') != -1)
|
|
alumno.nombre = alumno.nombre.replace('Ã', 'Ñ');
|
|
|
|
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,
|
|
nombre: alumno.nombre,
|
|
idTipoUsuario: 3,
|
|
});
|
|
return res;
|
|
})
|
|
.then((res) => ({ ...alumno, idUsuario: res.idUsuario }));
|
|
};
|
|
|
|
module.exports = escolares;
|