Actualizar server.js
This commit is contained in:
49
server.js
49
server.js
@@ -437,31 +437,26 @@ app.get("/public/assignment/:token", async (req, res) => {
|
|||||||
} catch (e) { res.status(500).json({ ok: false }); }
|
} 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]);
|
await client.query("UPDATE assignment_pings SET status = 'accepted' WHERE id = $1", [ping.id]);
|
||||||
|
|
||||||
// Obtenemos el ID del estado "Asignado" del sistema
|
// 1. Obtenemos el owner para poder enviar el WA
|
||||||
const statusQ = await client.query(
|
const ownerQ = await client.query("SELECT owner_id FROM scraped_services WHERE id = $1", [ping.scraped_id]);
|
||||||
"SELECT id FROM service_statuses WHERE owner_id = (SELECT owner_id FROM scraped_services WHERE id = $1) AND name = 'Asignado' LIMIT 1",
|
const ownerId = ownerQ.rows[0].owner_id;
|
||||||
[ping.scraped_id]
|
|
||||||
);
|
|
||||||
const idAsignado = statusQ.rows[0]?.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(`
|
await client.query(`
|
||||||
UPDATE scraped_services
|
UPDATE scraped_services
|
||||||
SET status = 'imported',
|
SET status = 'imported',
|
||||||
@@ -469,19 +464,9 @@ app.post("/public/assignment/respond", async (req, res) => {
|
|||||||
assigned_to = $1,
|
assigned_to = $1,
|
||||||
raw_data = raw_data || jsonb_build_object('assigned_to', $1::int, 'status_operativo', $3::text)
|
raw_data = raw_data || jsonb_build_object('assigned_to', $1::int, 'status_operativo', $3::text)
|
||||||
WHERE id = $2
|
WHERE id = $2
|
||||||
`, [ping.user_id, ping.scraped_id, idAsignado]);
|
`, [ping.user_id, ping.scraped_id, idFinal]);
|
||||||
|
|
||||||
} else {
|
} 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)
|
// 🌐 RUTAS PÚBLICAS: PORTAL DEL CLIENTE (SIN FRICCIÓN)
|
||||||
|
|||||||
Reference in New Issue
Block a user