47 lines
1.4 KiB
JavaScript
47 lines
1.4 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 escolares = async (body) => {
|
|
const numeroCuenta = validar.validarNumeroCuenta(body.numeroCuenta);
|
|
|
|
return Usuario.findOne({
|
|
where: { usuario: numeroCuenta },
|
|
include: [{ model: Carrera }],
|
|
}).then(async (res) => {
|
|
let usuario = res;
|
|
|
|
if (!usuario) {
|
|
const alumno = await obtenerAlumno(numeroCuenta);
|
|
|
|
if (!alumno) throw new Error('Este alumno no existe o no esta inscrito.');
|
|
await Carrera.findOne({ where: { carrera: alumno.carrera } })
|
|
.then(async (res) => {
|
|
if (!res) return Carrera.create({ carrera: alumno.carrera });
|
|
return res;
|
|
})
|
|
.then((res) =>
|
|
Usuario.create({
|
|
usuario: alumno.cuenta,
|
|
nombre: alumno.nombre,
|
|
idCarrera: res.idCarrera,
|
|
idTipoUsuario: 4,
|
|
})
|
|
)
|
|
.then((res) =>
|
|
Usuario.findOne({
|
|
where: { usuario: numeroCuenta },
|
|
include: [{ model: Carrera }],
|
|
})
|
|
)
|
|
.then((res) => (usuario = res));
|
|
} else if (usuario.password)
|
|
throw new Error('Este número de cuenta ya fue registrado.');
|
|
return usuario;
|
|
});
|
|
};
|
|
|
|
module.exports = escolares;
|