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