Files

91 lines
3.6 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 creditosCarreras = [
{ carrera: 'LIC. EN ACTUARIA', creditos: 64 },
{ carrera: 'LIC. EN ARQUITECTURA', creditos: 70 },
{ carrera: 'LIC. EN CIENCIAS POLITICAS Y ADMON PUB', creditos: 67 },
{ carrera: 'LIC. EN CIENCIAS POLITICAS Y ADMON.PUBL.', creditos: 67 },
{ carrera: 'LIC. EN COMUNICACION', creditos: 67 },
{ carrera: 'LIC. EN DERECHO', creditos: 68 },
{ carrera: 'LIC. EN DERECHO (SUA)', creditos: 67 },
{ carrera: 'LIC. EN DISEÑO GRAFICO', creditos: 70 },
{ carrera: 'LIC. EN ECONOMIA', creditos: 66 },
{ carrera: 'LIC. EN ENSEÑANZA DE INGLES', creditos: 69 },
{ carrera: 'LIC. EN FILOSOFIA', creditos: 70 },
{ carrera: 'LIC. EN HISTORIA', creditos: 70 },
{ carrera: 'LIC. EN INGENIERIA CIVIL', creditos: 70 },
{ carrera: 'LIC. EN LENGUA Y LITERATURA HISPANICAS', creditos: 70 },
{ carrera: 'LIC. EN MAT. APLICADAS Y COMPUTACION', creditos: 66 },
{ carrera: 'LIC. EN MATEMATICAS APLICADAS Y COMP.', creditos: 66 },
{ carrera: 'LIC. EN PEDAGOGÍA', creditos: 70 },
{ carrera: 'LIC. EN PERIODISMO Y COMUNICACION COL.', creditos: 70 },
{ carrera: 'LIC. EN RELACIONES INTERNACIONALES', creditos: 70 },
{ carrera: 'LIC. EN RELACIONES INTERNACIONALES (SUA)', creditos: 70 },
{ carrera: 'LIC. EN SOCIOLOGIA', creditos: 68 },
{ carrera: 'LIC. ENSEÑANZA DE ALEMÁN (LENG. EXTRANJS', creditos: 70 },
{ carrera: 'LIC. ENSEÑANZA DE ESPAÑOL(LENG. EXTRANJ)', creditos: 70 },
{ carrera: 'LIC. ENSEÑANZA DE INGLÉS(LENG. EXTRANJE)', creditos: 70 },
{ carrera: 'LIC. ENSEÑANZA DE ITALIANO(LENG. EXTRANJ', creditos: 70 },
];
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 erróneo comunícate al Departamento de Servicio Social y Bolsa de Trabajo.'
);
alumno = {
nombre: res.data.nombre.trim(),
creditos: res.data.avance,
carrera: res.data.carrconst.trim(),
};
for (let i = 0; i < creditosCarreras.length; i++)
if (
alumno.carrera === creditosCarreras[i].carrera &&
Number(alumno.creditos) < creditosCarreras[i].creditos
)
throw new Error('Este alumno no cuenta con los créditos necesarios.');
while (
alumno.nombre.search('‘') != -1 &&
alumno.nombre.search('Ã') != -1
) {
alumno.nombre = alumno.nombre.replace('Ã', 'Ñ');
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;