feat: aprimora upload/anexos e regras de atendimento no portal
This commit is contained in:
parent
7e8023ed87
commit
c90e99820f
8 changed files with 218 additions and 74 deletions
|
|
@ -89,7 +89,9 @@ function formatDuration(durationMs: number) {
|
|||
|
||||
export function TicketSummaryHeader({ ticket }: TicketHeaderProps) {
|
||||
const { convexUserId, role, isStaff } = useAuth()
|
||||
const isManager = role === "manager"
|
||||
const normalizedRole = (role ?? "").toLowerCase()
|
||||
const isManager = normalizedRole === "manager"
|
||||
const isAdmin = normalizedRole === "admin"
|
||||
useDefaultQueues(ticket.tenantId)
|
||||
const changeAssignee = useMutation(api.tickets.changeAssignee)
|
||||
const changeQueue = useMutation(api.tickets.changeQueue)
|
||||
|
|
@ -138,6 +140,8 @@ export function TicketSummaryHeader({ ticket }: TicketHeaderProps) {
|
|||
subcategoryId: ticket.subcategory?.id ?? "",
|
||||
}
|
||||
)
|
||||
const currentAssigneeId = ticket.assignee?.id ?? ""
|
||||
const [assigneeSelection, setAssigneeSelection] = useState(currentAssigneeId)
|
||||
const [saving, setSaving] = useState(false)
|
||||
const [pauseDialogOpen, setPauseDialogOpen] = useState(false)
|
||||
const [pauseReason, setPauseReason] = useState<string>(PAUSE_REASONS[0]?.value ?? "NO_CONTACT")
|
||||
|
|
@ -159,13 +163,20 @@ export function TicketSummaryHeader({ ticket }: TicketHeaderProps) {
|
|||
const isAvulso = Boolean(((ticket.company ?? null) as { isAvulso?: boolean } | null)?.isAvulso ?? false)
|
||||
const [queueSelection, setQueueSelection] = useState(currentQueueName)
|
||||
const queueDirty = useMemo(() => queueSelection !== currentQueueName, [queueSelection, currentQueueName])
|
||||
const formDirty = dirty || categoryDirty || queueDirty
|
||||
const assigneeDirty = useMemo(() => assigneeSelection !== currentAssigneeId, [assigneeSelection, currentAssigneeId])
|
||||
const formDirty = dirty || categoryDirty || queueDirty || assigneeDirty
|
||||
|
||||
const activeCategory = useMemo(
|
||||
() => categories.find((category) => category.id === selectedCategoryId) ?? null,
|
||||
[categories, selectedCategoryId]
|
||||
)
|
||||
const secondaryOptions = useMemo(() => activeCategory?.secondary ?? [], [activeCategory])
|
||||
const hasAssignee = Boolean(currentAssigneeId)
|
||||
const isCurrentResponsible = hasAssignee && convexUserId ? currentAssigneeId === convexUserId : false
|
||||
const canControlWork = isAdmin || !hasAssignee || isCurrentResponsible
|
||||
const canPauseWork = isAdmin || isCurrentResponsible
|
||||
const pauseDisabled = !canPauseWork
|
||||
const startDisabled = !canControlWork
|
||||
|
||||
async function handleSave() {
|
||||
if (!convexUserId || !formDirty) {
|
||||
|
|
@ -225,6 +236,31 @@ export function TicketSummaryHeader({ ticket }: TicketHeaderProps) {
|
|||
setQueueSelection(currentQueueName)
|
||||
}
|
||||
|
||||
if (assigneeDirty && !isManager) {
|
||||
if (!assigneeSelection) {
|
||||
toast.error("Selecione um responsável válido.", { id: "assignee" })
|
||||
setAssigneeSelection(currentAssigneeId)
|
||||
throw new Error("invalid-assignee")
|
||||
} else {
|
||||
toast.loading("Atualizando responsável...", { id: "assignee" })
|
||||
try {
|
||||
await changeAssignee({
|
||||
ticketId: ticket.id as Id<"tickets">,
|
||||
assigneeId: assigneeSelection as Id<"users">,
|
||||
actorId: convexUserId as Id<"users">,
|
||||
})
|
||||
toast.success("Responsável atualizado!", { id: "assignee" })
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
toast.error("Não foi possível atualizar o responsável.", { id: "assignee" })
|
||||
setAssigneeSelection(currentAssigneeId)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
} else if (assigneeDirty && isManager) {
|
||||
setAssigneeSelection(currentAssigneeId)
|
||||
}
|
||||
|
||||
if (dirty) {
|
||||
toast.loading("Salvando alterações...", { id: "save-header" })
|
||||
if (subject !== ticket.subject) {
|
||||
|
|
@ -259,6 +295,7 @@ export function TicketSummaryHeader({ ticket }: TicketHeaderProps) {
|
|||
subcategoryId: currentSubcategoryId,
|
||||
})
|
||||
setQueueSelection(currentQueueName)
|
||||
setAssigneeSelection(currentAssigneeId)
|
||||
setEditing(false)
|
||||
}
|
||||
|
||||
|
|
@ -269,6 +306,7 @@ export function TicketSummaryHeader({ ticket }: TicketHeaderProps) {
|
|||
subcategoryId: ticket.subcategory?.id ?? "",
|
||||
})
|
||||
setQueueSelection(ticket.queue ?? "")
|
||||
setAssigneeSelection(ticket.assignee?.id ?? "")
|
||||
}, [editing, ticket.category?.id, ticket.subcategory?.id, ticket.queue])
|
||||
|
||||
useEffect(() => {
|
||||
|
|
@ -387,8 +425,9 @@ export function TicketSummaryHeader({ ticket }: TicketHeaderProps) {
|
|||
} else {
|
||||
toast.success("Atendimento iniciado", { id: "work" })
|
||||
}
|
||||
} catch {
|
||||
toast.error("Não foi possível atualizar o atendimento", { id: "work" })
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Não foi possível atualizar o atendimento"
|
||||
toast.error(message, { id: "work" })
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -410,8 +449,9 @@ export function TicketSummaryHeader({ ticket }: TicketHeaderProps) {
|
|||
toast.success("Atendimento pausado", { id: "work" })
|
||||
}
|
||||
setPauseDialogOpen(false)
|
||||
} catch {
|
||||
toast.error("Não foi possível atualizar o atendimento", { id: "work" })
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : "Não foi possível atualizar o atendimento"
|
||||
toast.error(message, { id: "work" })
|
||||
} finally {
|
||||
setPausing(false)
|
||||
}
|
||||
|
|
@ -506,16 +546,41 @@ export function TicketSummaryHeader({ ticket }: TicketHeaderProps) {
|
|||
requesterName={ticket.requester?.name ?? ticket.requester?.email ?? null}
|
||||
/>
|
||||
{isPlaying ? (
|
||||
<Button
|
||||
size="sm"
|
||||
className={pauseButtonClass}
|
||||
onClick={() => {
|
||||
if (!convexUserId) return
|
||||
setPauseDialogOpen(true)
|
||||
}}
|
||||
>
|
||||
<IconPlayerPause className="size-4 text-white" /> Pausar
|
||||
</Button>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<span className="inline-flex">
|
||||
<Button
|
||||
size="sm"
|
||||
className={pauseButtonClass}
|
||||
onClick={() => {
|
||||
if (!convexUserId || pauseDisabled) return
|
||||
setPauseDialogOpen(true)
|
||||
}}
|
||||
disabled={pauseDisabled}
|
||||
>
|
||||
<IconPlayerPause className="size-4 text-white" /> Pausar
|
||||
</Button>
|
||||
</span>
|
||||
</TooltipTrigger>
|
||||
{pauseDisabled ? (
|
||||
<TooltipContent className="rounded-lg border border-slate-200 bg-white px-3 py-2 text-xs font-medium text-neutral-700 shadow-lg">
|
||||
Apenas o responsável atual ou um administrador pode pausar o atendimento.
|
||||
</TooltipContent>
|
||||
) : null}
|
||||
</Tooltip>
|
||||
) : startDisabled ? (
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<span className="inline-flex">
|
||||
<Button size="sm" className={startButtonClass} disabled>
|
||||
<IconPlayerPlay className="size-4 text-white" /> Iniciar
|
||||
</Button>
|
||||
</span>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent className="rounded-lg border border-slate-200 bg-white px-3 py-2 text-xs font-medium text-neutral-700 shadow-lg">
|
||||
Apenas o responsável atual ou um administrador pode iniciar este atendimento.
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
) : (
|
||||
<DropdownMenu>
|
||||
<DropdownMenuTrigger asChild>
|
||||
|
|
@ -666,17 +731,10 @@ export function TicketSummaryHeader({ ticket }: TicketHeaderProps) {
|
|||
{editing ? (
|
||||
<Select
|
||||
disabled={isManager}
|
||||
value={ticket.assignee?.id ?? ""}
|
||||
onValueChange={async (value) => {
|
||||
if (!convexUserId) return
|
||||
value={assigneeSelection}
|
||||
onValueChange={(value) => {
|
||||
if (isManager) return
|
||||
toast.loading("Atribuindo responsável...", { id: "assignee" })
|
||||
try {
|
||||
await changeAssignee({ ticketId: ticket.id as Id<"tickets">, assigneeId: value as Id<"users">, actorId: convexUserId as Id<"users"> })
|
||||
toast.success("Responsável atualizado!", { id: "assignee" })
|
||||
} catch {
|
||||
toast.error("Não foi possível atribuir.", { id: "assignee" })
|
||||
}
|
||||
setAssigneeSelection(value)
|
||||
}}
|
||||
>
|
||||
<SelectTrigger className={selectTriggerClass}>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue