Actualizar server.js

This commit is contained in:
2026-03-11 10:35:32 +00:00
parent 5881a672b8
commit 018d853cd2

View File

@@ -1672,11 +1672,16 @@ app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
let oldStatus = String(rawActual.status_operativo || "");
let newStatus = String(extra.status_operativo !== undefined ? extra.status_operativo : oldStatus);
// 🕒 AÑADIDO: Control exhaustivo de Fechas Y Horas
const oldDate = String(rawActual.scheduled_date || "");
const newDate = String(extra.scheduled_date !== undefined ? extra.scheduled_date : oldDate);
const oldTime = String(rawActual.scheduled_time || "");
const newTime = String(extra.scheduled_time !== undefined ? extra.scheduled_time : oldTime);
const statusChanged = (newStatus !== oldStatus && newStatus !== "" && newStatus !== "null");
const dateChanged = (newDate !== oldDate && newDate !== "" && newDate !== "null");
const timeChanged = (newTime !== oldTime && newTime !== "" && newTime !== "null"); // <-- NUEVO
const oldWorkerId = current.rows[0].assigned_to || rawActual.assigned_to;
let finalAssignedTo = assigned_to !== undefined ? (assigned_to === "" ? null : assigned_to) : oldWorkerId;
@@ -1687,7 +1692,7 @@ app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
stName = (statusQ.rows[0]?.name || "").toLowerCase();
}
console.log(`🤖 [DEBUG ADMIN-PANEL] Exp: ${id} | Estado: '${stName}' | statusChanged: ${statusChanged} | dateChanged: ${dateChanged}`);
console.log(`🤖 [DEBUG ADMIN-PANEL] Exp: ${id} | Estado: '${stName}' | statusChanged: ${statusChanged} | dateChanged: ${dateChanged} | timeChanged: ${timeChanged}`);
// ACTUALIZAR BASE DE DATOS PRIMERO
const updatedRawData = {
@@ -1696,6 +1701,7 @@ app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
"Teléfono": phone || rawActual["Teléfono"],
"Dirección": address || rawActual["Dirección"],
"scheduled_date": newDate,
"scheduled_time": newTime,
"status_operativo": newStatus === "null" ? null : newStatus
};
@@ -1713,7 +1719,8 @@ app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
if (statusChanged && isAsignado && finalAssignedTo) {
triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_assigned').catch(console.error);
}
if (isCitado && (statusChanged || dateChanged)) {
// 🔔 Se dispara si cambia el estado a citado, si cambia el día o si cambia la hora
if (isCitado && (statusChanged || dateChanged || timeChanged)) {
if (oldDate === "" || oldDate === "null") triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_date').catch(console.error);
else triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_update').catch(console.error);
}
@@ -1727,8 +1734,8 @@ app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
console.log("✅ [ADMIN] Disparando robot HS: ASIGNACIÓN");
triggerHomeServeRobot(req.user.accountId, id, 'assign').catch(console.error);
}
if (isCitado && (statusChanged || dateChanged) && newDate !== "" && newDate !== "null") {
console.log(`✅ [ADMIN] Disparando robot HS: CITA (${newDate})`);
if (isCitado && (statusChanged || dateChanged || timeChanged) && newDate !== "" && newDate !== "null") {
console.log(`✅ [ADMIN] Disparando robot HS: CITA (${newDate} ${newTime})`);
triggerHomeServeRobot(req.user.accountId, id, 'date').catch(console.error);
}
if (isNoLocalizado && statusChanged) {
@@ -1741,8 +1748,8 @@ app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
console.log("✅ [ADMIN] Disparando robot MULTI: ASIGNACIÓN");
triggerMultiRobot(req.user.accountId, id, 'assign').catch(console.error);
}
if (isCitado && (statusChanged || dateChanged) && newDate !== "" && newDate !== "null") {
console.log(`✅ [ADMIN] Disparando robot MULTI: CITA (${newDate})`);
if (isCitado && (statusChanged || dateChanged || timeChanged) && newDate !== "" && newDate !== "null") {
console.log(`✅ [ADMIN] Disparando robot MULTI: CITA (${newDate} ${newTime})`);
triggerMultiRobot(req.user.accountId, id, 'date').catch(console.error);
}
if (isNoLocalizado && statusChanged) {