fix(classrooms): blindar vistas presenciales y virtuales sin aulas + feat(admin): reinicializacion de sistema con clave de admin
This commit is contained in:
@@ -1529,5 +1529,7 @@ router.post('/settings/moodle/test', forwardSettingsAction);
|
||||
router.post('/settings/moodle/sync', forwardSettingsAction);
|
||||
router.get('/settings/moodle/stats', forwardSettingsAction);
|
||||
router.post('/settings/ui', forwardSettingsAction);
|
||||
router.post('/settings/reset_system', forwardSettingsAction);
|
||||
router.post('/system/reset', forwardSettingsAction);
|
||||
|
||||
module.exports = router;
|
||||
|
||||
@@ -6,8 +6,13 @@ router.use(requireAuth);
|
||||
|
||||
const handleClassroomsList = async (req, res) => {
|
||||
try {
|
||||
const response = await req.apiClient.get('/classrooms');
|
||||
const allClassrooms = (response.data && response.data.classrooms) || [];
|
||||
let allClassrooms = [];
|
||||
try {
|
||||
const response = await req.apiClient.get('/classrooms');
|
||||
allClassrooms = (response.data && response.data.classrooms) || [];
|
||||
} catch (cErr) {
|
||||
console.warn('Notice fetching classrooms:', cErr.message);
|
||||
}
|
||||
|
||||
const view_mode = (req.query.view_mode || 'physical').toLowerCase();
|
||||
const search = (req.query.search || '').toLowerCase().trim();
|
||||
@@ -17,7 +22,7 @@ const handleClassroomsList = async (req, res) => {
|
||||
|
||||
// Filter based on view mode (physical vs virtual)
|
||||
let filtered = allClassrooms.filter(c => {
|
||||
const isVirt = Boolean(c.is_virtual || (c.code && c.code.includes('VIRTUAL')) || (c.building && c.building.toLowerCase().includes('virtual')));
|
||||
const isVirt = Boolean(c.is_virtual || ((c.code || '').includes('VIRTUAL')) || ((c.building || '').toLowerCase().includes('virtual')));
|
||||
if (view_mode === 'virtual') return isVirt;
|
||||
return !isVirt;
|
||||
});
|
||||
@@ -25,9 +30,9 @@ const handleClassroomsList = async (req, res) => {
|
||||
// Additional filters
|
||||
if (search) {
|
||||
filtered = filtered.filter(c =>
|
||||
(c.code && c.code.toLowerCase().includes(search)) ||
|
||||
(c.description && c.description.toLowerCase().includes(search)) ||
|
||||
(c.building && c.building.toLowerCase().includes(search))
|
||||
((c.code || '').toLowerCase().includes(search)) ||
|
||||
((c.description || '').toLowerCase().includes(search)) ||
|
||||
((c.building || '').toLowerCase().includes(search))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -43,9 +48,21 @@ const handleClassroomsList = async (req, res) => {
|
||||
filtered = filtered.filter(c => c.is_active);
|
||||
}
|
||||
|
||||
// Extract unique buildings and floors
|
||||
// Extract unique buildings and floors (consultando también /buildings si está disponible)
|
||||
const buildingsMap = new Map();
|
||||
const floorsSet = new Set();
|
||||
try {
|
||||
const bRes = await req.apiClient.get('/buildings');
|
||||
const apiBuildings = (bRes.data && bRes.data.buildings) || [];
|
||||
apiBuildings.forEach(b => {
|
||||
if (b.id && b.name) {
|
||||
buildingsMap.set(b.id, { id: b.id, name: b.name });
|
||||
}
|
||||
});
|
||||
} catch (bErr) {
|
||||
console.warn('Notice fetching buildings for classrooms filter:', bErr.message);
|
||||
}
|
||||
|
||||
allClassrooms.forEach(c => {
|
||||
if (c.building_id && c.building) {
|
||||
buildingsMap.set(c.building_id, { id: c.building_id, name: c.building });
|
||||
@@ -55,8 +72,8 @@ const handleClassroomsList = async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
const physical_count = allClassrooms.filter(c => !c.is_virtual && !c.code.includes('VIRTUAL')).length;
|
||||
const virtual_count = allClassrooms.filter(c => c.is_virtual || c.code.includes('VIRTUAL')).length;
|
||||
const physical_count = allClassrooms.filter(c => !c.is_virtual && !(c.code || '').includes('VIRTUAL')).length;
|
||||
const virtual_count = allClassrooms.filter(c => Boolean(c.is_virtual || (c.code || '').includes('VIRTUAL'))).length;
|
||||
|
||||
const dateFilters = {
|
||||
today: new Date().toISOString().split('T')[0],
|
||||
@@ -80,7 +97,7 @@ const handleClassroomsList = async (req, res) => {
|
||||
|
||||
// Format classrooms with fallback fields and assigned commissions/classes
|
||||
const formattedItems = filtered.map(c => {
|
||||
const c_res = allReservations.filter(r => String(r.classroom_id) === String(c.id) || (r.classroom && String(r.classroom.id) === String(c.id)));
|
||||
const c_res = allReservations.filter(r => (c.id !== undefined && (String(r.classroom_id) === String(c.id) || (r.classroom && String(r.classroom.id) === String(c.id)))));
|
||||
const commMap = new Map();
|
||||
c_res.forEach(r => {
|
||||
const cid = r.commission_id || (r.commission && r.commission.id) || r.id;
|
||||
@@ -88,15 +105,15 @@ const handleClassroomsList = async (req, res) => {
|
||||
commMap.set(cid, {
|
||||
commission_id: cid,
|
||||
commission_code: (r.commission && r.commission.code) || 'C1',
|
||||
subject_name: r.subject_name || r.purpose,
|
||||
subject_name: r.subject_name || r.purpose || 'Clase Regular',
|
||||
subject_code: r.subject_code || '',
|
||||
shift: r.shift || 'Tarde'
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
const day_res = c_res.filter(r => r.start_time && (r.start_time.startsWith('2026-09-18') || r.start_time.startsWith('2026-09-15')));
|
||||
const week_res = c_res.filter(r => r.start_time && r.start_time >= '2026-09-14' && r.start_time <= '2026-09-20');
|
||||
const day_res = c_res.filter(r => (r.start_time || '').startsWith('2026-09-18') || (r.start_time || '').startsWith('2026-09-15'));
|
||||
const week_res = c_res.filter(r => (r.start_time || '') >= '2026-09-14' && (r.start_time || '') <= '2026-09-20');
|
||||
const day_pct = Math.min(100, Math.round((day_res.length / 3) * 100));
|
||||
const week_pct = Math.min(100, Math.round((week_res.length / 18) * 100));
|
||||
const month_pct = Math.min(100, Math.round((c_res.length / 72) * 100));
|
||||
@@ -129,8 +146,8 @@ const handleClassroomsList = async (req, res) => {
|
||||
total_count: 0
|
||||
};
|
||||
|
||||
const virtualClassroomObj = allClassrooms.find(c => Boolean(c.is_virtual || (c.code && c.code.includes('VIRTUAL'))));
|
||||
const virtual_classroom_id = virtualClassroomObj ? virtualClassroomObj.id : 32;
|
||||
const virtualClassroomObj = allClassrooms.find(c => Boolean(c.is_virtual || ((c.code || '').includes('VIRTUAL'))));
|
||||
const virtual_classroom_id = virtualClassroomObj ? virtualClassroomObj.id : null;
|
||||
|
||||
if (view_mode === 'virtual') {
|
||||
try {
|
||||
@@ -162,12 +179,14 @@ const handleClassroomsList = async (req, res) => {
|
||||
|
||||
virtual_rooms = Array.from(commMap.values()).map(data => {
|
||||
const resList = data.reservations;
|
||||
const dayCnt = resList.filter(r => r.start_time.startsWith(todayStr)).length;
|
||||
const dayCnt = resList.filter(r => (r.start_time || '').startsWith(todayStr)).length;
|
||||
const weekCnt = resList.filter(r => {
|
||||
if (!r.start_time) return false;
|
||||
const d = new Date(r.start_time);
|
||||
return d >= weekStart && d <= weekEnd;
|
||||
}).length;
|
||||
const monthCnt = resList.filter(r => {
|
||||
if (!r.start_time) return false;
|
||||
const d = new Date(r.start_time);
|
||||
return d >= monthStart && d <= monthEnd;
|
||||
}).length;
|
||||
@@ -186,27 +205,29 @@ const handleClassroomsList = async (req, res) => {
|
||||
week_count: weekCnt,
|
||||
month_count: monthCnt,
|
||||
total_count: resList.length,
|
||||
reservations: resList.sort((a, b) => a.start_time.localeCompare(b.start_time))
|
||||
reservations: resList.sort((a, b) => (a.start_time || '').localeCompare(b.start_time || ''))
|
||||
};
|
||||
});
|
||||
|
||||
if (search) {
|
||||
virtual_rooms = virtual_rooms.filter(room =>
|
||||
room.subject_name.toLowerCase().includes(search) ||
|
||||
room.commission_code.toLowerCase().includes(search) ||
|
||||
room.subject_code.toLowerCase().includes(search)
|
||||
(room.subject_name || '').toLowerCase().includes(search) ||
|
||||
(room.commission_code || '').toLowerCase().includes(search) ||
|
||||
(room.subject_code || '').toLowerCase().includes(search)
|
||||
);
|
||||
}
|
||||
|
||||
virtual_rooms.sort((a, b) => a.subject_name.localeCompare(b.subject_name));
|
||||
virtual_rooms.sort((a, b) => (a.subject_name || '').localeCompare(b.subject_name || ''));
|
||||
|
||||
global_virtual_metrics = {
|
||||
today_count: virtualReservations.filter(r => r.start_time.startsWith(todayStr)).length,
|
||||
today_count: virtualReservations.filter(r => (r.start_time || '').startsWith(todayStr)).length,
|
||||
week_count: virtualReservations.filter(r => {
|
||||
if (!r.start_time) return false;
|
||||
const d = new Date(r.start_time);
|
||||
return d >= weekStart && d <= weekEnd;
|
||||
}).length,
|
||||
month_count: virtualReservations.filter(r => {
|
||||
if (!r.start_time) return false;
|
||||
const d = new Date(r.start_time);
|
||||
return d >= monthStart && d <= monthEnd;
|
||||
}).length,
|
||||
@@ -247,10 +268,11 @@ const handleClassroomsList = async (req, res) => {
|
||||
|
||||
} catch (error) {
|
||||
console.error('Error in classrooms list route:', error.message);
|
||||
const view_mode = (req.query.view_mode || 'physical').toLowerCase();
|
||||
res.render('classrooms/list', {
|
||||
title: 'Aulas y Espacios (Offline)',
|
||||
title: 'Aulas y Espacios - Edu-Space Admin',
|
||||
classrooms: { items: [], total: 0, pages: 1, page: 1, has_prev: false, has_next: false, iter_pages: () => [1] },
|
||||
view_mode: 'physical',
|
||||
view_mode,
|
||||
search: '',
|
||||
building_filter: '',
|
||||
floor_filter: '',
|
||||
@@ -259,8 +281,17 @@ const handleClassroomsList = async (req, res) => {
|
||||
virtual_count: 0,
|
||||
buildings: [],
|
||||
floors: [],
|
||||
occupancy_metrics: { get: (id, defVal) => defVal },
|
||||
date_filters: {}
|
||||
occupancy_metrics: {},
|
||||
date_filters: {},
|
||||
virtual_rooms: [],
|
||||
global_virtual_metrics: {
|
||||
today_count: 0,
|
||||
week_count: 0,
|
||||
month_count: 0,
|
||||
commissions_count: 0,
|
||||
total_count: 0
|
||||
},
|
||||
virtual_classroom_id: null
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -270,38 +301,53 @@ const { createFormMock } = require('../utils/formHelper');
|
||||
const handleAddClassroom = async (req, res) => {
|
||||
try {
|
||||
let allClassrooms = [];
|
||||
let allBuildings = [];
|
||||
try {
|
||||
const response = await req.apiClient.get('/classrooms');
|
||||
allClassrooms = (response.data && response.data.classrooms) || [];
|
||||
const [cRes, bRes] = await Promise.allSettled([
|
||||
req.apiClient.get('/classrooms'),
|
||||
req.apiClient.get('/buildings')
|
||||
]);
|
||||
if (cRes.status === 'fulfilled' && cRes.value.data?.classrooms) {
|
||||
allClassrooms = cRes.value.data.classrooms;
|
||||
}
|
||||
if (bRes.status === 'fulfilled' && bRes.value.data?.buildings) {
|
||||
allBuildings = bRes.value.data.buildings;
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('Could not fetch classrooms for building floors:', e.message);
|
||||
console.warn('Could not fetch classrooms/buildings for add page:', e.message);
|
||||
}
|
||||
|
||||
// Map floors per building
|
||||
const buildingFloors = {
|
||||
'1': ['PB', '1', '2', '3'],
|
||||
'2': ['Virtual']
|
||||
};
|
||||
const buildingsMap = new Map();
|
||||
buildingsMap.set('1', { id: 1, name: 'Edificio Central' });
|
||||
buildingsMap.set('2', { id: 2, name: 'Campus Virtual' });
|
||||
allBuildings.forEach(b => {
|
||||
if (b.id && b.name) {
|
||||
buildingsMap.set(String(b.id), { id: b.id, name: b.name });
|
||||
}
|
||||
});
|
||||
|
||||
const buildingFloors = {};
|
||||
allBuildings.forEach(b => {
|
||||
const bId = String(b.id);
|
||||
buildingFloors[bId] = b.is_virtual ? ['Digital'] : ['PB', '1', '2'];
|
||||
});
|
||||
|
||||
allClassrooms.forEach(c => {
|
||||
const bId = String(c.building_id || 1);
|
||||
if (!buildingFloors[bId]) buildingFloors[bId] = [];
|
||||
if (c.floor && !buildingFloors[bId].includes(c.floor)) {
|
||||
buildingFloors[bId].push(c.floor);
|
||||
}
|
||||
if (c.building_id && c.building) {
|
||||
buildingsMap.set(bId, { id: c.building_id, name: c.building });
|
||||
const bId = String(c.building_id || '');
|
||||
if (bId) {
|
||||
if (!buildingFloors[bId]) buildingFloors[bId] = [];
|
||||
if (c.floor && !buildingFloors[bId].includes(c.floor)) {
|
||||
buildingFloors[bId].push(c.floor);
|
||||
}
|
||||
if (c.building) {
|
||||
buildingsMap.set(bId, { id: c.building_id, name: c.building });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
const buildings = Array.from(buildingsMap.values());
|
||||
const buildingOptions = [
|
||||
{ value: '', text: '-- Selecciona Edificio / Sede --' },
|
||||
{ value: '', text: buildings.length > 0 ? '-- Selecciona Edificio / Sede --' : '-- Sin sedes registradas (Cree una abajo) --' },
|
||||
...buildings.map(b => ({ value: b.id, text: b.name })),
|
||||
{ value: 'new', text: '+ Crear nuevo edificio' }
|
||||
{ value: 'new', text: '+ Crear nuevo edificio / sede' }
|
||||
];
|
||||
|
||||
const form = createFormMock({
|
||||
@@ -327,19 +373,12 @@ const handlePostClassroom = async (req, res) => {
|
||||
try {
|
||||
const { building_id, new_building_name, floor, room_number, capacity, description, is_active } = req.body;
|
||||
|
||||
let targetBuilding = building_id;
|
||||
if (building_id === 'new' && new_building_name) {
|
||||
targetBuilding = new_building_name;
|
||||
}
|
||||
|
||||
let bId = parseInt(targetBuilding, 10);
|
||||
if (isNaN(bId) || bId <= 0) {
|
||||
bId = (String(targetBuilding).toLowerCase().includes('virtual')) ? 2 : 1;
|
||||
}
|
||||
const isNewBuilding = building_id === 'new' || !building_id;
|
||||
const bName = (new_building_name || '').trim();
|
||||
const bId = (!isNewBuilding && building_id) ? parseInt(building_id, 10) : null;
|
||||
|
||||
const isVirtual = Boolean(
|
||||
bId === 2 ||
|
||||
String(targetBuilding).toLowerCase().includes('virtual') ||
|
||||
(bName && bName.toLowerCase().includes('virtual')) ||
|
||||
String(room_number).toUpperCase().includes('VIRTUAL') ||
|
||||
String(floor).toUpperCase() === 'DIGITAL'
|
||||
);
|
||||
@@ -348,12 +387,19 @@ const handlePostClassroom = async (req, res) => {
|
||||
code: room_number || `Aula-${Date.now()}`,
|
||||
capacity: (isVirtual || capacity === '∞' || capacity === '0' || !capacity) ? 0 : parseInt(capacity, 10),
|
||||
floor: floor || (isVirtual ? 'Digital' : 'PB'),
|
||||
building_id: bId,
|
||||
description: description || '',
|
||||
is_virtual: isVirtual,
|
||||
is_active: is_active === 'true' || is_active === 'on' || is_active === true
|
||||
};
|
||||
|
||||
if (bId && !isNaN(bId) && bId > 0) {
|
||||
payload.building_id = bId;
|
||||
} else if (bName) {
|
||||
payload.building_name = bName;
|
||||
} else {
|
||||
payload.building_name = isVirtual ? 'Campus Virtual' : 'Edificio Central';
|
||||
}
|
||||
|
||||
try {
|
||||
await req.apiClient.post('/classrooms', payload);
|
||||
} catch (apiErr) {
|
||||
|
||||
Reference in New Issue
Block a user