const { obtenerAlumno, obtenerProfesor } = require('../../config/mariadb.conf'); const validar = require('../../helper/validar'); const dbPath = '../../db/tablas'; const Usuario = require(`${dbPath}/Usuario`); const Carrera = require(`${dbPath}/Carrera`); const TipoUsuario = require(`${dbPath}/TipoUsuario`); 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; 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.'); 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({ 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; }); }; module.exports = escolares;