Conexión con escolares
This commit is contained in:
+2
-2
@@ -15,12 +15,12 @@ app.use(
|
||||
|
||||
app.use(bodyParser.json());
|
||||
|
||||
app.get('/', (req, res) => res.send('API Plantilla'));
|
||||
app.get('/', (req, res) => res.send('API de Inscripciones a CEDETEC'));
|
||||
|
||||
app.use(require('./routes/index'));
|
||||
|
||||
app.listen(Number(process.env.PORT), () =>
|
||||
console.log(
|
||||
`API Plantilla corriendo en el puerto: ${process.env.PORT}`.rainbow
|
||||
`API Inscripciones corriendo en el puerto: ${process.env.PORT}`.rainbow
|
||||
)
|
||||
);
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
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,
|
||||
});
|
||||
|
||||
const obtenerAlumno = async (numeroCuenta) => {
|
||||
const conn = await pool.getConnection().catch((err) => {
|
||||
throw new Error('No se pudo conectar con la db. ' + err);
|
||||
});
|
||||
|
||||
return conn
|
||||
.query(`CALL busca_alumno_pcpuma('${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;
|
||||
return rows[0];
|
||||
})
|
||||
.catch((err) => {
|
||||
conn.end();
|
||||
throw new Error('Hubo un problema con el querry.' + err);
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = { obtenerAlumno, obtenerProfesor };
|
||||
@@ -0,0 +1,79 @@
|
||||
const { obtenerAlumno, obtenerProfesor } = 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;
|
||||
|
||||
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.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;
|
||||
});
|
||||
};
|
||||
|
||||
module.exports = escolares;
|
||||
@@ -15,4 +15,24 @@ const route = '/usuario';
|
||||
// });
|
||||
// });
|
||||
|
||||
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 });
|
||||
});
|
||||
});
|
||||
|
||||
app.get(`${route}/escolares`, (req, res) => {
|
||||
return escolares(req.query)
|
||||
.then((data) => {
|
||||
res.status(200).json(data);
|
||||
})
|
||||
.catch((err) => {
|
||||
res.status(400).json({ message: err.message });
|
||||
});
|
||||
});
|
||||
|
||||
module.exports = app;
|
||||
@@ -1,6 +1,6 @@
|
||||
const express = require("express");
|
||||
const express = require('express');
|
||||
const app = express();
|
||||
|
||||
// app.use(require("./"));
|
||||
app.use(require('./Usuario'));
|
||||
|
||||
module.exports = app;
|
||||
|
||||
Reference in New Issue
Block a user