diff --git a/server.js b/server.js index 261747d..6c3301f 100644 --- a/server.js +++ b/server.js @@ -613,15 +613,17 @@ async function triggerHomeServeRobot(ownerId, serviceId, eventType) { const s = svcQ.rows[0]; const raw = s.raw_data || {}; - // 2. Calcular la fecha de Siguiente Acción (SALTANDO FINES DE SEMANA) + // 2. Calcular la fecha de Siguiente Acción let targetDate = new Date(); - if (eventType === 'date' && rule.days_next === 0 && raw.scheduled_date) { - // Si es "Cita" y pusiste 0 días, usa la fecha exacta de la cita - const [y, m, d] = raw.scheduled_date.split('-'); - targetDate = new Date(y, m - 1, d); + if (eventType === 'date') { + // 🎯 SI ES "CITA", usamos estrictamente la fecha en la que se ha agendado + if (raw.scheduled_date) { + const [y, m, d] = raw.scheduled_date.split('-'); + targetDate = new Date(y, m - 1, d); + } } else { - // Sumar días hábiles reales + // 📅 SI ES "ASIGNAR" o "NO LOCALIZADO", sumamos los días a partir de HOY saltando fines de semana let added = 0; while (added < (rule.days_next || 0)) { targetDate.setDate(targetDate.getDate() + 1); @@ -631,19 +633,18 @@ async function triggerHomeServeRobot(ownerId, serviceId, eventType) { } } - // Blindaje extra: Si cae en sábado (6) o domingo (0), lo pasamos al lunes + // Blindaje extra anti-fines de semana (por si la cita o el salto caen en Sábado o Domingo) if (targetDate.getDay() === 6) targetDate.setDate(targetDate.getDate() + 2); if (targetDate.getDay() === 0) targetDate.setDate(targetDate.getDate() + 1); const formattedDate = `${String(targetDate.getDate()).padStart(2, '0')}/${String(targetDate.getMonth() + 1).padStart(2, '0')}/${targetDate.getFullYear()}`; - // 3. Traducir variables personalizadas + // 3. Traducir variables personalizadas let text = rule.obs || ""; text = text.replace(/{{NOMBRE}}/g, raw["Nombre Cliente"] || raw["CLIENTE"] || "Cliente"); text = text.replace(/{{FECHA}}/g, raw["scheduled_date"] ? raw["scheduled_date"].split('-').reverse().join('/') : "la fecha acordada"); text = text.replace(/{{HORA}}/g, raw["scheduled_time"] || "la hora acordada"); - // 👇 AHORA USA EL EXTRACTOR INTELIGENTE DE TELÉFONOS let phone = raw["Teléfono"] || raw["TELEFONO"] || raw["TELEFONOS"] || ""; let cleanPhone = extractValidPhone(phone); text = text.replace(/{{TELEFONO}}/g, cleanPhone);