feat(v3.4.7): add interactive floors and classrooms panel in building edit and preselect room creation

This commit is contained in:
2026-10-08 10:28:28 -03:00
parent 368dc71b14
commit ea9991e795
14 changed files with 573 additions and 59 deletions
+150 -21
View File
@@ -180,36 +180,149 @@ router.get(['/view/:id', '/view_building', '/:id'], async (req, res) => {
floors_display: is_virtual ? 'Digital' : String(building.floors ?? 1)
};
// Agrupación dinámica por piso / nivel
// 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 => {
const floorKey = c.floor_display || (c.floor === '0' || c.floor === 'PB' ? 'Planta Baja (PB)' : (c.floor ? (c.floor.toLowerCase().startsWith('piso') ? c.floor : `Piso ${c.floor}`) : 'Planta Baja (PB)'));
if (!floorsMap.has(floorKey)) {
floorsMap.set(floorKey, []);
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;
}
}
floorsMap.get(floorKey).push(c);
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);
});
const sortedFloorKeys = Array.from(floorsMap.keys()).sort((a, b) => {
const fA = String(a).toUpperCase();
const fB = String(b).toUpperCase();
if (fA.includes('SUB') || fA.includes('SS')) return -1;
if (fB.includes('SUB') || fB.includes('SS')) return 1;
if (fA.includes('PB') || fA.includes('PLANTA BAJA')) return -1;
if (fB.includes('PB') || fB.includes('PLANTA BAJA')) return 1;
if (fA.includes('EP') || fA.includes('ENTRE')) return -1;
if (fB.includes('EP') || fB.includes('ENTRE')) return 1;
return fA.localeCompare(fB, undefined, { numeric: true });
// 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 });
});
const floors_list = sortedFloorKeys.map(floorName => ({
floor_name: floorName,
classrooms: floorsMap.get(floorName)
}));
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 = {};
floorsMap.forEach((rooms, floorName) => {
classrooms_by_floor[floorName] = rooms;
floors_list.forEach(f => {
classrooms_by_floor[f.floor_name] = f.classrooms;
});
res.render('buildings/view', {
@@ -225,11 +338,13 @@ router.get(['/view/:id', '/view_building', '/:id'], async (req, res) => {
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];
@@ -239,6 +354,17 @@ const handleEditBuildingGet = async (req, res) => {
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,
@@ -251,6 +377,9 @@ const handleEditBuildingGet = async (req, res) => {
res.render('buildings/form', {
title: `Editar Edificio - ${building.name}`,
building,
classrooms,
floors_list,
total_physical_capacity,
form
});
};