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

50 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-11-16 09:58:55 -06:00
require('../../config/config');
2020-11-17 17:27:12 -06:00
const validar = require('../../helper/validar');
2020-11-16 09:58:55 -06:00
const axios = require('axios');
2020-11-16 12:47:46 -06:00
const Usuario = require('../../db/tablas/Usuario');
const Carrera = require('../../db/tablas/Carrera');
2020-11-16 09:58:55 -06:00
const escolares = async (body) => {
2021-01-14 16:02:57 -06:00
const numeroCuenta = validar.validarNumeroCuenta(body.numeroCuenta);
2020-11-16 09:58:55 -06:00
let alumno;
return axios({
method: 'post',
url: `${process.env.ESCOLARES}${numeroCuenta}`,
data: `${process.env.ESCOLARES_PASS}`,
})
.then((res) => {
2021-01-14 16:02:57 -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-01-14 16:02:57 -06:00
nombre: res.data.nombre.trim(),
creditos: res.data.avance,
carrera: res.data.carrconst.trim(),
2020-11-16 09:58:55 -06: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;