Actualizar server.js
This commit is contained in:
78
server.js
78
server.js
@@ -600,7 +600,10 @@ async function triggerHomeServeRobot(ownerId, serviceId, eventType) {
|
|||||||
const settings = userQ.rows[0]?.wa_settings?.robot_homeserve || {};
|
const settings = userQ.rows[0]?.wa_settings?.robot_homeserve || {};
|
||||||
|
|
||||||
const rule = settings[eventType];
|
const rule = settings[eventType];
|
||||||
if (!rule || !rule.enabled) return; // Si la regla está apagada, abortamos.
|
if (!rule || !rule.enabled) {
|
||||||
|
console.log(`🤖 [ROBOT HS] Regla '${eventType}' desactivada. Omitiendo.`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const svcQ = await pool.query(`
|
const svcQ = await pool.query(`
|
||||||
SELECT s.service_ref, s.raw_data, u.full_name as worker_name
|
SELECT s.service_ref, s.raw_data, u.full_name as worker_name
|
||||||
@@ -613,52 +616,69 @@ async function triggerHomeServeRobot(ownerId, serviceId, eventType) {
|
|||||||
const s = svcQ.rows[0];
|
const s = svcQ.rows[0];
|
||||||
const raw = s.raw_data || {};
|
const raw = s.raw_data || {};
|
||||||
|
|
||||||
// 2. Calcular la fecha de Siguiente Acción
|
// 2. Calcular la fecha de Siguiente Acción (Formato exacto HomeServe DD/MM/YYYY)
|
||||||
let targetDate = new Date();
|
let targetDate = new Date();
|
||||||
|
let formattedDate = "";
|
||||||
|
|
||||||
if (eventType === 'date') {
|
if (eventType === 'date' && raw.scheduled_date) {
|
||||||
// 🎯 SI ES "CITA", usamos estrictamente la fecha en la que se ha agendado
|
// 🎯 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('-');
|
||||||
const [y, m, d] = raw.scheduled_date.split('-');
|
formattedDate = `${d.padStart(2, '0')}/${m.padStart(2, '0')}/${y}`;
|
||||||
targetDate = new Date(y, m - 1, d);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
// 📅 SI ES "ASIGNAR" o "NO LOCALIZADO", sumamos los días a partir de HOY saltando fines de semana
|
// 📅 SI ES "ASIGNAR" o "NO LOCALIZADO", sumamos los días a partir de HOY
|
||||||
let added = 0;
|
const daysToAdd = parseInt(rule.days_next) || 0;
|
||||||
while (added < (rule.days_next || 0)) {
|
targetDate.setDate(targetDate.getDate() + daysToAdd);
|
||||||
targetDate.setDate(targetDate.getDate() + 1);
|
|
||||||
if (targetDate.getDay() !== 0 && targetDate.getDay() !== 6) {
|
// Blindaje: Si la fecha resultante cae en fin de semana, la movemos al Lunes
|
||||||
added++;
|
if (targetDate.getDay() === 6) targetDate.setDate(targetDate.getDate() + 2); // Si es Sábado -> Suma 2 días (Lunes)
|
||||||
}
|
else if (targetDate.getDay() === 0) targetDate.setDate(targetDate.getDate() + 1); // Si es Domingo -> Suma 1 día (Lunes)
|
||||||
}
|
|
||||||
|
const dd = String(targetDate.getDate()).padStart(2, '0');
|
||||||
|
const mm = String(targetDate.getMonth() + 1).padStart(2, '0');
|
||||||
|
const yyyy = targetDate.getFullYear();
|
||||||
|
formattedDate = `${dd}/${mm}/${yyyy}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 || "";
|
let text = rule.obs || "";
|
||||||
text = text.replace(/{{NOMBRE}}/g, raw["Nombre Cliente"] || raw["CLIENTE"] || "Cliente");
|
const clientName = raw["Nombre Cliente"] || raw["CLIENTE"] || "Cliente";
|
||||||
text = text.replace(/{{FECHA}}/g, raw["scheduled_date"] ? raw["scheduled_date"].split('-').reverse().join('/') : "la fecha acordada");
|
let clientPhone = raw["Teléfono"] || raw["TELEFONO"] || raw["TELEFONOS"] || "";
|
||||||
text = text.replace(/{{HORA}}/g, raw["scheduled_time"] || "la hora acordada");
|
clientPhone = extractValidPhone(clientPhone);
|
||||||
|
|
||||||
let phone = raw["Teléfono"] || raw["TELEFONO"] || raw["TELEFONOS"] || "";
|
// Formateo humano de la fecha para el texto de observaciones
|
||||||
let cleanPhone = extractValidPhone(phone);
|
let textoFechaCita = "hoy";
|
||||||
text = text.replace(/{{TELEFONO}}/g, cleanPhone);
|
if (raw.scheduled_date) {
|
||||||
|
const [y, m, d] = raw.scheduled_date.split('-');
|
||||||
|
textoFechaCita = `${d.padStart(2, '0')}/${m.padStart(2, '0')}/${y}`;
|
||||||
|
} else {
|
||||||
|
// Si no hay cita agendada (Ej: No localizado), usamos la fecha de hoy para el texto
|
||||||
|
const hoy = new Date();
|
||||||
|
textoFechaCita = `${String(hoy.getDate()).padStart(2, '0')}/${String(hoy.getMonth() + 1).padStart(2, '0')}/${hoy.getFullYear()}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
text = text.replace(/{{NOMBRE}}/g, clientName);
|
||||||
|
text = text.replace(/{{FECHA}}/g, textoFechaCita);
|
||||||
|
|
||||||
|
// Si no hay hora (ej: No localizado), ponemos la hora actual
|
||||||
|
const horaActual = `${String(new Date().getHours()).padStart(2, '0')}:${String(new Date().getMinutes()).padStart(2, '0')}`;
|
||||||
|
text = text.replace(/{{HORA}}/g, raw["scheduled_time"] || horaActual);
|
||||||
|
|
||||||
|
text = text.replace(/{{TELEFONO}}/g, clientPhone);
|
||||||
text = text.replace(/{{REFERENCIA}}/g, s.service_ref);
|
text = text.replace(/{{REFERENCIA}}/g, s.service_ref);
|
||||||
text = text.replace(/{{NOMBRE_OPERARIO}}/g, s.worker_name || "el técnico");
|
text = text.replace(/{{NOMBRE_OPERARIO}}/g, s.worker_name || "el técnico");
|
||||||
|
|
||||||
// 4. Mandar la orden definitiva al Robot
|
// 4. ESCUDO ANTI-POPUP HOMESERVE (Mínimo 20 caracteres)
|
||||||
|
if (text.length < 25) {
|
||||||
|
text += " - Actualizado por el sistema.";
|
||||||
|
}
|
||||||
|
|
||||||
|
// 5. Mandar la orden definitiva al Robot
|
||||||
await pool.query(`
|
await pool.query(`
|
||||||
INSERT INTO robot_queue (owner_id, provider, service_number, new_status, appointment_date, observation, inform_client)
|
INSERT INTO robot_queue (owner_id, provider, service_number, new_status, appointment_date, observation, inform_client)
|
||||||
VALUES ($1, 'homeserve', $2, $3, $4, $5, $6)
|
VALUES ($1, 'homeserve', $2, $3, $4, $5, $6)
|
||||||
`, [ownerId, s.service_ref, rule.status, formattedDate, text, rule.check_inform]);
|
`, [ownerId, s.service_ref, rule.status, formattedDate, text, rule.check_inform]);
|
||||||
|
|
||||||
console.log(`🤖 [ROBOT HS] Orden enviada a la cola para exp ${s.service_ref} (Regla: ${eventType})`);
|
console.log(`🤖 [ROBOT HS] Orden enviada a la cola para exp ${s.service_ref} (Regla: ${eventType}) | Fecha: ${formattedDate} | Obs: "${text}"`);
|
||||||
|
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
console.error("❌ Error en puente HomeServe:", e);
|
console.error("❌ Error en puente HomeServe:", e);
|
||||||
|
|||||||
Reference in New Issue
Block a user