feat(tickets): Play e Chat atribuem responsavel automaticamente
- Botao Play habilitado mesmo sem responsavel - Ao clicar Play sem responsavel, atribui usuario logado automaticamente - Ao iniciar chat ao vivo sem responsavel, atribui usuario logado - Adiciona mutation fixLegacySessions para corrigir sessoes antigas 🤖 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
3bfc5793f1
commit
98b23af4b2
3 changed files with 105 additions and 15 deletions
|
|
@ -343,13 +343,38 @@ export function TicketSummaryHeader({ ticket }: TicketHeaderProps) {
|
|||
const machineOnline = liveChatSession?.machineOnline ?? false
|
||||
const hasActiveSession = Boolean(liveChatSession?.sessionId)
|
||||
const ticketHasAssignee = Boolean(ticket.assignee)
|
||||
const canStartChat = hasMachine && !hasActiveSession && isStaff && ticketHasAssignee
|
||||
// Permitir iniciar chat mesmo sem responsavel - vai atribuir automaticamente
|
||||
const canStartChat = hasMachine && !hasActiveSession && isStaff
|
||||
|
||||
const handleStartLiveChat = async () => {
|
||||
if (!convexUserId || !ticket.id || isStartingChat) return
|
||||
setIsStartingChat(true)
|
||||
toast.dismiss("live-chat")
|
||||
|
||||
try {
|
||||
// Se nao ha responsavel, atribuir o usuario logado automaticamente
|
||||
if (!ticketHasAssignee) {
|
||||
toast.loading("Atribuindo responsavel...", { id: "live-chat" })
|
||||
await changeAssignee({
|
||||
ticketId: ticket.id as Id<"tickets">,
|
||||
assigneeId: convexUserId as Id<"users">,
|
||||
actorId: convexUserId as Id<"users">,
|
||||
reason: "Atribuido automaticamente ao iniciar chat ao vivo",
|
||||
})
|
||||
// Atualizar estado local
|
||||
const agent = agents.find((a) => String(a._id) === convexUserId)
|
||||
if (agent) {
|
||||
setAssigneeState({
|
||||
id: String(agent._id),
|
||||
name: agent.name,
|
||||
email: agent.email,
|
||||
avatarUrl: agent.avatarUrl ?? undefined,
|
||||
teams: Array.isArray(agent.teams) ? agent.teams.filter((t): t is string => typeof t === "string") : [],
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
toast.loading("Iniciando chat ao vivo...", { id: "live-chat" })
|
||||
const result = await startLiveChat({
|
||||
ticketId: ticket.id as Id<"tickets">,
|
||||
actorId: convexUserId as Id<"users">,
|
||||
|
|
@ -357,18 +382,18 @@ export function TicketSummaryHeader({ ticket }: TicketHeaderProps) {
|
|||
if (result.isNew) {
|
||||
toast.success("Chat ao vivo iniciado", { id: "live-chat" })
|
||||
} else {
|
||||
toast.info("Já existe uma sessão de chat ativa", { id: "live-chat" })
|
||||
toast.info("Ja existe uma sessao de chat ativa", { id: "live-chat" })
|
||||
}
|
||||
} catch (error: unknown) {
|
||||
console.error("[LiveChat] Erro ao iniciar chat:", error)
|
||||
// Extrair mensagem amigável do erro do Convex
|
||||
let message = "Não foi possível iniciar o chat"
|
||||
let message = "Nao foi possivel iniciar o chat"
|
||||
if (error instanceof Error) {
|
||||
const errorMsg = error.message.toLowerCase()
|
||||
if (errorMsg.includes("offline")) {
|
||||
message = "Máquina offline. Aguarde a máquina ficar online para iniciar o chat."
|
||||
} else if (errorMsg.includes("não encontrad") || errorMsg.includes("not found")) {
|
||||
message = "Máquina não encontrada"
|
||||
message = "Maquina offline. Aguarde a maquina ficar online para iniciar o chat."
|
||||
} else if (errorMsg.includes("nao encontrad") || errorMsg.includes("not found")) {
|
||||
message = "Maquina nao encontrada"
|
||||
}
|
||||
}
|
||||
toast.error(message, { id: "live-chat" })
|
||||
|
|
@ -597,7 +622,8 @@ export function TicketSummaryHeader({ ticket }: TicketHeaderProps) {
|
|||
const hasAssignee = Boolean(currentAssigneeId)
|
||||
const isCurrentResponsible = hasAssignee && convexUserId ? currentAssigneeId === convexUserId : false
|
||||
const isResolved = status === "RESOLVED"
|
||||
const canControlWork = !isResolved && isStaff && hasAssignee
|
||||
// Permitir Play mesmo sem responsavel - vai atribuir automaticamente
|
||||
const canControlWork = !isResolved && isStaff
|
||||
const canPauseWork = !isResolved && (isAdmin || isCurrentResponsible)
|
||||
const pauseDisabled = !canPauseWork
|
||||
const startDisabled = !canControlWork
|
||||
|
|
@ -605,14 +631,11 @@ export function TicketSummaryHeader({ ticket }: TicketHeaderProps) {
|
|||
if (isResolved) {
|
||||
return "Este chamado está encerrado. Reabra o ticket para iniciar um novo atendimento."
|
||||
}
|
||||
if (!hasAssignee) {
|
||||
return "Defina um responsável antes de iniciar o atendimento."
|
||||
}
|
||||
if (!isStaff) {
|
||||
return "Apenas a equipe interna pode iniciar este atendimento."
|
||||
}
|
||||
return "Não é possível iniciar o atendimento neste momento."
|
||||
}, [isResolved, hasAssignee, isStaff])
|
||||
}, [isResolved, isStaff])
|
||||
|
||||
useEffect(() => {
|
||||
if (!customersInitialized) {
|
||||
|
|
@ -1097,10 +1120,34 @@ export function TicketSummaryHeader({ ticket }: TicketHeaderProps) {
|
|||
|
||||
const handleStartWork = async (workType: "INTERNAL" | "EXTERNAL") => {
|
||||
if (!convexUserId) return
|
||||
|
||||
// Se nao ha responsavel, atribuir o usuario logado automaticamente
|
||||
if (!assigneeState?.id) {
|
||||
toast.error("Defina um responsável antes de iniciar o atendimento.")
|
||||
return
|
||||
toast.loading("Atribuindo responsavel e iniciando...", { id: "work" })
|
||||
try {
|
||||
await changeAssignee({
|
||||
ticketId: ticket.id as Id<"tickets">,
|
||||
assigneeId: convexUserId as Id<"users">,
|
||||
actorId: convexUserId as Id<"users">,
|
||||
reason: "Atribuido automaticamente ao iniciar atendimento",
|
||||
})
|
||||
// Atualizar estado local
|
||||
const agent = agents.find((a) => String(a._id) === convexUserId)
|
||||
if (agent) {
|
||||
setAssigneeState({
|
||||
id: String(agent._id),
|
||||
name: agent.name,
|
||||
email: agent.email,
|
||||
avatarUrl: agent.avatarUrl ?? undefined,
|
||||
teams: Array.isArray(agent.teams) ? agent.teams.filter((t): t is string => typeof t === "string") : [],
|
||||
})
|
||||
}
|
||||
} catch (err) {
|
||||
toast.error("Nao foi possivel atribuir responsavel.", { id: "work" })
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
toast.dismiss("work")
|
||||
toast.loading("Iniciando atendimento...", { id: "work" })
|
||||
try {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue