diff --git a/server/config/mariadb.conf.js b/server/config/mariadb.conf.js index 737ddf0..10945e2 100644 --- a/server/config/mariadb.conf.js +++ b/server/config/mariadb.conf.js @@ -17,7 +17,8 @@ const obtenerAlumno = async (numeroCuenta) => { .query(`CALL busca_alumno_pcpuma('${numeroCuenta}')`) .then((rows) => { conn.end(); - return { rows, length: rows.length }; + if (rows[0].length === 0) return null; + return rows[0][0]; }) .catch((err) => { conn.end(); diff --git a/server/controller/Usuario/escolares.js b/server/controller/Usuario/escolares.js index a299d55..41eadfc 100644 --- a/server/controller/Usuario/escolares.js +++ b/server/controller/Usuario/escolares.js @@ -1,10 +1,44 @@ 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 obtenerAlumno(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: nombre, + idCarrera: res.idCarrera, + }) + ) + .then((res) => + Usuario.findOne({ + where: { usuario: numeroCuenta }, + include: [{ model: Carrera }], + }) + ) + .then((res) => (usuario = res)); + } + return usuario; + }); }; module.exports = escolares; diff --git a/server/db/install.js b/server/db/install.js index 391b2f0..48343b1 100644 --- a/server/db/install.js +++ b/server/db/install.js @@ -99,43 +99,6 @@ const dataTipoUsuario = async () => { } }; -const dataCarrera = async () => { - const data = [ - 'LIC. EN DERECHO', - 'LIC. EN ACTUARIA', - 'LIC. EN MATEMATICAS APLICADAS Y COMP.', - 'LIC. EN COMUNICACION', - 'LIC. EN ECONOMIA', - 'LIC. EN PERIODISMO Y COMUNICACION COL.', - 'LIC. EN RELACIONES INTERNACIONALES', - 'LIC. EN FILOSOFIA', - 'LIC. EN HISTORIA', - 'LIC. EN LENGUA Y LITERATURA HISPANICAS', - 'LIC. EN SOCIOLOGIA', - 'LIC. EN CIENCIAS POLITICAS Y ADMON PUB', - 'LIC. EN INGENIERIA CIVIL', - 'LIC. EN ENSEÑANZA DE INGLES', - 'LIC. EN DISEÑO GRAFICO', - 'LIC. EN PEDAGOGÍA', - 'LIC. EN DERECHO (SUA)', - 'LIC. ENSEÑANZA DE ESPAÑOL(LENG. EXTRANJ)', - 'LIC. ENSEÑANZA DE ITALIANO(LENG. EXTRANJ', - 'LIC. EN ARQUITECTURA', - 'LIC. EN RELACIONES INTERNACIONALES (SUA)', - 'LIC. ENSEÑANZA DE INGLÉS(LENG. EXTRANJE)', - 'LIC. ENSEÑANZA DE ALEMÁN (LENG. EXTRANJS', - 'LIC. EN MAT. APLICADAS Y COMPUTACION', - ]; - - console.log('\nPaso 4) Insertando data carrera.'.bold.blue); - for (let i = 0; i < data.length; i++) { - await Carrera.create({ - carrera: data[i], - }); - console.log(`Se agrego la carrera ${data[i]}.`.magenta); - } -}; - const dataModulo = async () => { const data = ['MAC', 'Derecho']; diff --git a/server/db/tablas/Usuario.js b/server/db/tablas/Usuario.js index 5f85b8d..60c1574 100644 --- a/server/db/tablas/Usuario.js +++ b/server/db/tablas/Usuario.js @@ -20,7 +20,8 @@ Usuario.init( }, password: { type: DataTypes.STRING(60), - allowNull: false, + allowNull: true, + defaultValue: null, }, nombre: { type: DataTypes.STRING(70), @@ -28,7 +29,8 @@ Usuario.init( }, telefono: { type: DataTypes.STRING(15), - allowNull: false, + allowNull: true, + defaultValue: null, }, multa: { type: DataTypes.BOOLEAN,