fix(chat): corrige sessao problematica diretamente pelo ID

- 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 <noreply@anthropic.com>
This commit is contained in:
rever-tecnologia 2025-12-15 16:48:11 -03:00
parent 129ae70930
commit f451ca2e3b

View file

@ -902,48 +902,32 @@ export const fixLegacySessions = mutation({
handler: async (ctx) => { handler: async (ctx) => {
const now = Date.now() const now = Date.now()
// Buscar TODAS as sessoes (nao so por index) para garantir que pegamos as problematicas // IDs problematicos conhecidos - sessoes sem lastAgentMessageAt
const allSessions = await ctx.db const knownProblematicIds = [
.query("liveChatSessions") "pd71bvfbxx7th3npdj519hcf3s7xbe2j",
.take(500) ]
let fixed = 0 let fixed = 0
let ended = 0 let ended = 0
const problematic: string[] = [] const results: string[] = []
for (const session of allSessions) { for (const sessionId of knownProblematicIds) {
// Se sessao nao tem lastAgentMessageAt, adiciona o valor try {
if (session.lastAgentMessageAt === undefined) { // Usar patch diretamente sem buscar a sessao (evita erro de shape)
problematic.push(session._id) await ctx.db.patch(sessionId as Id<"liveChatSessions">, {
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", status: "ENDED",
endedAt: now, endedAt: now,
lastAgentMessageAt: session.lastActivityAt ?? session.startedAt, lastAgentMessageAt: now,
}) })
ended++ ended++
} else { results.push(`${sessionId}: ended`)
// Sessao recente - apenas corrigir o campo } catch (error) {
await ctx.db.patch(session._id, { results.push(`${sessionId}: error - ${error}`)
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++
}
} }
} }
console.log(`fixLegacySessions: fixed=${fixed}, ended=${ended}, problematic=${problematic.join(",")}`) console.log(`fixLegacySessions: fixed=${fixed}, ended=${ended}, results=${results.join(", ")}`)
return { fixed, ended, total: allSessions.length, problematic } return { fixed, ended, results }
}, },
}) })