247 lines
7.6 KiB
JavaScript
247 lines
7.6 KiB
JavaScript
import express from 'express';
|
|
import bodyParser from 'body-parser';
|
|
import cors from 'cors';
|
|
import DAO from './Controller/DAO.js';
|
|
import DAOTutor from './Controller/DAOTutor.js';
|
|
import DAOCarreras from './Controller/DAOCarrera.js';
|
|
import Usuario from './Model/Usuario.js';
|
|
import multer from 'multer';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
import DAODocumento from './Controller/DAODocumento.js';
|
|
import fs from 'fs';
|
|
import logger from './utils/logger.js';
|
|
import EmailCtrl from './utils/mailCtrl.js'
|
|
|
|
const app = express();
|
|
const port = 3000;
|
|
|
|
// Obtener el directorio actual
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
const diskStorage = multer.diskStorage({
|
|
destination: path.join(__dirname, '/Documentos'),
|
|
filename: (req, file, cb) => {
|
|
cb(null, file.originalname);
|
|
}
|
|
});
|
|
|
|
const fileUpload = multer({
|
|
storage: diskStorage
|
|
}).single('archivo');
|
|
|
|
app.use(bodyParser.json());
|
|
|
|
// Configuración de CORS
|
|
app.use(cors({
|
|
origin: 'http://localhost:5173',
|
|
methods: ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
|
|
allowedHeaders: ['Origin', 'X-Requested-With', 'Content-Type', 'Accept'],
|
|
credentials: true
|
|
}));
|
|
app.use(express.static(path.join(__dirname, 'Documentos')));
|
|
|
|
// Rutas
|
|
app.get("/matriculas", async (req, res) => {
|
|
try {
|
|
const alumnos = await DAO.matriculas();
|
|
res.json(alumnos);
|
|
} catch (error) {
|
|
logger.error(`Error en /matriculas: ${error.message}`);
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
app.post("/agregarAlumno", async (req, res) => {
|
|
try {
|
|
const usuario = req.body;
|
|
usuario.rol = "estudiante";
|
|
const msj = await DAO.agregarAlumno(usuario);
|
|
res.json(msj);
|
|
} catch (error) {
|
|
logger.error(`Error en /agregarAlumno: ${error.message}`);
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
app.put("/editarUsuario", async (req, res) => {
|
|
try {
|
|
const usuario = req.body;
|
|
const verificado = await DAO.editarAlumno(usuario);
|
|
res.json({ Editado: verificado });
|
|
} catch (error) {
|
|
logger.error(`Error en /editarUsuario: ${error.message}`);
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
app.put("/editarTutor", async (req, res) => {
|
|
try {
|
|
const tutor = req.body;
|
|
const verificado = await DAOTutor.editarTutor(tutor);
|
|
res.json({ Editado: verificado });
|
|
} catch (error) {
|
|
logger.error(`Error en /editarTutor: ${error.message}`);
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
app.post("/agregarTutor", async (req, res) => {
|
|
try {
|
|
const tutor = req.body;
|
|
const agregado = await DAOTutor.agregarTutor(tutor);
|
|
res.json({ msj: agregado });
|
|
} catch (error) {
|
|
logger.error(`Error en /agregarTutor: ${error.message}`);
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
app.post("/alumnoIniciado", async (req, res) => {
|
|
try {
|
|
const alumno = req.body;
|
|
const matricula = alumno.matricula;
|
|
if (!alumno.matricula || !alumno.contrasena) {
|
|
return res.status(400).json({ error: "Missing matricula or contrasena" });
|
|
}
|
|
const usuario = await DAO.alumnoIniciado(alumno.matricula, alumno.contrasena);
|
|
const user = new Usuario();
|
|
if (usuario) {
|
|
const authToken = user.crearToken();
|
|
const authRol = usuario.getRol();
|
|
const authId = usuario.getId();
|
|
const message = `Bienvenido ${usuario.getNombre()}`;
|
|
res.json({ matricula, authToken, authRol, authId, message });
|
|
} else {
|
|
res.status(401).json({ error: "Credenciales inválidas" });
|
|
}
|
|
} catch (error) {
|
|
logger.error(`Error en /alumnoIniciado: ${error.message}`);
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
app.post("/usuarioValido", async (req, res) => {
|
|
try {
|
|
const usuario = req.body;
|
|
const verificado = await DAO.validarAlumno(usuario);
|
|
res.json({ existe: verificado });
|
|
} catch (error) {
|
|
logger.error(`Error en /usuarioValido: ${error.message}`);
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
app.post("/traerDatosAlumno", async (req, res) => {
|
|
try {
|
|
const usuario = req.body;
|
|
const datos = await DAO.traeUsuario(usuario.id);
|
|
res.json(datos);
|
|
} catch (error) {
|
|
logger.error(`Error en /traerDatosAlumno: ${error.message}`);
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
app.get("/carreras", async (req, res) => {
|
|
try {
|
|
const carreras = await DAOCarreras.dameCarreras();
|
|
res.json(carreras);
|
|
} catch (error) {
|
|
logger.error(`Error en /carreras: ${error.message}`);
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
app.post("/traerDatosTutor", async (req, res) => {
|
|
try {
|
|
const usuario = req.body;
|
|
const datos = await DAOTutor.traerTutor(usuario.id);
|
|
res.json(datos);
|
|
} catch (error) {
|
|
logger.error(`Error en /traerDatosTutor: ${error.message}`);
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
app.post("/agregarDocumento", fileUpload, async (req, res) => {
|
|
try {
|
|
if (!req.file) {
|
|
return res.status(400).json({ error: "No se ha cargado ningún archivo" });
|
|
}
|
|
const data = req.body;
|
|
const filePath = path.join(__dirname, 'Documentos', req.file.filename);
|
|
const file = fs.readFileSync(filePath);
|
|
|
|
const guardado = await DAODocumento.agregarDocumento(data, file);
|
|
|
|
if (guardado) {
|
|
const cambiar = await DAO.editarAlumnoInscrito(data, 1);
|
|
if (cambiar) {
|
|
res.json({ message: true });
|
|
} else {
|
|
res.json({ message: false });
|
|
}
|
|
} else {
|
|
res.json({ message: false });
|
|
}
|
|
} catch (error) {
|
|
logger.error(`Error en /agregarDocumento: ${error.message}`);
|
|
res.status(500).json({ error: error.message });
|
|
logger.error(error);
|
|
}
|
|
});
|
|
|
|
app.use((err, req, res, next) => {
|
|
logger.error(err.stack);
|
|
res.status(500).send('Something broke!');
|
|
});
|
|
|
|
app.listen(port, () => {
|
|
console.info(`App listening at http://localhost:${port}`);
|
|
});
|
|
|
|
app.post("/traerTodosDatosAlumno", async (req, res) => {
|
|
try {
|
|
const usuario = req.body;
|
|
const datos = await DAO.traeTodosLosDatosUsuario(usuario.id);
|
|
res.json(datos);
|
|
} catch (error) {
|
|
logger.error(`Error en /traerTodosDatosAlumno: ${error.message}`);
|
|
res.status(500).json({ error: error.message });
|
|
}
|
|
});
|
|
|
|
app.post("/iscribirAlumno", async (req, res) => {
|
|
try {
|
|
const usuario = req.body;
|
|
const datos = await DAO.inscribirUsuario(usuario);
|
|
if (datos) {
|
|
res.json({ message: true });
|
|
} else {
|
|
res.json({ message: false });
|
|
}
|
|
} catch (error) {
|
|
logger.error(`Error en /iscribirAlumno: ${error.message}`);
|
|
res.status(500).json({ error: error.message, message: false });
|
|
}
|
|
});
|
|
|
|
app.post("/regresarAlumno", async (req, res) => {
|
|
try {
|
|
const data = req.body;
|
|
const cambiar = await DAO.editarAlumnoRechazado(data, 0);
|
|
if (cambiar) {
|
|
res.json({ message: true });
|
|
} else {
|
|
res.json({ message: false });
|
|
}
|
|
} catch (error) {
|
|
logger.error(`Error en /regresarAlumno: ${error.message}`);
|
|
res.status(500).json({ error: error.message, message: false });
|
|
}
|
|
});
|
|
|
|
app.post('/email', EmailCtrl.sendEmail);
|