From f451ca2e3b224b2bad5b4561769996f87cd257f1 Mon Sep 17 00:00:00 2001 From: rever-tecnologia Date: Mon, 15 Dec 2025 16:48:11 -0300 Subject: [PATCH] fix(chat): corrige sessao problematica diretamente pelo ID MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Usa patch direto sem buscar sessao (evita erro de shape) - Encerra sessao pd71bvfbxx7th3npdj519hcf3s7xbe2j que estava causando erros 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- convex/liveChat.ts | 54 ++++++++++++++++------------------------------ 1 file changed, 19 insertions(+), 35 deletions(-) diff --git a/convex/liveChat.ts b/convex/liveChat.ts index b68835e..8067631 100644 --- a/convex/liveChat.ts +++ b/convex/liveChat.ts @@ -902,48 +902,32 @@ export const fixLegacySessions = mutation({ handler: async (ctx) => { const now = Date.now() - // Buscar TODAS as sessoes (nao so por index) para garantir que pegamos as problematicas - const allSessions = await ctx.db - .query("liveChatSessions") - .take(500) + // IDs problematicos conhecidos - sessoes sem lastAgentMessageAt + const knownProblematicIds = [ + "pd71bvfbxx7th3npdj519hcf3s7xbe2j", + ] let fixed = 0 let ended = 0 - const problematic: string[] = [] + const results: string[] = [] - for (const session of allSessions) { - // Se sessao nao tem lastAgentMessageAt, adiciona o valor - if (session.lastAgentMessageAt === undefined) { - 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 ja encerrada - apenas adicionar o campo - await ctx.db.patch(session._id, { - lastAgentMessageAt: session.lastActivityAt ?? session.startedAt, - }) - fixed++ - } + for (const sessionId of knownProblematicIds) { + try { + // Usar patch diretamente sem buscar a sessao (evita erro de shape) + await ctx.db.patch(sessionId as Id<"liveChatSessions">, { + status: "ENDED", + endedAt: now, + lastAgentMessageAt: now, + }) + ended++ + results.push(`${sessionId}: ended`) + } catch (error) { + results.push(`${sessionId}: error - ${error}`) } } - console.log(`fixLegacySessions: fixed=${fixed}, ended=${ended}, problematic=${problematic.join(",")}`) - return { fixed, ended, total: allSessions.length, problematic } + console.log(`fixLegacySessions: fixed=${fixed}, ended=${ended}, results=${results.join(", ")}`) + return { fixed, ended, results } }, })