Actualizar server.js
This commit is contained in:
49
server.js
49
server.js
@@ -863,10 +863,10 @@ app.post("/providers/import/:id", authMiddleware, async (req, res) => {
|
|||||||
} finally { client.release(); }
|
} finally { client.release(); }
|
||||||
});
|
});
|
||||||
|
|
||||||
// AÑADIDO: CAPTURA COMPLETA DE DATOS (...EXTRA)
|
// AÑADIDO: CAPTURA COMPLETA DE DATOS (...EXTRA) Y REGLA WHATSAPP MANUAL
|
||||||
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;
|
||||||
const { 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;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (automation_status) {
|
if (automation_status) {
|
||||||
@@ -879,9 +879,35 @@ app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
|
|||||||
return res.json({ ok: true });
|
return res.json({ ok: true });
|
||||||
}
|
}
|
||||||
|
|
||||||
const current = await pool.query('SELECT raw_data FROM scraped_services WHERE id = $1 AND owner_id = $2', [id, req.user.accountId]);
|
const current = await pool.query('SELECT raw_data, assigned_to FROM scraped_services WHERE id = $1 AND owner_id = $2', [id, req.user.accountId]);
|
||||||
if (current.rows.length === 0) return res.status(404).json({ error: 'No encontrado' });
|
if (current.rows.length === 0) return res.status(404).json({ error: 'No encontrado' });
|
||||||
|
|
||||||
|
// --- NUEVA LÓGICA: DETECCIÓN DE ASIGNACIÓN MANUAL ---
|
||||||
|
const oldStatus = current.rows[0].raw_data.status_operativo;
|
||||||
|
let newStatus = extra.status_operativo || oldStatus;
|
||||||
|
|
||||||
|
// Si el estado operativo ha cambiado...
|
||||||
|
if (newStatus && newStatus !== oldStatus) {
|
||||||
|
const statusQ = await pool.query("SELECT name FROM service_statuses WHERE id=$1", [newStatus]);
|
||||||
|
const stName = (statusQ.rows[0]?.name || "").toLowerCase();
|
||||||
|
|
||||||
|
// Si lo has puesto manual a "Asignado"
|
||||||
|
if (stName.includes('asignado')) {
|
||||||
|
// Disparamos WA y esperamos a ver si tiene éxito
|
||||||
|
const waEnviadoExito = await triggerWhatsAppEvent(req.user.accountId, id, 'wa_evt_assigned');
|
||||||
|
|
||||||
|
if (waEnviadoExito) {
|
||||||
|
// Si llega bien, forzamos la base de datos para que avance a Esperando al Cliente
|
||||||
|
const estadoEsperando = await pool.query("SELECT id FROM service_statuses WHERE owner_id=$1 AND name='Esperando al Cliente' LIMIT 1", [req.user.accountId]);
|
||||||
|
if(estadoEsperando.rowCount > 0) {
|
||||||
|
newStatus = estadoEsperando.rows[0].id;
|
||||||
|
extra.status_operativo = newStatus; // Actualizamos la variable para que se guarde
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// ----------------------------------------------------
|
||||||
|
|
||||||
// Fusión total: conservamos los extra para no perder información
|
// Fusión total: conservamos los extra para no perder información
|
||||||
const updatedRawData = {
|
const updatedRawData = {
|
||||||
...current.rows[0].raw_data,
|
...current.rows[0].raw_data,
|
||||||
@@ -896,20 +922,25 @@ app.put('/providers/scraped/:id', authMiddleware, async (req, res) => {
|
|||||||
"assigned_to_name": assigned_to_name,
|
"assigned_to_name": assigned_to_name,
|
||||||
"internal_notes": internal_notes,
|
"internal_notes": internal_notes,
|
||||||
"client_notes": client_notes,
|
"client_notes": client_notes,
|
||||||
"Urgente": is_urgent ? "Sí" : "No"
|
"Urgente": is_urgent ? "Sí" : "No",
|
||||||
|
"status_operativo": newStatus
|
||||||
};
|
};
|
||||||
|
|
||||||
// ====== CORRECCIÓN AQUÍ: SINCRONIZACIÓN COLUMNA IS_URGENT ======
|
// ACTUALIZAMOS (Respetando el nuevo estado operativo y el nuevo operario asignado)
|
||||||
await pool.query(
|
await pool.query(
|
||||||
`UPDATE scraped_services
|
`UPDATE scraped_services
|
||||||
SET raw_data = $1,
|
SET raw_data = $1,
|
||||||
status = 'pending',
|
status = 'pending',
|
||||||
is_urgent = $2
|
is_urgent = $2,
|
||||||
WHERE id = $3 AND owner_id = $4`,
|
assigned_to = $3
|
||||||
[JSON.stringify(updatedRawData), is_urgent || false, id, req.user.accountId]
|
WHERE id = $4 AND owner_id = $5`,
|
||||||
|
[JSON.stringify(updatedRawData), is_urgent || false, assigned_to || current.rows[0].assigned_to, id, req.user.accountId]
|
||||||
);
|
);
|
||||||
res.json({ ok: true });
|
res.json({ ok: true });
|
||||||
} catch (error) { res.status(500).json({ error: 'Error' }); }
|
} catch (error) {
|
||||||
|
console.error("Error actualización manual:", error);
|
||||||
|
res.status(500).json({ error: 'Error' });
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
app.get("/discovery/keys/:provider", authMiddleware, async (req, res) => {
|
app.get("/discovery/keys/:provider", authMiddleware, async (req, res) => {
|
||||||
|
|||||||
Reference in New Issue
Block a user