diff --git a/server.js b/server.js index 09592c9..b72aca5 100644 --- a/server.js +++ b/server.js @@ -639,7 +639,6 @@ app.get("/public/portal/:token", async (req, res) => { } catch (e) { res.status(500).json({ ok: false, error: "Error de servidor" }); } }); -// 2. Obtener huecos disponibles inteligentes (CON HORARIOS DINÁMICOS Y 30 MIN) // 2. Obtener huecos disponibles inteligentes (CON HORARIOS DINÁMICOS Y TRAMOS DE 1 HORA) app.get("/public/portal/:token/slots", async (req, res) => { try { @@ -778,6 +777,46 @@ app.get("/public/portal/:token/slots", async (req, res) => { } catch (e) { console.error("Error Slots:", e); res.status(500).json({ ok: false }); } }); + const availableDays = []; + let d = new Date(); + d.setDate(d.getDate() + 1); + + let daysAdded = 0; + while(daysAdded < 10) { + if (d.getDay() !== 0) { // Omitir domingos + const dateStr = d.toISOString().split('T')[0]; + const dayData = agendaMap[dateStr]; + let isDayAllowed = true; + + if (dayData && dayData.zone && targetZone) { + if (!dayData.zone.includes(targetZone) && !targetZone.includes(dayData.zone)) { + isDayAllowed = false; + } + } + + if (isDayAllowed) { + const takenTimes = dayData ? dayData.times : []; + // Filtramos nuestra plantilla contra los huecos ocupados + const availMorning = morningBase.filter(t => !takenTimes.includes(t)); + const availAfternoon = afternoonBase.filter(t => !takenTimes.includes(t)); + + if (availMorning.length > 0 || availAfternoon.length > 0) { + availableDays.push({ + date: dateStr, + displayDate: d.toLocaleDateString('es-ES', { weekday: 'long', day: 'numeric', month: 'long' }), + morning: availMorning, + afternoon: availAfternoon + }); + daysAdded++; + } + } + } + d.setDate(d.getDate() + 1); + } + res.json({ ok: true, days: availableDays }); + } catch (e) { console.error("Error Slots:", e); res.status(500).json({ ok: false }); } +}); + // 3. OBTENER SOLICITUDES PARA EL PANEL DEL ADMIN app.get("/agenda/requests", authMiddleware, async (req, res) => { try {