fix(classrooms): excluir reservas pasadas de próximas clases y cursadas activas (v3.8.11)

This commit is contained in:
2026-10-26 11:20:08 -03:00
parent 452ff8098a
commit bbaab0b1d7
9 changed files with 146 additions and 40 deletions
+3 -3
View File
@@ -86,9 +86,9 @@ const env = nunjucks.configure(path.join(__dirname, '../views'), {
});
// Versiones institucionales del sistema
const APP_VERSION = '3.8.10';
const FRONTEND_VERSION = '3.8.10';
const BACKEND_VERSION = '3.8.10';
const APP_VERSION = '3.8.11';
const FRONTEND_VERSION = '3.8.11';
const BACKEND_VERSION = '3.8.11';
env.addGlobal('app_version', APP_VERSION);
env.addGlobal('frontend_version', FRONTEND_VERSION);
+75 -25
View File
@@ -122,6 +122,24 @@ const handleClassroomsList = async (req, res) => {
}
}
// 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];
// Normalizar campos de salas virtuales para garantizar compatibilidad con plantillas Nunjucks
virtual_rooms = virtual_rooms.map(room => ({
...room,
@@ -130,7 +148,21 @@ const handleClassroomsList = async (req, res) => {
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 || []
reservations: (room.reservations || []).map(r => {
const sStr = String(r.start_time || '');
const eStr = String(r.end_time || '');
const dPart = sStr.includes('T') ? sStr.split('T')[0] : (sStr.split(' ')[0] || '');
const isPast = dPart < todayIso;
const sTime = sStr.includes('T') ? sStr.split('T')[1].substring(0, 5) : (sStr.split(' ')[1] ? sStr.split(' ')[1].substring(0, 5) : sStr);
const eTime = eStr.includes('T') ? eStr.split('T')[1].substring(0, 5) : (eStr.split(' ')[1] ? eStr.split(' ')[1].substring(0, 5) : eStr);
return {
...r,
date_clean: dPart,
time_clean: `${sTime} a ${eTime} hs`,
is_past: isPast,
status_display: isPast ? 'Finalizada' : (r.status || 'CONFIRMED')
};
})
}));
// Si la búsqueda está activa en modo virtual, filtrar el catálogo de salas virtuales
@@ -150,24 +182,6 @@ const handleClassroomsList = async (req, res) => {
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: todayIso,
week_start: weekStartIso,
@@ -194,8 +208,18 @@ const handleClassroomsList = async (req, res) => {
// 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)))));
// Filtrar únicamente reservas activas y vigentes (de hoy en adelante) para las cursadas asignadas del aula
const nowIso = now.toISOString();
const active_c_res = c_res.filter(r => {
const rawEnd = r.end_time ? String(r.end_time).replace(' ', 'T') : '';
const rawStart = r.start_time ? String(r.start_time).replace(' ', 'T') : '';
const datePart = rawStart.split('T')[0];
return datePart >= todayIso || (rawEnd && rawEnd >= nowIso);
});
const commMap = new Map();
c_res.forEach(r => {
active_c_res.forEach(r => {
const cid = r.commission_id || (r.commission && r.commission.id) || r.id;
if (!commMap.has(cid)) {
commMap.set(cid, {
@@ -237,7 +261,8 @@ const handleClassroomsList = async (req, res) => {
floor_display: c.floor_display || (c.floor === '0' || c.floor === 'PB' ? 'Planta Baja' : `Piso ${c.floor}`),
resources: c.resources || [],
assigned_commissions: Array.from(commMap.values()),
total_classes: c_res.length
total_classes: active_c_res.length,
historical_classes: c_res.length
};
});
@@ -440,6 +465,7 @@ const handleViewClassroom = async (req, res) => {
try {
let classroom = null;
let upcomingReservations = [];
let pastReservations = [];
try {
const cRes = await req.apiClient.get(`/classrooms/${classroomId}`);
@@ -468,13 +494,17 @@ const handleViewClassroom = async (req, res) => {
classroom.code_display = classroom.code_display || classroom.code || `Aula-${classroomId}`;
classroom.building_entity = classroom.building_entity || null;
// Fetch upcoming reservations for this classroom (encompassing full 2026 academic term)
// Fetch upcoming and past reservations for this classroom
try {
const now = new Date();
const todayIso = now.toISOString().split('T')[0];
const nowIso = now.toISOString();
const rRes = await req.apiClient.get(`/reservations?classroom_id=${classroomId}&start_date=2026-08-01&end_date=2026-12-31`);
const allClassroomRes = (rRes.data && (rRes.data.reservations || rRes.data)) || [];
const dayNames = ['Domingo', 'Lunes', 'Martes', 'Miércoles', 'Jueves', 'Viernes', 'Sábado'];
upcomingReservations = allClassroomRes.map(r => {
const formatReservationItem = (r) => {
let dateDisplayFriendly = '';
let timeRangeFriendly = '';
let fullDisplay = '';
@@ -514,7 +544,26 @@ const handleViewClassroom = async (req, res) => {
start_time_display: r.start_time ? (r.start_time.includes('T') ? r.start_time.split('T')[1].substring(0, 5) : r.start_time) : '',
end_time_display: r.end_time ? (r.end_time.includes('T') ? r.end_time.split('T')[1].substring(0, 5) : r.end_time) : ''
};
}).sort((a, b) => (a.start_time || '').localeCompare(b.start_time || '')).slice(0, 30);
};
// Filtrar estrictamente Próximas Reservas (de hoy en adelante o no finalizadas)
const upcomingRaw = allClassroomRes.filter(r => {
const rawEnd = r.end_time ? String(r.end_time).replace(' ', 'T') : '';
const rawStart = r.start_time ? String(r.start_time).replace(' ', 'T') : '';
const datePart = rawStart.split('T')[0];
return datePart >= todayIso || (rawEnd && rawEnd >= nowIso);
});
// Reservas pasadas (ya concluidas en fechas anteriores)
const pastRaw = allClassroomRes.filter(r => {
const rawEnd = r.end_time ? String(r.end_time).replace(' ', 'T') : '';
const rawStart = r.start_time ? String(r.start_time).replace(' ', 'T') : '';
const datePart = rawStart.split('T')[0];
return datePart < todayIso && (!rawEnd || rawEnd < nowIso);
});
upcomingReservations = upcomingRaw.map(formatReservationItem).sort((a, b) => (a.start_time || '').localeCompare(b.start_time || '')).slice(0, 30);
pastReservations = pastRaw.map(formatReservationItem).sort((a, b) => (b.start_time || '').localeCompare(a.start_time || '')).slice(0, 30);
} catch (e) {
console.warn('Could not fetch reservations for classroom:', e.message);
}
@@ -522,7 +571,8 @@ const handleViewClassroom = async (req, res) => {
res.render('classrooms/view', {
title: `${classroom.code_display} - Edu-Space Admin`,
classroom,
upcoming_reservations: upcomingReservations
upcoming_reservations: upcomingReservations,
past_reservations: pastReservations
});
} catch (error) {
console.error('Error in view classroom:', error.message);