Actualizar server.js
This commit is contained in:
34
server.js
34
server.js
@@ -1541,7 +1541,7 @@ app.post("/providers/automate/:id", authMiddleware, async (req, res) => {
|
||||
});
|
||||
|
||||
// ==========================================
|
||||
// 📝 ACTUALIZACIÓN MANUAL (RUTA PRINCIPAL)
|
||||
// 📝 ACTUALIZACIÓN MANUAL (RUTA PRINCIPAL CORREGIDA CON LOGS)
|
||||
// ==========================================
|
||||
app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
|
||||
const { id } = req.params;
|
||||
@@ -1566,30 +1566,40 @@ app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
|
||||
|
||||
const oldDate = rawActual.scheduled_date || "";
|
||||
const newDate = extra.scheduled_date !== undefined ? extra.scheduled_date : oldDate;
|
||||
const dateChanged = newDate !== "" && newDate !== oldDate;
|
||||
const statusChanged = newStatus !== oldStatus;
|
||||
const dateChanged = (newDate !== "" && newDate !== oldDate);
|
||||
const statusChanged = (String(newStatus) !== String(oldStatus));
|
||||
|
||||
// Obtener nombre del estado para la lógica
|
||||
let stName = "";
|
||||
if (newStatus) {
|
||||
const statusQ = await pool.query("SELECT name FROM service_statuses WHERE id=$1", [newStatus]);
|
||||
stName = (statusQ.rows[0]?.name || "").toLowerCase();
|
||||
}
|
||||
|
||||
// WhatsApp Eventos
|
||||
console.log(`DEBUG HS: Exp ${id} | Estado: ${stName} | statusChanged: ${statusChanged} | dateChanged: ${dateChanged}`);
|
||||
|
||||
// --- DISPARADORES DE EVENTOS ---
|
||||
|
||||
// 1. WhatsApp Asignación
|
||||
if (stName.includes('asignado') && finalAssignedTo && statusChanged) {
|
||||
console.log("DEBUG HS: Disparando WA Asignación");
|
||||
await triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_assigned');
|
||||
}
|
||||
|
||||
// 2. WhatsApp Cita
|
||||
if ((statusChanged && stName.includes('citado') && newDate !== "") || (dateChanged && stName.includes('citado'))) {
|
||||
console.log("DEBUG HS: Disparando WA Cita");
|
||||
if (oldDate === "") await triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_date');
|
||||
else await triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_update');
|
||||
}
|
||||
|
||||
// 3. ACTUALIZAR BASE DE DATOS
|
||||
const updatedRawData = {
|
||||
...rawActual, ...extra,
|
||||
"Nombre Cliente": name || rawActual["Nombre Cliente"],
|
||||
"Teléfono": phone || rawActual["Teléfono"],
|
||||
"Dirección": address || rawActual["Dirección"],
|
||||
"scheduled_date": newDate,
|
||||
"status_operativo": newStatus
|
||||
};
|
||||
|
||||
@@ -1598,18 +1608,24 @@ app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
|
||||
[JSON.stringify(updatedRawData), finalAssignedTo, id, req.user.accountId]
|
||||
);
|
||||
|
||||
// Robot HomeServe
|
||||
if (statusChanged && stName.includes('asignado') && finalAssignedTo) {
|
||||
// 4. 🤖 DISPARAR ROBOT HOMESERVE (Lógica reforzada)
|
||||
const esCitado = stName.includes('citado') || stName.includes('cita');
|
||||
const esAsignado = stName.includes('asignado');
|
||||
|
||||
if (statusChanged && esAsignado && finalAssignedTo) {
|
||||
console.log("DEBUG HS: Enviando ASIGNACIÓN a la cola del Robot");
|
||||
triggerHomeServeRobot(req.user.accountId, id, 'assign').catch(console.error);
|
||||
}
|
||||
if ((statusChanged && stName.includes('citado')) || (dateChanged && stName.includes('citado'))) {
|
||||
|
||||
if ((statusChanged && esCitado && newDate !== "") || (dateChanged && esCitado)) {
|
||||
console.log(`DEBUG HS: Enviando CITA (${newDate}) a la cola del Robot`);
|
||||
triggerHomeServeRobot(req.user.accountId, id, 'date').catch(console.error);
|
||||
}
|
||||
|
||||
res.json({ ok: true });
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
res.status(500).json({ error: 'Error' });
|
||||
console.error("ERROR CRÍTICO EN PUT SCRAPED:", error);
|
||||
res.status(500).json({ error: 'Error interno' });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user