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

69 lines
2.4 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 Carrera = require(`${dbPath}/Carrera`);
2021-05-20 01:01:55 -05:00
const TipoUsuario = require(`${dbPath}/TipoUsuario`);
2021-09-20 00:21:36 -05:00
const Usuario = require(`${dbPath}/Usuario`);
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 },
2022-01-20 22:01:14 -06:00
attributes: ['idUsuario', 'password'],
}).then((res) => {
if (!res) {
let usuario = { Carrera: null };
let esAlumno = false;
2021-05-19 02:34:45 -05:00
2022-01-20 22:01:14 -06:00
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;
2021-09-02 12:13:55 -05:00
});
2022-01-20 22:01:14 -06:00
} 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'],
2021-09-02 12:13:55 -05:00
})
2022-01-20 22:01:14 -06:00
);
} 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'],
2021-05-20 10:48:35 -05:00
});
2022-01-20 22:01:14 -06:00
});
2021-05-19 01:43:01 -05:00
};
module.exports = escolares;