From 6983bc295d4b7e9fff985398fafbdeb44c8c1f98 Mon Sep 17 00:00:00 2001 From: Andres2908 Date: Wed, 26 Jan 2022 00:21:54 -0600 Subject: [PATCH] conexion con sat --- server/config/mariadb.conf.js | 59 +++++++------- server/controller/Usuario/escolares.js | 108 ++++++++++--------------- server/db/tablas/Carrera.js | 1 - server/routes/Usuario.js | 14 +--- 4 files changed, 73 insertions(+), 109 deletions(-) diff --git a/server/config/mariadb.conf.js b/server/config/mariadb.conf.js index b1d162e..3d8b474 100644 --- a/server/config/mariadb.conf.js +++ b/server/config/mariadb.conf.js @@ -2,17 +2,10 @@ require('./config'); const mariadb = require('mariadb'); const pool = mariadb.createPool({ - host: process.env.ESCOLARES_HOST, - user: process.env.ESCOLARES_USER, - password: process.env.ESCOLARES_PASSWORD, - database: process.env.ESCOLARES_DB, -}); - -const pool2 = mariadb.createPool({ - host: process.env.PROFESORES_HOST, - user: process.env.PROFESORES_USER, - password: process.env.PROFESORES_PASSWORD, - database: process.env.PROFESORES_DB, + host: process.env.SAT_HOST, + user: process.env.SAT_USER, + password: process.env.SAT_PASSWORD, + database: process.env.SAT_DB, }); const obtenerAlumno = async (numeroCuenta) => { @@ -21,28 +14,11 @@ const obtenerAlumno = async (numeroCuenta) => { }); return conn - .query(`CALL busca_alumno_pcpuma('${numeroCuenta}')`) + .query(`SELECT *FROM alumno WHERE id_cuenta = ${numeroCuenta}`) .then((rows) => { conn.end(); - if (rows[0].length === 0) return null; - return rows[0][0]; - }) - .catch((err) => { - conn.end(); - throw new Error('Hubo un problema con el querry.' + err); - }); -}; - -const obtenerProfesor = async (numeroCuenta) => { - const conn = await pool2.getConnection().catch((err) => { - throw new Error('No se pudo conectar con la db. ' + err); - }); - - return conn - .query(`SELECT * FROM Profesor WHERE NumTrabajador = ${numeroCuenta}`) - .then((rows) => { - conn.end(); - if (rows.length === 0) return null; + // console.log(rows[0]); + // if (rows[0].length === 0) return null; return rows[0]; }) .catch((err) => { @@ -51,4 +27,23 @@ const obtenerProfesor = async (numeroCuenta) => { }); }; -module.exports = { obtenerAlumno, obtenerProfesor }; +const obtenerCarrera = async (idCarrera) => { + const conn = await pool.getConnection().catch((err) => { + throw new Error('No se pudo conectar con la db. ' + err); + }); + + return conn + .query(`SELECT *FROM carrera WHERE id_carrera = ${idCarrera}`) + .then((rows) => { + conn.end(); + // console.log(rows[0]); + // if (rows[0].length === 0) return null; + return rows[0]; + }) + .catch((err) => { + conn.end(); + throw new Error('Hubo un problema con el querry.' + err); + }); +}; + +module.exports = { obtenerAlumno, obtenerCarrera }; diff --git a/server/controller/Usuario/escolares.js b/server/controller/Usuario/escolares.js index 06463c3..1990eaf 100644 --- a/server/controller/Usuario/escolares.js +++ b/server/controller/Usuario/escolares.js @@ -1,79 +1,59 @@ -const { obtenerAlumno, obtenerProfesor } = require('../../config/mariadb.conf'); +const { obtenerAlumno, obtenerCarrera } = 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 }, - include: [{ model: Carrera }, { model: TipoUsuario }], - }) - .then((res) => { - if (!res) { - let usuario = { Carrera: {} }; - let esAlumno = false; + where: { numeroCuenta: numeroCuenta }, + }).then((res) => { + if (!res) { + let usuario = {}; - return obtenerAlumno(numeroCuenta) - .then(async (res) => { - if (!res) return obtenerProfesor(numeroCuenta); - esAlumno = true; + return obtenerAlumno(numeroCuenta) + .then(async (res) => { + usuario = res; + return res; + }) + .then(async (res) => { + return obtenerCarrera(res.id_carrera); + }) + .then(async (res) => { + if (!usuario) throw new Error('Este número de cuenta no existe.'); + if (usuario.vigente != 'si') + throw new Error('Este alumno no esta inscrito este semestre.'); + usuario.nombre = usuario.nombre; + usuario.carrera = res.carrera; + + usuario.carrera = await Carrera.findOne({ + where: { idCarrera: usuario.id_carrera }, + }).then((res) => { + if (!res) + return Carrera.create({ + idCarrera: usuario.id_carrera, + carrera: usuario.carrera, + }); 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.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 }], - }) - ); - } 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; + }); + + return Usuario.create({ + numeroCuenta: numeroCuenta, + password: 'hola', + nombre: usuario.nombre, + semestre: 8, + idCarrera: usuario.id_carrera, + }); + }); + } + return Usuario.findOne({ + where: { numeroCuenta: numeroCuenta }, + include: [{ model: Carrera }], + attributes: ['idUsuario', 'numeroCuenta', 'nombre'], }); + }); }; module.exports = escolares; diff --git a/server/db/tablas/Carrera.js b/server/db/tablas/Carrera.js index 077ef34..ae59586 100644 --- a/server/db/tablas/Carrera.js +++ b/server/db/tablas/Carrera.js @@ -9,7 +9,6 @@ Carrera.init( primaryKey: true, allowNull: false, unique: true, - autoIncrement: true, }, carrera: { type: DataTypes.STRING(60), diff --git a/server/routes/Usuario.js b/server/routes/Usuario.js index 22dabba..d9cfb0f 100644 --- a/server/routes/Usuario.js +++ b/server/routes/Usuario.js @@ -2,18 +2,8 @@ const express = require('express'); const app = express(); const { verificaToken } = require('../middleware/autentificacion'); const route = '/usuario'; -// const controllerPath = '../controller/Usuario'; -// const login = require(`${controllerPath}/login`); - -// app.post(`${route}/login`, (req, res) => { -// return login(req.body) -// .then((data) => { -// res.status(200).json(data); -// }) -// .catch((err) => { -// res.status(400).json({ message: err.message }); -// }); -// }); +const controllerPath = '../controller/Usuario'; +const escolares = require(`${controllerPath}/escolares`); app.post(`${route}/login`, (req, res) => { return login(req.body)