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