Files
admin-edu-space/frontend/src/routes/buildings.js
T

409 lines
15 KiB
JavaScript

const express = require('express');
const router = express.Router();
const { requireAuth } = require('../middlewares/authMiddleware');
const { createFormMock } = require('../utils/formHelper');
router.use(requireAuth);
// Fallback data si la API estuviera inaccesible (con capacidad física real)
const defaultBuildings = [
{
id: 1,
code: 'CENTRAL',
name: 'Edificio Central',
is_active: true,
is_virtual: false,
address: 'Tte. Gral. Juan Domingo Perón 802, CABA',
description: 'Sede central institucional de UniCABA. Aulas de grado, auditorio y laboratorios.',
floors: 4,
classrooms_count: 29,
physical_capacity: 1250,
total_capacity: 1250,
capacity_display: '1250'
},
{
id: 2,
code: 'VIRTUAL',
name: 'Campus Virtual',
is_active: true,
is_virtual: true,
address: 'campus.unicaba.edu.ar / MS Teams & Moodle',
description: 'Entorno de aprendizaje digital, aulas sincrónicas y videoconferencias.',
floors: 0,
classrooms_count: 2,
physical_capacity: 0,
total_capacity: 0,
capacity_display: 'Ilimitada (Virtual)'
}
];
// 1. List Buildings
const handleListBuildings = async (req, res) => {
try {
const { search = '', status = '' } = req.query;
let buildings = [];
let stats = {
total_buildings: 2,
total_classrooms: 31,
physical_classrooms_count: 29,
total_physical_capacity: 1250,
total_floors: 4
};
try {
const apiRes = await req.apiClient.get('/buildings', { params: { search, status } });
if (apiRes.data && apiRes.data.buildings) {
buildings = apiRes.data.buildings;
if (apiRes.data.stats) {
stats = apiRes.data.stats;
}
}
} catch (apiErr) {
console.warn('API /buildings notice, using fallback:', apiErr.message);
buildings = [...defaultBuildings];
}
// Si por alguna razón la lista vino vacía de la API y no había filtros de búsqueda, usar default
if (buildings.length === 0 && !search && !status) {
buildings = [...defaultBuildings];
}
// Asegurar campos calculados consistentes para cada edificio
buildings = buildings.map(b => {
const is_virtual = Boolean(b.is_virtual || (b.code && b.code.toUpperCase() === 'VIRTUAL') || (b.name && b.name.toUpperCase().includes('VIRTUAL')));
return {
...b,
is_virtual,
capacity_display: is_virtual ? 'Ilimitada (Virtual)' : String(b.physical_capacity !== undefined ? b.physical_capacity : (b.total_capacity || 0)),
total_capacity: is_virtual ? 0 : (b.physical_capacity !== undefined ? b.physical_capacity : (b.total_capacity || 0)),
floors_display: is_virtual ? 'Digital' : String(b.floors ?? 1)
};
});
// Capacidad física total: suma exclusiva de sedes presenciales (nunca suma 9999)
const total_physical_capacity = stats.total_physical_capacity || buildings
.filter(b => !b.is_virtual)
.reduce((acc, b) => acc + (b.physical_capacity || b.total_capacity || 0), 0);
const total_floors = stats.total_floors || buildings
.filter(b => !b.is_virtual)
.reduce((acc, b) => acc + (parseInt(b.floors, 10) || 0), 0);
res.render('buildings/list', {
title: 'Gestión de Edificios - Edu-Space Admin',
buildings,
total_classrooms: stats.total_classrooms || buildings.reduce((a, b) => a + (b.classrooms_count || 0), 0),
total_capacity: total_physical_capacity,
total_physical_capacity,
total_floors,
search,
status_filter: status
});
} catch (error) {
console.error('Error rendering buildings list:', error.message);
res.render('buildings/list', {
title: 'Gestión de Edificios - Edu-Space Admin',
buildings: defaultBuildings,
total_classrooms: 31,
total_capacity: 1250,
total_physical_capacity: 1250,
total_floors: 4,
search: '',
status_filter: ''
});
}
};
router.get(['/', '/list', '/list_buildings'], handleListBuildings);
// 2. Add Building (GET & POST)
router.get(['/add', '/add_building'], (req, res) => {
const form = createFormMock({
name: '',
code: '',
address: '',
floors: 1,
description: '',
is_active: true
});
res.render('buildings/form', {
title: 'Nuevo Edificio - Edu-Space Admin',
building: null,
form
});
});
router.post(['/add', '/add_building'], async (req, res) => {
try {
const { name, code, address, floors, description, is_active } = req.body;
const payload = {
name: (name || '').trim(),
code: (code || '').trim().toUpperCase() || null,
address: (address || '').trim() || null,
floors: parseInt(floors, 10) || 1,
description: (description || '').trim() || null,
is_active: is_active === 'true' || is_active === 'on' || is_active === true || is_active === '1'
};
await req.apiClient.post('/buildings', payload);
res.redirect('/buildings/list_buildings?created=1');
} catch (err) {
console.warn('Error creating building:', err.response?.data || err.message);
res.redirect('/buildings/list_buildings?error=create');
}
});
// Función auxiliar para agrupar y sintetizar los pisos / niveles físicos y sus aulas
const buildFloorsList = (building, classrooms = []) => {
const is_virtual = Boolean(building.is_virtual || (building.code && building.code.toUpperCase() === 'VIRTUAL') || (building.name && building.name.toUpperCase().includes('VIRTUAL')));
if (is_virtual) {
return [{
floor_code: 'Digital',
floor_name: 'Campus Virtual (Digital)',
classrooms: classrooms,
total_capacity: 0,
classrooms_count: classrooms.length
}];
}
const floorsMap = new Map();
// 1. Incorporar todas las aulas reales existentes agrupadas por su piso
classrooms.forEach(c => {
let code = String(c.floor !== undefined && c.floor !== null ? c.floor : 'PB').trim();
let name = c.floor_display;
if (!name) {
const fLower = code.toLowerCase();
if (code === '0' || code.toUpperCase() === 'PB' || fLower.includes('planta baja')) {
name = 'Planta Baja (PB)';
code = 'PB';
} else if (code.toUpperCase() === 'EP' || fLower.includes('entrepiso')) {
name = 'Entrepiso (EP)';
code = 'EP';
} else if (code.toUpperCase() === 'SS' || fLower.includes('subsuelo')) {
name = 'Subsuelo (SS)';
code = 'SS';
} else if (fLower.startsWith('piso')) {
name = code;
} else if (/^\d+$/.test(code)) {
name = `Piso ${code}`;
} else {
name = code;
}
}
if (!floorsMap.has(code)) {
floorsMap.set(code, {
floor_code: code,
floor_name: name,
classrooms: [],
total_capacity: 0
});
}
const fEntry = floorsMap.get(code);
fEntry.classrooms.push(c);
fEntry.total_capacity += (c.capacity || 0);
});
// 2. Si el edificio tiene floors > 0, asegurar niveles básicos esperados (PB y Pisos 1..N)
const numFloors = parseInt(building.floors, 10) || 1;
if (numFloors >= 1 && !floorsMap.has('PB') && !floorsMap.has('0')) {
floorsMap.set('PB', {
floor_code: 'PB',
floor_name: 'Planta Baja (PB)',
classrooms: [],
total_capacity: 0
});
}
for (let i = 1; i < numFloors; i++) {
const floorStr = String(i);
if (!floorsMap.has(floorStr)) {
floorsMap.set(floorStr, {
floor_code: floorStr,
floor_name: `Piso ${i}`,
classrooms: [],
total_capacity: 0
});
}
}
if (numFloors === 1 && floorsMap.size === 0) {
floorsMap.set('PB', {
floor_code: 'PB',
floor_name: 'Planta Baja (PB)',
classrooms: [],
total_capacity: 0
});
}
// Ordenar pisos: Subsuelos primero, Planta Baja, Entrepisos, luego numéricos
const sortedFloors = Array.from(floorsMap.values()).sort((a, b) => {
const cA = String(a.floor_code).toUpperCase();
const cB = String(b.floor_code).toUpperCase();
const nA = String(a.floor_name).toUpperCase();
const nB = String(b.floor_name).toUpperCase();
if (cA.includes('SUB') || cA.includes('SS') || nA.includes('SUB')) return -1;
if (cB.includes('SUB') || cB.includes('SS') || nB.includes('SUB')) return 1;
if (cA === 'PB' || cA === '0' || nA.includes('PLANTA BAJA')) return -1;
if (cB === 'PB' || cB === '0' || nB.includes('PLANTA BAJA')) return 1;
if (cA === 'EP' || nA.includes('ENTRE')) return -1;
if (cB === 'EP' || nB.includes('ENTRE')) return 1;
const numA = parseInt(a.floor_code, 10);
const numB = parseInt(b.floor_code, 10);
if (!isNaN(numA) && !isNaN(numB)) return numA - numB;
return a.floor_name.localeCompare(b.floor_name, undefined, { numeric: true });
});
sortedFloors.forEach(f => {
f.classrooms_count = f.classrooms.length;
});
return sortedFloors;
};
// 3. View Building
router.get(['/view/:id', '/view_building', '/:id'], async (req, res) => {
const id = parseInt(req.params.id || req.query.id, 10) || 1;
let building = null;
let classrooms = [];
try {
const apiRes = await req.apiClient.get(`/buildings/${id}`);
if (apiRes.data && apiRes.data.building) {
building = apiRes.data.building;
classrooms = apiRes.data.classrooms || [];
}
} catch (e) {
building = defaultBuildings.find(b => b.id === id) || defaultBuildings[0];
}
if (!building) {
return res.redirect('/buildings/list_buildings');
}
const is_virtual = Boolean(building.is_virtual || (building.code && building.code.toUpperCase() === 'VIRTUAL') || (building.name && building.name.toUpperCase().includes('VIRTUAL')));
building = {
...building,
is_virtual,
capacity_display: is_virtual ? 'Ilimitada (Virtual)' : String(building.physical_capacity || building.total_capacity || 0),
floors_display: is_virtual ? 'Digital' : String(building.floors ?? 1)
};
const floors_list = buildFloorsList(building, classrooms);
const classrooms_by_floor = {};
floors_list.forEach(f => {
classrooms_by_floor[f.floor_name] = f.classrooms;
});
res.render('buildings/view', {
title: `Edificio ${building.name} - Edu-Space Admin`,
building,
classrooms,
classrooms_by_floor,
floors_list
});
});
// 4. Edit Building (GET)
const handleEditBuildingGet = async (req, res) => {
const id = parseInt(req.params.id || req.query.id, 10) || 1;
let building = null;
let classrooms = [];
try {
const apiRes = await req.apiClient.get(`/buildings/${id}`);
if (apiRes.data && apiRes.data.building) {
building = apiRes.data.building;
classrooms = apiRes.data.classrooms || [];
}
} catch (e) {
building = defaultBuildings.find(b => b.id === id) || defaultBuildings[0];
}
if (!building) {
return res.redirect('/buildings/list_buildings');
}
const is_virtual = Boolean(building.is_virtual || (building.code && building.code.toUpperCase() === 'VIRTUAL') || (building.name && building.name.toUpperCase().includes('VIRTUAL')));
building = {
...building,
is_virtual,
capacity_display: is_virtual ? 'Ilimitada (Virtual)' : String(building.physical_capacity || building.total_capacity || 0),
floors_display: is_virtual ? 'Digital' : String(building.floors ?? 1)
};
const floors_list = buildFloorsList(building, classrooms);
const total_physical_capacity = is_virtual ? 0 : classrooms.reduce((acc, c) => acc + (c.capacity || 0), 0);
const form = createFormMock({
name: building.name,
code: building.code,
address: building.address,
floors: building.floors,
description: building.description,
is_active: building.is_active
});
res.render('buildings/form', {
title: `Editar Edificio - ${building.name}`,
building,
classrooms,
floors_list,
total_physical_capacity,
form
});
};
router.get(['/edit/:id', '/:id/edit', '/edit_building'], handleEditBuildingGet);
// 5. Edit Building (POST)
const handleEditBuildingPost = async (req, res) => {
const id = parseInt(req.params.id || req.query.id || req.body.id, 10) || 1;
const { name, code, address, floors, description, is_active } = req.body;
try {
const payload = {
name: (name || '').trim(),
code: (code || '').trim().toUpperCase() || null,
address: (address || '').trim() || null,
floors: parseInt(floors, 10) || 0,
description: (description || '').trim() || null,
is_active: is_active === 'true' || is_active === 'on' || is_active === true || is_active === '1'
};
await req.apiClient.put(`/buildings/${id}`, payload);
res.redirect('/buildings/list_buildings?updated=1');
} catch (err) {
console.warn('Error updating building:', err.response?.data || err.message);
res.redirect('/buildings/list_buildings?error=update');
}
};
router.post(['/edit/:id', '/:id/edit', '/edit_building'], handleEditBuildingPost);
// 6. Toggle Building
router.post(['/toggle/:id', '/:id/toggle', '/toggle_building'], async (req, res) => {
const id = parseInt(req.params.id || req.query.id || req.body.id, 10) || 1;
try {
await req.apiClient.post(`/buildings/${id}/toggle`);
} catch (e) {
console.warn('Error toggling building:', e.message);
}
res.redirect('/buildings/list_buildings');
});
// 7. Delete Building
router.post(['/delete/:id', '/:id/delete', '/delete_building'], async (req, res) => {
const id = parseInt(req.params.id || req.query.id || req.body.id, 10);
try {
await req.apiClient.delete(`/buildings/${id}`);
} catch (e) {
console.warn('Error deleting building:', e.message);
}
res.redirect('/buildings/list_buildings');
});
module.exports = router;