conexion con sat
This commit is contained in:
@@ -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 };
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -9,7 +9,6 @@ Carrera.init(
|
||||
primaryKey: true,
|
||||
allowNull: false,
|
||||
unique: true,
|
||||
autoIncrement: true,
|
||||
},
|
||||
carrera: {
|
||||
type: DataTypes.STRING(60),
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user