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:
parent
129ae70930
commit
f451ca2e3b
1 changed files with 19 additions and 35 deletions
|
|
@ -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">, {
|
||||||
|
status: "ENDED",
|
||||||
if (session.status === "ACTIVE") {
|
endedAt: now,
|
||||||
// Sessao ativa muito antiga (mais de 24h) - encerrar
|
lastAgentMessageAt: now,
|
||||||
if (now - session.startedAt > 24 * 60 * 60 * 1000) {
|
})
|
||||||
await ctx.db.patch(session._id, {
|
ended++
|
||||||
status: "ENDED",
|
results.push(`${sessionId}: ended`)
|
||||||
endedAt: now,
|
} catch (error) {
|
||||||
lastAgentMessageAt: session.lastActivityAt ?? session.startedAt,
|
results.push(`${sessionId}: error - ${error}`)
|
||||||
})
|
|
||||||
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++
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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 }
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue