diff --git a/convex/tickets.ts b/convex/tickets.ts index c273960..bd7bf00 100644 --- a/convex/tickets.ts +++ b/convex/tickets.ts @@ -778,6 +778,15 @@ export const addComment = mutation({ throw new ConvexError("Apenas administradores e agentes podem registrar comentários internos") } + // Regra: a equipe (ADMIN/AGENT/MANAGER) só pode comentar se o ticket tiver responsável. + // O solicitante (colaborador) pode comentar sempre. + const isRequester = String(ticketDoc.requesterId) === String(author._id) + const isStaff = normalizedRole === "ADMIN" || normalizedRole === "AGENT" || normalizedRole === "MANAGER" + const hasAssignee = Boolean(ticketDoc.assigneeId) + if (!isRequester && isStaff && !hasAssignee) { + throw new ConvexError("Somente é possível comentar quando o chamado possui um responsável.") + } + if (ticketDoc.requesterId === args.authorId) { // O próprio solicitante pode comentar seu ticket. // Comentários internos já são bloqueados acima para quem não é STAFF. diff --git a/src/components/tickets/ticket-comments.rich.tsx b/src/components/tickets/ticket-comments.rich.tsx index 8eafed7..849ef8c 100644 --- a/src/components/tickets/ticket-comments.rich.tsx +++ b/src/components/tickets/ticket-comments.rich.tsx @@ -36,6 +36,10 @@ export function TicketComments({ ticket }: TicketCommentsProps) { const { convexUserId, isStaff, role } = useAuth() const normalizedRole = role ?? null const canSeeInternalComments = normalizedRole === "admin" || normalizedRole === "agent" + const hasAssignee = Boolean(ticket.assignee) + const isRequester = convexUserId ? ticket.requester.id === convexUserId : false + const canStaffComment = hasAssignee + const canComment = isRequester || (isStaff && canStaffComment) const addComment = useMutation(api.tickets.addComment) const removeAttachment = useMutation(api.tickets.removeCommentAttachment) const updateComment = useMutation(api.tickets.updateComment) @@ -360,12 +364,18 @@ export function TicketComments({ ticket }: TicketCommentsProps) { ) }) )} + {!canComment && ( +
+ Atribua um responsável ao chamado para que a equipe possa comentar. +
+ )}
- + setAttachmentsToSend((prev) => [...prev, ...files])} currentFileCount={attachmentsToSend.length} currentTotalBytes={attachmentsToSendTotalBytes} + disabled={!canComment} /> {attachmentsToSend.length > 0 ? (
@@ -477,7 +487,7 @@ export function TicketComments({ ticket }: TicketCommentsProps) {
-