Actualizar server.js
This commit is contained in:
34
server.js
34
server.js
@@ -2782,29 +2782,21 @@ 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
|
||||
// Buscamos expedientes donde la fecha del último mensaje de ADMIN sea más reciente que la última lectura del operario
|
||||
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]);
|
||||
SELECT DISTINCT s.id
|
||||
FROM scraped_services s
|
||||
JOIN service_communications c ON s.id = c.scraped_id
|
||||
WHERE s.assigned_to = $1
|
||||
AND c.sender_role IN ('admin', 'superadmin')
|
||||
AND c.is_internal = FALSE
|
||||
AND c.created_at > COALESCE(s.last_chat_read_worker, '2000-01-01')
|
||||
`, [req.user.sub]);
|
||||
|
||||
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 });
|
||||
res.json({ ok: true, unreadCount: q.rowCount, serviceIds: q.rows.map(r => r.id) });
|
||||
} catch (e) {
|
||||
console.error("Error notificaciones:", e);
|
||||
res.status(500).json({ ok: false });
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user