From 4f81f624292ce397f4e09a3ea0125536d1e23685 Mon Sep 17 00:00:00 2001 From: esdrasrenan Date: Sun, 7 Dec 2025 11:34:47 -0300 Subject: [PATCH] Fix unread badge - increment unreadByMachine when agent sends message MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The unreadByMachine counter was never being incremented when agents sent chat messages, causing the badge to always show 0. Now when an agent (ADMIN/MANAGER/AGENT) posts a message to a ticket with an active chat session, the counter is incremented properly. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- convex/tickets.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/convex/tickets.ts b/convex/tickets.ts index 3cc9ce4..ef4d5e2 100644 --- a/convex/tickets.ts +++ b/convex/tickets.ts @@ -3257,6 +3257,23 @@ export const postChatMessage = mutation({ await ctx.db.patch(ticketId, { updatedAt: now }) + // Se o autor for um agente (ADMIN, MANAGER, AGENT), incrementar unreadByMachine na sessao de chat ativa + const actorRole = participant.role?.toUpperCase() ?? "" + if (["ADMIN", "MANAGER", "AGENT"].includes(actorRole)) { + const activeSession = await ctx.db + .query("liveChatSessions") + .withIndex("by_ticket", (q) => q.eq("ticketId", ticketId)) + .filter((q) => q.eq(q.field("status"), "ACTIVE")) + .first() + + if (activeSession) { + await ctx.db.patch(activeSession._id, { + unreadByMachine: (activeSession.unreadByMachine ?? 0) + 1, + lastActivityAt: now, + }) + } + } + return { ok: true, messageId } }, })