service->getByTicket($ticketId); return $this->successResponse(payload: SupportReplyResource::collection($replies)); } public function store(SupportReplyRequest $request, int $ticketId): JsonResponse { $reply = $this->service->create($ticketId, auth()->id(), $request->validated()['reply']); $this->notifyReplyCreated($ticketId); return $this->successResponse(payload: new SupportReplyResource($reply), message: __('messages.created'), code: 201); } /** * Notifica o outro lado do chamado quando há uma nova resposta. * Autor da matriz → unidade do chamado; autor da unidade → matriz. */ private function notifyReplyCreated(int $ticketId): void { $ticket = SupportTicket::find($ticketId); if (!$ticket) { return; } $author = auth()->user(); $authorIsMatriz = $author->user_type === 'ADMIN'; $unitId = $ticket->applicant_unit_id ?? $ticket->target_unit_id; $recipients = $authorIsMatriz ? $this->notifications->unitUserIds($unitId) : $this->notifications->matrizUserIds(); // Não notifica o próprio autor da resposta. $recipients = array_values(array_diff($recipients, [$author->id])); $this->notifications->dispatch([ 'origin_table' => 'support_replies', 'origin_id' => $ticket->id, 'unit_id' => $unitId, 'type' => NotificationType::SUPPORT_TICKET_REPLY->value, 'title' => 'Nova resposta no chamado', 'message' => $ticket->title, 'url' => '/support', 'action_text' => 'Ver chamado', 'created_by_user_id' => $author->id, 'recipient_user_ids' => $recipients, ]); } public function update(SupportReplyRequest $request, int $ticketId, int $id): JsonResponse { $reply = $this->service->update($ticketId, $id, $request->validated()['reply']); return $this->successResponse(payload: new SupportReplyResource($reply), message: __('messages.updated')); } public function destroy(int $ticketId, int $id): JsonResponse { $this->service->delete($ticketId, $id); return $this->successResponse(message: __('messages.deleted'), code: 204); } }