conexion con sat
This commit is contained in:
@@ -1,79 +1,59 @@
|
||||
const { obtenerAlumno, obtenerProfesor } = require('../../config/mariadb.conf');
|
||||
const { obtenerAlumno, obtenerCarrera } = require('../../config/mariadb.conf');
|
||||
const validar = require('../../helper/validar');
|
||||
const dbPath = '../../db/tablas';
|
||||
const Carrera = require(`${dbPath}/Carrera`);
|
||||
const TipoUsuario = require(`${dbPath}/TipoUsuario`);
|
||||
const Usuario = require(`${dbPath}/Usuario`);
|
||||
|
||||
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 usuario = { Carrera: {} };
|
||||
let esAlumno = false;
|
||||
where: { numeroCuenta: numeroCuenta },
|
||||
}).then((res) => {
|
||||
if (!res) {
|
||||
let usuario = {};
|
||||
|
||||
return obtenerAlumno(numeroCuenta)
|
||||
.then(async (res) => {
|
||||
if (!res) return obtenerProfesor(numeroCuenta);
|
||||
esAlumno = true;
|
||||
return obtenerAlumno(numeroCuenta)
|
||||
.then(async (res) => {
|
||||
usuario = res;
|
||||
return res;
|
||||
})
|
||||
.then(async (res) => {
|
||||
return obtenerCarrera(res.id_carrera);
|
||||
})
|
||||
.then(async (res) => {
|
||||
if (!usuario) throw new Error('Este número de cuenta no existe.');
|
||||
if (usuario.vigente != 'si')
|
||||
throw new Error('Este alumno no esta inscrito este semestre.');
|
||||
usuario.nombre = usuario.nombre;
|
||||
usuario.carrera = res.carrera;
|
||||
|
||||
usuario.carrera = await Carrera.findOne({
|
||||
where: { idCarrera: usuario.id_carrera },
|
||||
}).then((res) => {
|
||||
if (!res)
|
||||
return Carrera.create({
|
||||
idCarrera: usuario.id_carrera,
|
||||
carrera: usuario.carrera,
|
||||
});
|
||||
return res;
|
||||
})
|
||||
.then(async (res) => {
|
||||
usuario = res;
|
||||
if (!usuario)
|
||||
throw new Error(
|
||||
'Este número de cuenta o de trabajador no existe.'
|
||||
);
|
||||
if (esAlumno) {
|
||||
if (usuario.inscrito != 'SI')
|
||||
throw new Error('Este alumno no esta inscrito este semestre.');
|
||||
await Carrera.findOne({
|
||||
where: { carrera: usuario.carrera },
|
||||
})
|
||||
.then((res) => {
|
||||
if (!res) return Carrera.create({ carrera: usuario.carrera });
|
||||
return res;
|
||||
})
|
||||
.then((res) => {
|
||||
usuario.Carrera = res;
|
||||
usuario.nombre = usuario.nombre.trim();
|
||||
usuario.idTipoUsuario = 4;
|
||||
});
|
||||
} else {
|
||||
usuario.idTipoUsuario = 3;
|
||||
usuario.nombre = `${usuario.ApPaterno.trim()} ${usuario.ApMaterno.trim()} ${usuario.Nombre.trim()}`;
|
||||
}
|
||||
return Usuario.create({
|
||||
usuario: numeroCuenta,
|
||||
nombre: usuario.nombre,
|
||||
idCarrera: usuario.Carrera ? usuario.Carrera.idCarrera : null,
|
||||
idTipoUsuario: usuario.idTipoUsuario,
|
||||
});
|
||||
})
|
||||
.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;
|
||||
});
|
||||
|
||||
return Usuario.create({
|
||||
numeroCuenta: numeroCuenta,
|
||||
password: 'hola',
|
||||
nombre: usuario.nombre,
|
||||
semestre: 8,
|
||||
idCarrera: usuario.id_carrera,
|
||||
});
|
||||
});
|
||||
}
|
||||
return Usuario.findOne({
|
||||
where: { numeroCuenta: numeroCuenta },
|
||||
include: [{ model: Carrera }],
|
||||
attributes: ['idUsuario', 'numeroCuenta', 'nombre'],
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = escolares;
|
||||
|
||||
Reference in New Issue
Block a user