| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- <?php
- namespace App\Http\Controllers;
- use App\Enums\NotificationType;
- use App\Models\Kanban;
- use App\Services\KanbanReplyService;
- use App\Services\NotificationService;
- use App\Http\Requests\KanbanReplyRequest;
- use App\Http\Resources\KanbanReplyResource;
- use Illuminate\Http\JsonResponse;
- class KanbanReplyController extends Controller
- {
- public function __construct(
- protected KanbanReplyService $service,
- protected NotificationService $notifications,
- ) {}
- public function index(int $kanbanId): JsonResponse
- {
- $replies = $this->service->getByKanban($kanbanId);
- return $this->successResponse(payload: KanbanReplyResource::collection($replies));
- }
- public function store(KanbanReplyRequest $request, int $kanbanId): JsonResponse
- {
- $reply = $this->service->create($kanbanId, auth()->id(), $request->validated()['reply']);
- $this->notifyReplyCreated($kanbanId);
- return $this->successResponse(payload: new KanbanReplyResource($reply), message: __('messages.created'), code: 201);
- }
- /**
- * Notifica o outro lado da atividade quando há um novo comentário.
- * Autor da matriz → unidade da atividade; autor da unidade → matriz.
- */
- private function notifyReplyCreated(int $kanbanId): void
- {
- $card = Kanban::find($kanbanId);
- if (!$card) {
- return;
- }
- $author = auth()->user();
- $authorIsMatriz = $author->user_type === 'ADMIN';
- $unitId = $card->unit_id ?? $card->target_unit_id;
- $recipients = $authorIsMatriz
- ? $this->notifications->unitUserIds($unitId)
- : $this->notifications->matrizUserIds();
- // Não notifica o próprio autor do comentário.
- $recipients = array_values(array_diff($recipients, [$author->id]));
- $this->notifications->dispatch([
- 'origin_table' => 'kanban_replies',
- 'origin_id' => $card->id,
- 'unit_id' => $unitId,
- 'type' => NotificationType::KANBAN_REPLY->value,
- 'title' => 'Novo comentário na atividade',
- 'message' => $card->title,
- 'url' => '/kanban',
- 'action_text' => 'Ver atividade',
- 'created_by_user_id' => $author->id,
- 'recipient_user_ids' => $recipients,
- ]);
- }
- public function update(KanbanReplyRequest $request, int $kanbanId, int $id): JsonResponse
- {
- $reply = $this->service->update($kanbanId, $id, $request->validated()['reply']);
- return $this->successResponse(payload: new KanbanReplyResource($reply), message: __('messages.updated'));
- }
- public function destroy(int $kanbanId, int $id): JsonResponse
- {
- $this->service->delete($kanbanId, $id);
- return $this->successResponse(message: __('messages.deleted'), code: 204);
- }
- }
|