Actualizar server.js
This commit is contained in:
26
server.js
26
server.js
@@ -879,6 +879,7 @@ app.post("/providers/import/:id", authMiddleware, async (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// AÑADIDO Y BLINDADO: CAPTURA COMPLETA DE DATOS Y REGLA WHATSAPP MANUAL
|
// AÑADIDO Y BLINDADO: CAPTURA COMPLETA DE DATOS Y REGLA WHATSAPP MANUAL
|
||||||
|
// AÑADIDO Y BLINDADO: CAPTURA COMPLETA DE DATOS, REGLA WHATSAPP MANUAL Y DESASIGNACIÓN
|
||||||
app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
|
app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
|
||||||
const { id } = req.params;
|
const { id } = req.params;
|
||||||
let { automation_status, status, name, phone, address, cp, description, guild_id, assigned_to, assigned_to_name, internal_notes, client_notes, is_urgent, ...extra } = req.body;
|
let { automation_status, status, name, phone, address, cp, description, guild_id, assigned_to, assigned_to_name, internal_notes, client_notes, is_urgent, ...extra } = req.body;
|
||||||
@@ -902,12 +903,13 @@ app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
|
|||||||
let newStatus = extra.status_operativo || oldStatus;
|
let newStatus = extra.status_operativo || oldStatus;
|
||||||
if (newStatus === "") newStatus = null;
|
if (newStatus === "") newStatus = null;
|
||||||
|
|
||||||
// --- REGLA WA MANUAL ---
|
// --- REGLAS DE CAMBIO DE ESTADO MANUAL ---
|
||||||
if (newStatus && newStatus !== oldStatus) {
|
if (newStatus && newStatus !== oldStatus) {
|
||||||
const statusQ = await pool.query("SELECT name FROM service_statuses WHERE id=$1", [newStatus]);
|
const statusQ = await pool.query("SELECT name FROM service_statuses WHERE id=$1", [newStatus]);
|
||||||
const stName = (statusQ.rows[0]?.name || "").toLowerCase();
|
const stName = (statusQ.rows[0]?.name || "").toLowerCase();
|
||||||
|
|
||||||
if (stName.includes('asignado')) {
|
if (stName.includes('asignado')) {
|
||||||
|
// Regla Asignado -> Pasa a Esperando si se envía WA
|
||||||
const waEnviadoExito = await triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_assigned');
|
const waEnviadoExito = await triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_assigned');
|
||||||
if (waEnviadoExito) {
|
if (waEnviadoExito) {
|
||||||
const estadoEsperando = await pool.query("SELECT id FROM service_statuses WHERE owner_id=$1 AND name='Esperando al Cliente' LIMIT 1", [req.user.accountId]);
|
const estadoEsperando = await pool.query("SELECT id FROM service_statuses WHERE owner_id=$1 AND name='Esperando al Cliente' LIMIT 1", [req.user.accountId]);
|
||||||
@@ -916,6 +918,23 @@ app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
|
|||||||
extra.status_operativo = newStatus;
|
extra.status_operativo = newStatus;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else if (stName.includes('pendiente de asignar') || stName.includes('desasignado')) {
|
||||||
|
// Regla Desasignar -> Avisa al Operario y borra datos
|
||||||
|
const oldWorkerId = current.rows[0].assigned_to || current.rows[0].raw_data.assigned_to;
|
||||||
|
if (oldWorkerId) {
|
||||||
|
const workerQ = await pool.query("SELECT full_name, phone FROM users WHERE id=$1", [oldWorkerId]);
|
||||||
|
if (workerQ.rowCount > 0) {
|
||||||
|
const w = workerQ.rows[0];
|
||||||
|
const ref = current.rows[0].raw_data.service_ref || current.rows[0].raw_data["Referencia"] || id;
|
||||||
|
const msg = `⚠️ *AVISO DE DESASIGNACIÓN*\n\nHola ${w.full_name}, se te ha retirado el expediente *#${ref}*.\n\nYa no tienes que atender este servicio.`;
|
||||||
|
sendWhatsAppAuto(w.phone, msg, `cliente_${req.user.accountId}`, false).catch(console.error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Forzamos el borrado del operario
|
||||||
|
assigned_to = null;
|
||||||
|
assigned_to_name = null;
|
||||||
|
extra.assigned_to = null;
|
||||||
|
extra.assigned_to_name = null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -938,10 +957,9 @@ app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
|
|||||||
|
|
||||||
// --- BLINDAJE DE OPERARIO VACÍO (Evita el error 500) ---
|
// --- BLINDAJE DE OPERARIO VACÍO (Evita el error 500) ---
|
||||||
let finalAssignedTo = assigned_to;
|
let finalAssignedTo = assigned_to;
|
||||||
if (finalAssignedTo === "") finalAssignedTo = null;
|
if (finalAssignedTo === "" || finalAssignedTo === null) finalAssignedTo = null; // Queda libre
|
||||||
else if (!finalAssignedTo) finalAssignedTo = current.rows[0].assigned_to;
|
else if (finalAssignedTo === undefined) finalAssignedTo = current.rows[0].assigned_to; // Mantiene el que tenía
|
||||||
|
|
||||||
// Respetamos el estatus maestro original para que no desaparezca del tablero
|
|
||||||
let currentDbStatus = current.rows[0].status;
|
let currentDbStatus = current.rows[0].status;
|
||||||
|
|
||||||
await pool.query(
|
await pool.query(
|
||||||
|
|||||||
Reference in New Issue
Block a user