Files
servicio_social_api/server/controller/Usuario/escolares.js
T

56 lines
1.7 KiB
JavaScript
Raw Normal View History

2020-11-16 09:58:55 -06:00
require('../../config/config');
const axios = require('axios');
2021-05-13 14:06:07 -05:00
const { validarNumeroCuenta } = require('../../helper/validar');
2021-05-05 18:49:23 -05:00
const dbPath = '../../db/tablas';
const Usuario = require(`${dbPath}/Usuario`);
const Carrera = require(`${dbPath}/Carrera`);
2020-11-16 09:58:55 -06:00
const escolares = async (body) => {
2021-05-13 14:06:07 -05:00
const numeroCuenta = validarNumeroCuenta(body.numeroCuenta);
2020-11-16 09:58:55 -06:00
let alumno;
2021-02-15 15:11:32 -06:00
return axios({
method: 'post',
url: `${process.env.ESCOLARES}${numeroCuenta}`,
data: `${process.env.ESCOLARES_PASS}`,
})
2020-11-16 09:58:55 -06:00
.then((res) => {
2021-02-15 15:11:32 -06:00
if (!res.data.nombre || !res.data.carrconst || !res.data.avance)
2020-11-16 09:58:55 -06:00
throw new Error(
2021-01-21 20:25:27 -06:00
'El alumno no cumple con los requisitos para realizar el Servicio Social. Si cree que esto es erroneo comunicate con COESI.'
2020-11-16 09:58:55 -06:00
);
alumno = {
2021-02-15 15:11:32 -06:00
nombre: res.data.nombre.trim(),
creditos: res.data.avance,
carrera: res.data.carrconst.trim(),
2020-11-16 09:58:55 -06:00
};
2021-05-05 18:49:23 -05:00
while (alumno.nombre.search('‘') != -1)
2021-04-17 10:20:11 -05:00
alumno.nombre = alumno.nombre.replace('‘', '');
2021-05-05 18:49:23 -05:00
while (alumno.nombre.search('Ã') != -1)
alumno.nombre = alumno.nombre.replace('Ã', 'Ñ');
2021-04-17 10:17:54 -05:00
2020-11-16 12:47:46 -06:00
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;
2020-11-16 09:58:55 -06:00
return Usuario.findOne({ where: { usuario: numeroCuenta } });
})
.then((res) => {
if (!res)
return Usuario.create({
usuario: numeroCuenta,
activo: false,
2020-11-17 17:27:12 -06:00
nombre: alumno.nombre,
2020-11-16 09:58:55 -06:00
idTipoUsuario: 3,
});
return res;
})
2021-01-14 16:02:57 -06:00
.then((res) => ({ ...alumno, idUsuario: res.idUsuario }));
2020-11-16 09:58:55 -06:00
};
module.exports = escolares;