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

80 lines
2.7 KiB
JavaScript
Raw Normal View History

2021-09-02 12:13:55 -05:00
const { obtenerAlumno, obtenerProfesor } = require('../../config/mariadb.conf');
2021-05-19 01:43:01 -05:00
const validar = require('../../helper/validar');
2021-05-19 02:34:45 -05:00
const dbPath = '../../db/tablas';
const Usuario = require(`${dbPath}/Usuario`);
const Carrera = require(`${dbPath}/Carrera`);
2021-05-20 01:01:55 -05:00
const TipoUsuario = require(`${dbPath}/TipoUsuario`);
2021-05-19 01:43:01 -05:00
const escolares = async (body) => {
2021-05-19 01:47:25 -05:00
const numeroCuenta = validar.validarNumeroCuenta(body.numeroCuenta);
2021-05-19 01:43:01 -05:00
2021-05-19 02:34:45 -05:00
return Usuario.findOne({
where: { usuario: numeroCuenta },
2021-05-20 01:01:55 -05:00
include: [{ model: Carrera }, { model: TipoUsuario }],
2021-05-20 10:48:35 -05:00
})
.then((res) => {
if (!res) {
2021-09-02 12:22:23 -05:00
let usuario = { Carrera: {} };
2021-09-02 12:13:55 -05:00
let esAlumno = false;
2021-05-19 02:34:45 -05:00
2021-05-20 10:48:35 -05:00
return obtenerAlumno(numeroCuenta)
.then(async (res) => {
2021-09-02 12:13:55 -05:00
if (!res) return obtenerProfesor(numeroCuenta);
esAlumno = true;
2021-05-20 10:48:35 -05:00
return res;
2021-05-19 02:34:45 -05:00
})
2021-09-02 12:13:55 -05:00
.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.a_paterno.trim()} ${usuario.a_materno.trim()} ${usuario.nombre.trim()}`;
}
return Usuario.create({
2021-05-20 10:48:35 -05:00
usuario: numeroCuenta,
2021-09-02 12:13:55 -05:00
nombre: usuario.nombre,
2021-09-02 12:24:08 -05:00
idCarrera: usuario.Carrera ? usuario.Carrera.idCarrera : null,
2021-09-02 12:13:55 -05:00
idTipoUsuario: usuario.idTipoUsuario,
});
})
2021-05-20 10:48:35 -05:00
.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) => {
2021-05-20 11:20:24 -05:00
delete res.dataValues.password;
delete res.dataValues.telefono;
delete res.dataValues.telefono;
delete res.dataValues.multa;
delete res.dataValues.idTipoUsuario;
2021-05-20 13:50:31 -05:00
delete res.dataValues.idCarrera;
2021-05-20 10:48:35 -05:00
return res;
});
2021-05-19 01:43:01 -05:00
};
module.exports = escolares;