diff --git a/server.js b/server.js index 66d49ba..09592c9 100644 --- a/server.js +++ b/server.js @@ -640,6 +640,7 @@ app.get("/public/portal/:token", async (req, res) => { }); // 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 { const { token } = req.params; @@ -653,8 +654,7 @@ app.get("/public/portal/:token/slots", async (req, res) => { const userQ = await pool.query("SELECT portal_settings FROM users WHERE id = $1", [ownerId]); const pSet = userQ.rows[0]?.portal_settings || { m_start:"09:00", m_end:"14:00", a_start:"16:00", a_end:"19:00" }; - // Función para generar huecos cada 30 minutos - // Función para generar huecos cada 15 minutos + // Función para generar huecos cada 60 minutos (Ventanas de 1 hora) function genSlots(start, end) { if(!start || !end) return []; let s = []; @@ -662,9 +662,12 @@ app.get("/public/portal/:token/slots", async (req, res) => { let [eh, em] = end.split(':').map(Number); let cur = sh * 60 + sm; let limit = eh * 60 + em; - while(cur <= limit) { + + // Queremos ventanas de 1 hora completas. + // Si el límite es 15:00, el último hueco que puede empezar es a las 14:00. + while(cur + 60 <= limit) { s.push(`${String(Math.floor(cur/60)).padStart(2,'0')}:${String(cur%60).padStart(2,'0')}`); - cur += 15; // <--- CAMBIADO DE 30 A 15 MINUTOS + cur += 60; // Avanzamos de hora en hora } return s; } @@ -706,18 +709,32 @@ app.get("/public/portal/:token/slots", async (req, res) => { if (!agendaMap[row.date]) agendaMap[row.date] = { times: [], zone: (row.poblacion || row.cp || "").toLowerCase().trim() }; - // Bloqueamos la agenda en fracciones de 15 minutos reales + // Bloqueamos la agenda evaluando la duración estimada real del aviso/bloqueo const dur = parseInt(row.duration || 60); if (row.time) { let [th, tm] = row.time.split(':').map(Number); let startMin = th * 60 + tm; let endMin = startMin + dur; - // Bloquea cada tramo de 15 minutos que dure el servicio - for (let m = startMin; m < endMin; m += 15) { // <--- CAMBIADO DE 30 A 15 - let hStr = String(Math.floor(m/60)).padStart(2,'0'); - let mStr = String(m%60).padStart(2,'0'); - agendaMap[row.date].times.push(`${hStr}:${mStr}`); - } + + // Bloquea cualquier franja de 1 HORA que se solape con este aviso + // Como ahora generamos horas en punto (o y media, según config), chequeamos si el tramo de 60 mins pisa al servicio + morningBase.forEach(slot => { + let [sh, sm] = slot.split(':').map(Number); + let slotStart = sh * 60 + sm; + let slotEnd = slotStart + 60; + if(slotStart < endMin && slotEnd > startMin) { + agendaMap[row.date].times.push(slot); + } + }); + + afternoonBase.forEach(slot => { + let [sh, sm] = slot.split(':').map(Number); + let slotStart = sh * 60 + sm; + let slotEnd = slotStart + 60; + if(slotStart < endMin && slotEnd > startMin) { + agendaMap[row.date].times.push(slot); + } + }); } });