diff --git a/server.js b/server.js index 736092c..1a90d7f 100644 --- a/server.js +++ b/server.js @@ -2772,6 +2772,35 @@ app.get("/providers/credentials", authMiddleware, async (req, res) => { } }); +// 3. Obtener resumen de mensajes nuevos para el operario (Globo de Notificación) +app.get("/worker/notifications", authMiddleware, async (req, res) => { + try { + const accountId = req.user.accountId; + const userId = req.user.sub; + + if (!accountId || !userId) return res.json({ ok: false, unreadCount: 0, serviceIds: [] }); + + // Busca expedientes asignados a este operario que tengan mensajes de ADMIN en las últimas 24h + const q = await pool.query(` + SELECT DISTINCT scraped_id + FROM service_communications + WHERE owner_id = $1 + AND sender_role IN ('admin', 'superadmin') + AND created_at > NOW() - INTERVAL '24 hours' + AND scraped_id IN (SELECT id FROM scraped_services WHERE assigned_to = $2) + `, [accountId, userId]); + + res.json({ + ok: true, + unreadCount: q.rowCount, + serviceIds: q.rows.map(r => r.scraped_id) + }); + } catch (e) { + console.error("Error en notificaciones operario:", e); + res.status(500).json({ ok: false }); + } +}); + // ========================================== // 💬 CHAT Y COMUNICACIÓN INTERNA (TIPO iTRAMIT) // ==========================================