Skip to content

Commit

Permalink
Merge pull request #67 from Saca-la-Bici/feat/0.1.5/actividades
Browse files Browse the repository at this point in the history
Feat/0.1.5/actividades: Cambios y HU45
  • Loading branch information
Jesus0204 authored Sep 30, 2024
2 parents 6f3a64d + 2df3d1c commit 7408ad9
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 22 deletions.
6 changes: 3 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,6 @@ app.use((request, response) => {
});
});

app.listen(port, '0.0.0.0', () => {
console.log('Server running on all network interfaces');
});
app.listen(port, () => {
console.log(`Servidor escuchando en el puerto ${port}`);
});
23 changes: 18 additions & 5 deletions models/actividades/actividad.model.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const mongoose = require('mongoose');
const comentarioSchema = require('../foro/comentario.model');
const Foro = require('../foro/foro.model');

const actividadSchema = new mongoose.Schema ({
titulo: {
Expand Down Expand Up @@ -49,10 +49,23 @@ const actividadSchema = new mongoose.Schema ({
immutable: true,
enum: ['Rodada', 'Taller', 'Evento']
},
comentarios: {
type: [comentarioSchema],
default: []
},
foro: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Foro'
}
});

// Middleware para crear un foro vacío antes de guardar la actividad
actividadSchema.pre('save', async function(next) {
if (this.isNew) {
try {
const foro = await Foro.create({});
this.foro = foro._id;
} catch (error) {
return next(error);
}
}
next();
});

module.exports = actividadSchema;
28 changes: 28 additions & 0 deletions models/actividades/consultarActividadIndividual.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const Rodada = require('./rodada.model');
const Taller = require('./taller.model');
const Evento = require('./evento.model');

async function encontrarEvento(id) {

// Encontrar el tipo de evento por colección
let event = await Rodada.findById(id);
if (event) return { model: Rodada, event };

event = await Taller.findById(id);
if (event) return { model: Taller, event };

event = await Evento.findById(id);
if (event) return { model: Evento, event };


throw new Error('No se encontró ninguna actividad con este ID.');
}

async function consultarActividadIndividual(id) {
const { model } = await encontrarEvento(id);

const actividad = await model.findById(id);
return actividad;
}

module.exports = { consultarActividadIndividual };
3 changes: 1 addition & 2 deletions models/actividades/registraActividad.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ const Rodada = require('./rodada.model');
const Taller = require('./taller.model');
const Evento = require('./evento.model');

async function postRodada(data, id) {
data.ruta = id;
async function postRodada(data) {
if (Array.isArray(data.informacion)) {
data.informacion = data.informacion.map(info => {
if (info.fecha) {
Expand Down
9 changes: 5 additions & 4 deletions models/foro/comentario.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ const comentarioSchema = new Schema({
type: Number,
default: 0
},
respuestas: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Comentario'
}]
respuestaDe: {
type: Schema.Types.ObjectId,
ref: 'Comentario',
default: null
}
})

module.exports = comentarioSchema;
14 changes: 14 additions & 0 deletions models/foro/foro.model.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const comentarioSchema = require('../foro/comentario.model');

const foroSchema = new Schema({
comentarios: {
type: [comentarioSchema],
default: []
}
}, {
collection: 'Foro'
});

module.exports = mongoose.model('Foro', foroSchema);
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const Rodada = require('../../../models/actividades/rodada.model');
const Evento = require('../../../models/actividades/evento.model');
const Taller = require('../../../models/actividades/taller.model');
const { consultarActividadIndividual } = require('../../../models/actividades/consultarActividadIndividual.model');

const getRodadas = async (request, response) => {
try {
Expand Down Expand Up @@ -38,4 +39,18 @@ const getTalleres = async (request, response) => {
}
}

module.exports = { getRodadas, getEventos, getTalleres };
const getActividad = async (request, response) => {
const id = request.query.id;
console.log("ID: ", id);
try {
const actividad = await consultarActividadIndividual(id);
response.status(200).json({
actividad: actividad,
permisos: request.permisos
});
} catch (error) {
return response.status(500).json({ message: 'Error al obtener la actividad', error });
}
}

module.exports = { getRodadas, getEventos, getTalleres, getActividad };
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ const {

exports.postRodada = async (request, response) => {
const data = request.body;
const {id} = request.query;

try {
const registro = await postRodada(data, id);
const registro = await postRodada(data);
response.status(201).json({ message: `Rodada creada exitosamente.`, registro });
} catch (error) {
response.status(500).json({ message: `Error al crear la rodada`, error });
Expand Down
3 changes: 2 additions & 1 deletion modules/actividades/routes/consultarActividades.routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ const router = express.Router();
const verifyUserToken = require('../../../util/verifyUserToken');
const verifyUserPermissions = require('../../../util/verifyUserPermissions');

const { getRodadas, getEventos, getTalleres } = require('../controllers/consultarActividades.controller');
const { getRodadas, getEventos, getTalleres, getActividad } = require('../controllers/consultarActividades.controller');

router.get('/rodadas', verifyUserToken, verifyUserPermissions, getRodadas);
router.get('/eventos', verifyUserToken, verifyUserPermissions, getEventos);
router.get('/talleres', verifyUserToken, verifyUserPermissions, getTalleres);
router.get('/', verifyUserToken, verifyUserPermissions, getActividad);

module.exports = router;
7 changes: 3 additions & 4 deletions modules/actividades/routes/registrarActividad.routes.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const express = require('express');
const router = express.Router();
const verifyUserToken = require('../../../util/verifyUserToken')
const verifyUserPermissions = require('../../../util/verifyUserPermissions');

// Importar el controlador que maneja la creación de actividades
const registrarActividadController = require('../controllers/registrarActividad.controller');

// Definir la ruta para crear una actividad
router.post('/rodada', verifyUserToken, verifyUserPermissions, registrarActividadController.postRodada);
router.post('/taller', verifyUserToken, verifyUserPermissions, registrarActividadController.postTaller);
router.post('/evento', verifyUserToken, verifyUserPermissions, registrarActividadController.postEvento);
router.post('/rodada', verifyUserToken, registrarActividadController.postRodada);
router.post('/taller', verifyUserToken, registrarActividadController.postTaller);
router.post('/evento', verifyUserToken, registrarActividadController.postEvento);

module.exports = router;

0 comments on commit 7408ad9

Please sign in to comment.