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 } }, })