KanbanReplyController.php 2.9 KB

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