62 lines
2.0 KiB
JavaScript
62 lines
2.0 KiB
JavaScript
const obtenerAlumno = require('../../config/mariadb.conf');
|
|
const validar = require('../../helper/validar');
|
|
const dbPath = '../../db/tablas';
|
|
const Usuario = require(`${dbPath}/Usuario`);
|
|
const Carrera = require(`${dbPath}/Carrera`);
|
|
const TipoUsuario = require(`${dbPath}/TipoUsuario`);
|
|
|
|
const escolares = async (body) => {
|
|
const numeroCuenta = validar.validarNumeroCuenta(body.numeroCuenta);
|
|
|
|
return Usuario.findOne({
|
|
where: { usuario: numeroCuenta },
|
|
include: [{ model: Carrera }, { model: TipoUsuario }],
|
|
})
|
|
.then((res) => {
|
|
if (!res) {
|
|
let alumno = {};
|
|
|
|
return obtenerAlumno(numeroCuenta)
|
|
.then(async (res) => {
|
|
alumno = res;
|
|
if (!alumno)
|
|
throw new Error('Este alumno no existe o no esta inscrito.');
|
|
if (alumno.inscrito != 'SI')
|
|
throw new Error('Este alumno no esta inscrito este semestre.');
|
|
return Carrera.findOne({ where: { carrera: alumno.carrera } });
|
|
})
|
|
.then((res) => {
|
|
if (!res) return Carrera.create({ carrera: alumno.carrera });
|
|
return res;
|
|
})
|
|
.then((res) =>
|
|
Usuario.create({
|
|
usuario: numeroCuenta,
|
|
nombre: alumno.nombre.trim(),
|
|
idCarrera: res.idCarrera,
|
|
idTipoUsuario: 4,
|
|
})
|
|
)
|
|
.then((res) =>
|
|
Usuario.findOne({
|
|
where: { usuario: numeroCuenta },
|
|
include: [{ model: Carrera }, { model: TipoUsuario }],
|
|
})
|
|
);
|
|
} else if (res.password)
|
|
throw new Error('Este número de cuenta ya fue registrado.');
|
|
return res;
|
|
})
|
|
.then((res) => {
|
|
delete res.dataValues.password;
|
|
delete res.dataValues.telefono;
|
|
delete res.dataValues.telefono;
|
|
delete res.dataValues.multa;
|
|
delete res.dataValues.idTipoUsuario;
|
|
delete res.dataValues.idCarrera;
|
|
return res;
|
|
});
|
|
};
|
|
|
|
module.exports = escolares;
|