fix(classrooms): corregir renderizado de aulas presenciales, metricas de ocupacion y asignacion de cursadas
This commit is contained in:
@@ -16,6 +16,9 @@ try {
|
||||
const nunjucksRuntime = require('nunjucks/src/runtime');
|
||||
const origMemberLookup = nunjucksRuntime.memberLookup;
|
||||
nunjucksRuntime.memberLookup = function(obj, val) {
|
||||
if (obj && val === 'items' && Array.isArray(obj.items)) {
|
||||
return obj.items;
|
||||
}
|
||||
if (obj && val === 'get' && typeof obj.get !== 'function') {
|
||||
return function(key, defaultVal) {
|
||||
return (obj[key] !== undefined && obj[key] !== null) ? obj[key] : defaultVal;
|
||||
|
||||
@@ -66,13 +66,59 @@ const handleClassroomsList = async (req, res) => {
|
||||
month_end: new Date(Date.now() + 30 * 86400000).toISOString().split('T')[0]
|
||||
};
|
||||
|
||||
// Format classrooms with fallback fields
|
||||
const formattedItems = filtered.map(c => ({
|
||||
...c,
|
||||
building_name: c.building_name || c.building || 'Edificio Central',
|
||||
floor_display: c.floor_display || (c.floor === '0' || c.floor === 'PB' ? 'Planta Baja' : `Piso ${c.floor}`),
|
||||
resources: c.resources || []
|
||||
}));
|
||||
let allReservations = [];
|
||||
try {
|
||||
const vRes = await req.apiClient.get('/reservations', {
|
||||
params: { start_date: '2026-08-31', end_date: '2026-10-01' }
|
||||
});
|
||||
allReservations = (vRes.data && vRes.data.reservations) || [];
|
||||
} catch (vErr) {
|
||||
console.warn('Error fetching reservations:', vErr.message);
|
||||
}
|
||||
|
||||
const occupancy_metrics = {};
|
||||
|
||||
// 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 commMap = new Map();
|
||||
c_res.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,
|
||||
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_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));
|
||||
|
||||
occupancy_metrics[c.id] = {
|
||||
day_pct,
|
||||
day_count: day_res.length,
|
||||
week_pct,
|
||||
week_count: week_res.length,
|
||||
month_pct,
|
||||
month_count: c_res.length
|
||||
};
|
||||
|
||||
return {
|
||||
...c,
|
||||
building_name: c.building_name || c.building || 'Edificio Central',
|
||||
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
|
||||
};
|
||||
});
|
||||
|
||||
let virtual_rooms = [];
|
||||
let global_virtual_metrics = {
|
||||
@@ -88,10 +134,6 @@ const handleClassroomsList = async (req, res) => {
|
||||
|
||||
if (view_mode === 'virtual') {
|
||||
try {
|
||||
const vRes = await req.apiClient.get('/reservations', {
|
||||
params: { start_date: '2026-08-31', end_date: '2026-10-01' }
|
||||
});
|
||||
const allReservations = (vRes.data && vRes.data.reservations) || [];
|
||||
const virtualReservations = allReservations.filter(r => Boolean(r.is_virtual || (r.classroom && r.classroom.is_virtual)));
|
||||
|
||||
const commMap = new Map();
|
||||
@@ -196,7 +238,7 @@ const handleClassroomsList = async (req, res) => {
|
||||
virtual_count,
|
||||
buildings: Array.from(buildingsMap.values()),
|
||||
floors: Array.from(floorsSet).sort(),
|
||||
occupancy_metrics: { get: (id, defVal) => defVal },
|
||||
occupancy_metrics,
|
||||
date_filters: dateFilters,
|
||||
virtual_rooms,
|
||||
global_virtual_metrics,
|
||||
@@ -358,13 +400,14 @@ const handleViewClassroom = async (req, res) => {
|
||||
|
||||
// Fetch upcoming reservations for this classroom
|
||||
try {
|
||||
const rRes = await req.apiClient.get(`/reservations?classroom_id=${classroomId}`);
|
||||
upcomingReservations = (rRes.data && (rRes.data.reservations || rRes.data)) || [];
|
||||
// Only show future ones
|
||||
const now = new Date().toISOString();
|
||||
upcomingReservations = upcomingReservations
|
||||
.filter(r => r.start_time >= now)
|
||||
.slice(0, 10);
|
||||
const rRes = await req.apiClient.get(`/reservations?classroom_id=${classroomId}&start_date=2026-08-31&end_date=2026-10-01`);
|
||||
const allClassroomRes = (rRes.data && (rRes.data.reservations || rRes.data)) || [];
|
||||
upcomingReservations = allClassroomRes.map(r => ({
|
||||
...r,
|
||||
date_display: r.start_time ? r.start_time.split('T')[0] : '',
|
||||
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, 20);
|
||||
} catch (e) {
|
||||
console.warn('Could not fetch reservations for classroom:', e.message);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user