KanbanReplyController.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Enums\NotificationType;
  4. use App\Models\Kanban;
  5. use App\Services\KanbanReplyService;
  6. use App\Services\NotificationService;
  7. use App\Http\Requests\KanbanReplyRequest;
  8. use App\Http\Resources\KanbanReplyResource;
  9. use Illuminate\Http\JsonResponse;
  10. class KanbanReplyController extends Controller
  11. {
  12. public function __construct(
  13. protected KanbanReplyService $service,
  14. protected NotificationService $notifications,
  15. ) {}
  16. public function index(int $kanbanId): JsonResponse
  17. {
  18. $replies = $this->service->getByKanban($kanbanId);
  19. return $this->successResponse(payload: KanbanReplyResource::collection($replies));
  20. }
  21. public function store(KanbanReplyRequest $request, int $kanbanId): JsonResponse
  22. {
  23. $reply = $this->service->create($kanbanId, auth()->id(), $request->validated()['reply']);
  24. $this->notifyReplyCreated($kanbanId);
  25. return $this->successResponse(payload: new KanbanReplyResource($reply), message: __('messages.created'), code: 201);
  26. }
  27. /**
  28. * Notifica o outro lado da atividade quando há um novo comentário.
  29. * Autor da matriz → unidade da atividade; autor da unidade → matriz.
  30. */
  31. private function notifyReplyCreated(int $kanbanId): void
  32. {
  33. $card = Kanban::find($kanbanId);
  34. if (!$card) {
  35. return;
  36. }
  37. $author = auth()->user();
  38. $authorIsMatriz = $author->user_type === 'ADMIN';
  39. $unitId = $card->unit_id ?? $card->target_unit_id;
  40. $recipients = $authorIsMatriz
  41. ? $this->notifications->unitUserIds($unitId)
  42. : $this->notifications->matrizUserIds();
  43. // Não notifica o próprio autor do comentário.
  44. $recipients = array_values(array_diff($recipients, [$author->id]));
  45. $this->notifications->dispatch([
  46. 'origin_table' => 'kanban_replies',
  47. 'origin_id' => $card->id,
  48. 'unit_id' => $unitId,
  49. 'type' => NotificationType::KANBAN_REPLY->value,
  50. 'title' => 'Novo comentário na atividade',
  51. 'message' => $card->title,
  52. 'url' => '/kanban',
  53. 'action_text' => 'Ver atividade',
  54. 'created_by_user_id' => $author->id,
  55. 'recipient_user_ids' => $recipients,
  56. ]);
  57. }
  58. public function update(KanbanReplyRequest $request, int $kanbanId, int $id): JsonResponse
  59. {
  60. $reply = $this->service->update($kanbanId, $id, $request->validated()['reply']);
  61. return $this->successResponse(payload: new KanbanReplyResource($reply), message: __('messages.updated'));
  62. }
  63. public function destroy(int $kanbanId, int $id): JsonResponse
  64. {
  65. $this->service->delete($kanbanId, $id);
  66. return $this->successResponse(message: __('messages.deleted'), code: 204);
  67. }
  68. }