From f26bfeeb6634bc902dfb05f45fd45879129ed6b2 Mon Sep 17 00:00:00 2001 From: marsalva Date: Sat, 21 Feb 2026 10:46:46 +0000 Subject: [PATCH] Actualizar server.js --- server.js | 51 ++++++++++++++++++--------------------------------- 1 file changed, 18 insertions(+), 33 deletions(-) diff --git a/server.js b/server.js index adac134..04e366a 100644 --- a/server.js +++ b/server.js @@ -437,31 +437,26 @@ app.get("/public/assignment/:token", async (req, res) => { } catch (e) { res.status(500).json({ ok: false }); } }); -app.post("/public/assignment/respond", async (req, res) => { - const client = await pool.connect(); - try { - const { token, action } = req.body; - await client.query('BEGIN'); - - const q = await client.query( - "SELECT *, CURRENT_TIMESTAMP as db_now FROM assignment_pings WHERE token = $1 FOR UPDATE", - [token] - ); - - if (q.rowCount === 0) throw new Error("Enlace no válido"); - const ping = q.rows[0]; - - if (action === 'accept') { +if (action === 'accept') { await client.query("UPDATE assignment_pings SET status = 'accepted' WHERE id = $1", [ping.id]); - // Obtenemos el ID del estado "Asignado" del sistema - const statusQ = await client.query( - "SELECT id FROM service_statuses WHERE owner_id = (SELECT owner_id FROM scraped_services WHERE id = $1) AND name = 'Asignado' LIMIT 1", - [ping.scraped_id] - ); - const idAsignado = statusQ.rows[0]?.id; + // 1. Obtenemos el owner para poder enviar el WA + const ownerQ = await client.query("SELECT owner_id FROM scraped_services WHERE id = $1", [ping.scraped_id]); + const ownerId = ownerQ.rows[0].owner_id; - // Actualizamos el servicio: Operario asignado y Estado "Asignado" + // 2. DISPARAMOS EL WHATSAPP Y ESPERAMOS EL RESULTADO + const waEnviadoExito = await triggerWhatsAppEvent(ownerId, ping.scraped_id, 'wa_evt_assigned'); + + // 3. REGLA ESTRICTA: Decidimos el estado según el éxito del mensaje + const estadoFinalNombre = waEnviadoExito ? 'Esperando al Cliente' : 'Asignado'; + + const statusQ = await client.query( + "SELECT id FROM service_statuses WHERE owner_id = $1 AND name = $2 LIMIT 1", + [ownerId, estadoFinalNombre] + ); + const idFinal = statusQ.rows[0]?.id; + + // 4. Actualizamos el servicio con el operario y el estado correcto await client.query(` UPDATE scraped_services SET status = 'imported', @@ -469,19 +464,9 @@ app.post("/public/assignment/respond", async (req, res) => { assigned_to = $1, raw_data = raw_data || jsonb_build_object('assigned_to', $1::int, 'status_operativo', $3::text) WHERE id = $2 - `, [ping.user_id, ping.scraped_id, idAsignado]); + `, [ping.user_id, ping.scraped_id, idFinal]); } else { - await client.query("UPDATE assignment_pings SET status = 'rejected', expires_at = CURRENT_TIMESTAMP WHERE id = $1", [ping.id]); - } - - await client.query('COMMIT'); - res.json({ ok: true }); - } catch (e) { - await client.query('ROLLBACK'); - res.status(400).json({ ok: false, error: e.message }); - } finally { client.release(); } -}); // ========================================== // 🌐 RUTAS PÚBLICAS: PORTAL DEL CLIENTE (SIN FRICCIÓN)