Files
pcpuma_acatlan_api/server/controller/Usuario/escolares.js
T
2022-01-25 22:08:13 -06:00

69 lines
2.4 KiB
JavaScript

const { obtenerAlumno, obtenerProfesor } = 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 },
attributes: ['idUsuario', 'password'],
}).then((res) => {
if (!res) {
let usuario = { Carrera: null };
let esAlumno = false;
return obtenerAlumno(numeroCuenta)
.then(async (res) => {
if (!res) return obtenerProfesor(numeroCuenta);
esAlumno = true;
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.');
usuario.nombre = usuario.nombre.trim();
usuario.idTipoUsuario = 4;
usuario.Carrera = await Carrera.findOne({
where: { carrera: usuario.carrera },
}).then((res) => {
if (!res) return Carrera.create({ carrera: usuario.carrera });
return res;
});
} 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 }],
attributes: ['idUsuario', 'usuario', 'nombre'],
})
);
} else if (res.password)
throw new Error('Este número de cuenta ya fue registrado.');
return Usuario.findOne({
where: { usuario: numeroCuenta },
include: [{ model: Carrera }, { model: TipoUsuario }],
attributes: ['idUsuario', 'usuario', 'nombre'],
});
});
};
module.exports = escolares;