From f1833be1eaef41318e3c1e44596cea98345d8361 Mon Sep 17 00:00:00 2001 From: rever-tecnologia Date: Mon, 15 Dec 2025 16:30:45 -0300 Subject: [PATCH] fix(chat): melhora fixLegacySessions para buscar todas sessoes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Busca todas as sessoes, nao apenas por index - Corrige sessoes encerradas que tambem nao tem o campo - Adiciona log das sessoes problematicas encontradas 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- convex/liveChat.ts | 44 +++++++++++++++++++++++++++----------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/convex/liveChat.ts b/convex/liveChat.ts index 3b2c0f4..b68835e 100644 --- a/convex/liveChat.ts +++ b/convex/liveChat.ts @@ -902,28 +902,38 @@ export const fixLegacySessions = mutation({ handler: async (ctx) => { const now = Date.now() - // Buscar sessoes ativas que podem ter campos faltando - const activeSessions = await ctx.db + // Buscar TODAS as sessoes (nao so por index) para garantir que pegamos as problematicas + const allSessions = await ctx.db .query("liveChatSessions") - .withIndex("by_status_lastActivity", (q) => q.eq("status", "ACTIVE")) - .take(100) + .take(500) let fixed = 0 let ended = 0 + const problematic: string[] = [] - for (const session of activeSessions) { - // Se sessao nao tem lastAgentMessageAt, adiciona o valor de startedAt + for (const session of allSessions) { + // Se sessao nao tem lastAgentMessageAt, adiciona o valor if (session.lastAgentMessageAt === undefined) { - // Sessao muito antiga (mais de 24h) - encerrar - if (now - session.startedAt > 24 * 60 * 60 * 1000) { - await ctx.db.patch(session._id, { - status: "ENDED", - endedAt: now, - lastAgentMessageAt: session.lastActivityAt ?? session.startedAt, - }) - ended++ + problematic.push(session._id) + + if (session.status === "ACTIVE") { + // Sessao ativa muito antiga (mais de 24h) - encerrar + if (now - session.startedAt > 24 * 60 * 60 * 1000) { + await ctx.db.patch(session._id, { + status: "ENDED", + endedAt: now, + lastAgentMessageAt: session.lastActivityAt ?? session.startedAt, + }) + ended++ + } else { + // Sessao recente - apenas corrigir o campo + await ctx.db.patch(session._id, { + lastAgentMessageAt: session.lastActivityAt ?? session.startedAt, + }) + fixed++ + } } else { - // Sessao recente - apenas corrigir o campo + // Sessao ja encerrada - apenas adicionar o campo await ctx.db.patch(session._id, { lastAgentMessageAt: session.lastActivityAt ?? session.startedAt, }) @@ -932,8 +942,8 @@ export const fixLegacySessions = mutation({ } } - console.log(`fixLegacySessions: fixed=${fixed}, ended=${ended}`) - return { fixed, ended, total: activeSessions.length } + console.log(`fixLegacySessions: fixed=${fixed}, ended=${ended}, problematic=${problematic.join(",")}`) + return { fixed, ended, total: allSessions.length, problematic } }, })