fix(classrooms): sincronizar salas virtuales con dashboard y dinamizar fechas (v3.8.10)
This commit is contained in:
+116
-111
@@ -72,21 +72,117 @@ const handleClassroomsList = async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// 1. Obtención consolidada de salas virtuales desde la API backend
|
||||
let virtual_rooms = [];
|
||||
let global_virtual_metrics = {
|
||||
today_count: 0,
|
||||
week_count: 0,
|
||||
month_count: 0,
|
||||
commissions_count: 0,
|
||||
total_count: 0
|
||||
};
|
||||
let virtual_count = 0;
|
||||
|
||||
try {
|
||||
const vRoomsRes = await req.apiClient.get('/classrooms/virtual-rooms');
|
||||
if (vRoomsRes.data && vRoomsRes.data.virtual_rooms) {
|
||||
virtual_rooms = vRoomsRes.data.virtual_rooms;
|
||||
virtual_count = vRoomsRes.data.virtual_classrooms_count || virtual_rooms.length;
|
||||
global_virtual_metrics = vRoomsRes.data.metrics || {
|
||||
today_count: 0,
|
||||
week_count: 0,
|
||||
month_count: 0,
|
||||
commissions_count: virtual_rooms.length,
|
||||
total_count: 0
|
||||
};
|
||||
}
|
||||
} catch (vErr) {
|
||||
console.warn('Notice fetching virtual rooms from /classrooms/virtual-rooms:', vErr.message);
|
||||
}
|
||||
|
||||
// Fallback a /dashboard/stats si /classrooms/virtual-rooms no devolvió salas
|
||||
if (virtual_rooms.length === 0) {
|
||||
try {
|
||||
const dashRes = await req.apiClient.get('/dashboard/stats');
|
||||
const bData = (dashRes.data && (dashRes.data.bedelia || dashRes.data)) || {};
|
||||
const vList = bData.virtual_rooms || [];
|
||||
if (vList.length > 0) {
|
||||
virtual_rooms = vList;
|
||||
virtual_count = vList.length;
|
||||
global_virtual_metrics = {
|
||||
today_count: vList.reduce((acc, r) => acc + (r.today_classes || 0), 0),
|
||||
week_count: vList.reduce((acc, r) => acc + (r.week_classes || 0), 0),
|
||||
month_count: vList.reduce((acc, r) => acc + (r.month_classes || 0), 0),
|
||||
commissions_count: vList.length,
|
||||
total_count: vList.reduce((acc, r) => acc + (r.res_count || 0), 0)
|
||||
};
|
||||
}
|
||||
} catch (dErr) {
|
||||
console.warn('Fallback fetching dashboard stats notice:', dErr.message);
|
||||
}
|
||||
}
|
||||
|
||||
// Normalizar campos de salas virtuales para garantizar compatibilidad con plantillas Nunjucks
|
||||
virtual_rooms = virtual_rooms.map(room => ({
|
||||
...room,
|
||||
commission_code: room.commission_code || room.code || 'C1',
|
||||
day_count: room.day_count !== undefined ? room.day_count : (room.today_classes || 0),
|
||||
week_count: room.week_count !== undefined ? room.week_count : (room.week_classes || 0),
|
||||
month_count: room.month_count !== undefined ? room.month_count : (room.month_classes || 0),
|
||||
total_count: room.total_count !== undefined ? room.total_count : (room.res_count || 0),
|
||||
reservations: room.reservations || []
|
||||
}));
|
||||
|
||||
// Si la búsqueda está activa en modo virtual, filtrar el catálogo de salas virtuales
|
||||
if (search && view_mode === 'virtual') {
|
||||
virtual_rooms = virtual_rooms.filter(room =>
|
||||
((room.subject_name || '').toLowerCase().includes(search)) ||
|
||||
((room.code || '').toLowerCase().includes(search)) ||
|
||||
((room.full_code || '').toLowerCase().includes(search)) ||
|
||||
((room.subject_code || '').toLowerCase().includes(search)) ||
|
||||
((room.career_name || '').toLowerCase().includes(search)) ||
|
||||
((room.teacher_name || '').toLowerCase().includes(search))
|
||||
);
|
||||
}
|
||||
|
||||
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;
|
||||
if (virtual_count === 0) {
|
||||
virtual_count = allClassrooms.filter(c => Boolean(c.is_virtual || (c.code || '').includes('VIRTUAL'))).length;
|
||||
}
|
||||
|
||||
// 2. Fechas operativas dinámicas calculadas desde el momento actual
|
||||
const now = new Date();
|
||||
const todayIso = now.toISOString().split('T')[0];
|
||||
|
||||
const dayOfWeek = now.getDay();
|
||||
const diffToMonday = (dayOfWeek === 0 ? -6 : 1) - dayOfWeek;
|
||||
const monday = new Date(now);
|
||||
monday.setDate(now.getDate() + diffToMonday);
|
||||
const sunday = new Date(monday);
|
||||
sunday.setDate(monday.getDate() + 6);
|
||||
const weekStartIso = monday.toISOString().split('T')[0];
|
||||
const weekEndIso = sunday.toISOString().split('T')[0];
|
||||
|
||||
const firstDayOfMonth = new Date(now.getFullYear(), now.getMonth(), 1);
|
||||
const lastDayOfMonth = new Date(now.getFullYear(), now.getMonth() + 1, 0);
|
||||
const monthStartIso = firstDayOfMonth.toISOString().split('T')[0];
|
||||
const monthEndIso = lastDayOfMonth.toISOString().split('T')[0];
|
||||
|
||||
const dateFilters = {
|
||||
today: new Date().toISOString().split('T')[0],
|
||||
week_start: new Date().toISOString().split('T')[0],
|
||||
week_end: new Date(Date.now() + 7 * 86400000).toISOString().split('T')[0],
|
||||
month_start: new Date().toISOString().split('T')[0],
|
||||
month_end: new Date(Date.now() + 30 * 86400000).toISOString().split('T')[0]
|
||||
today: todayIso,
|
||||
week_start: weekStartIso,
|
||||
week_end: weekEndIso,
|
||||
month_start: monthStartIso,
|
||||
month_end: monthEndIso
|
||||
};
|
||||
|
||||
// 3. Consultar reservas de un período cuatrimestral amplio (±90 días)
|
||||
let allReservations = [];
|
||||
try {
|
||||
const qStart = new Date(now.getFullYear(), now.getMonth() - 2, 1).toISOString().split('T')[0];
|
||||
const qEnd = new Date(now.getFullYear(), now.getMonth() + 3, 0).toISOString().split('T')[0];
|
||||
const vRes = await req.apiClient.get('/reservations', {
|
||||
params: { start_date: '2026-08-31', end_date: '2026-10-01' }
|
||||
params: { start_date: qStart, end_date: qEnd }
|
||||
});
|
||||
allReservations = (vRes.data && vRes.data.reservations) || [];
|
||||
} catch (vErr) {
|
||||
@@ -95,7 +191,7 @@ const handleClassroomsList = async (req, res) => {
|
||||
|
||||
const occupancy_metrics = {};
|
||||
|
||||
// Format classrooms with fallback fields and assigned commissions/classes
|
||||
// Formatear aulas físicas y calcular métricas de ocupación en base al calendario activo
|
||||
const formattedItems = filtered.map(c => {
|
||||
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();
|
||||
@@ -112,11 +208,19 @@ const handleClassroomsList = async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
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_res = c_res.filter(r => (r.start_time || '').startsWith(todayIso));
|
||||
const week_res = c_res.filter(r => {
|
||||
const st = (r.start_time || '').split('T')[0];
|
||||
return st >= weekStartIso && st <= weekEndIso;
|
||||
});
|
||||
const month_res = c_res.filter(r => {
|
||||
const st = (r.start_time || '').split('T')[0];
|
||||
return st >= monthStartIso && st <= monthEndIso;
|
||||
});
|
||||
|
||||
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));
|
||||
const month_pct = Math.min(100, Math.round((month_res.length / 72) * 100));
|
||||
|
||||
occupancy_metrics[c.id] = {
|
||||
day_pct,
|
||||
@@ -124,7 +228,7 @@ const handleClassroomsList = async (req, res) => {
|
||||
week_pct,
|
||||
week_count: week_res.length,
|
||||
month_pct,
|
||||
month_count: c_res.length
|
||||
month_count: month_res.length || c_res.length
|
||||
};
|
||||
|
||||
return {
|
||||
@@ -137,108 +241,9 @@ const handleClassroomsList = async (req, res) => {
|
||||
};
|
||||
});
|
||||
|
||||
let virtual_rooms = [];
|
||||
let global_virtual_metrics = {
|
||||
today_count: 0,
|
||||
week_count: 0,
|
||||
month_count: 0,
|
||||
commissions_count: 0,
|
||||
total_count: 0
|
||||
};
|
||||
|
||||
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 {
|
||||
const virtualReservations = allReservations.filter(r => Boolean(r.is_virtual || (r.classroom && r.classroom.is_virtual)));
|
||||
|
||||
const commMap = new Map();
|
||||
const todayStr = '2026-09-18';
|
||||
const weekStart = new Date('2026-09-14');
|
||||
const weekEnd = new Date('2026-09-20');
|
||||
const monthStart = new Date('2026-09-01');
|
||||
const monthEnd = new Date('2026-09-30');
|
||||
|
||||
virtualReservations.forEach(r => {
|
||||
const cid = r.commission_id || (r.commission && r.commission.id) || r.id;
|
||||
if (!commMap.has(cid)) {
|
||||
commMap.set(cid, {
|
||||
commission_id: cid,
|
||||
commission_code: (r.commission && r.commission.code) || 'C1',
|
||||
subject_name: r.subject_name || r.purpose || 'Clase Virtual',
|
||||
subject_code: r.subject_code || '',
|
||||
career_name: 'UniCABA',
|
||||
max_students: r.expected_attendees || 35,
|
||||
virtual_link: r.effective_virtual_link || r.virtual_link || '',
|
||||
reservations: []
|
||||
});
|
||||
}
|
||||
commMap.get(cid).reservations.push(r);
|
||||
});
|
||||
|
||||
virtual_rooms = Array.from(commMap.values()).map(data => {
|
||||
const resList = data.reservations;
|
||||
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;
|
||||
const shift = (resList[0] && resList[0].shift) || 'Tarde';
|
||||
|
||||
return {
|
||||
commission_id: data.commission_id,
|
||||
commission_code: data.commission_code,
|
||||
subject_name: data.subject_name,
|
||||
subject_code: data.subject_code,
|
||||
career_name: data.career_name,
|
||||
max_students: data.max_students,
|
||||
virtual_link: data.virtual_link,
|
||||
shift,
|
||||
day_count: dayCnt,
|
||||
week_count: weekCnt,
|
||||
month_count: monthCnt,
|
||||
total_count: resList.length,
|
||||
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)
|
||||
);
|
||||
}
|
||||
|
||||
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,
|
||||
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,
|
||||
commissions_count: commMap.size,
|
||||
total_count: virtualReservations.length
|
||||
};
|
||||
} catch (vErr) {
|
||||
console.warn('Error aggregating virtual rooms:', vErr.message);
|
||||
}
|
||||
}
|
||||
|
||||
res.render('classrooms/list', {
|
||||
title: 'Aulas y Espacios - Edu-Space Admin',
|
||||
classrooms: {
|
||||
|
||||
Reference in New Issue
Block a user