Files
construprogress/app/Notifications/TaskStatusChangedNotification.php
T
Javier Braña 9222eefdf9 feat: Phase 2 - Laravel Reverb + real-time notifications
- Install laravel/reverb + laravel-echo + pusher-js
- Configure broadcasting to reverb in .env.example + .env
- Add Echo + Pusher client in resources/js/app.js
- Update all 14 notifications to implement ShouldBroadcast + broadcastOn()
- Update NotificationBell component to listen for real-time events
- Add Livewire.emit in notification-bell blade for JS-to-Livewire bridge

Tests: 101 passing (319 assertions)
2026-08-31 12:16:56 +02:00

54 lines
1.7 KiB
PHP

<?php
namespace App\Notifications;
use App\Models\Task;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Notifications\Notification;
class TaskStatusChangedNotification extends Notification implements ShouldBroadcast
{
use Queueable;
public function __construct(public Task $task, public string $oldStatus) {}
public function via($notifiable): array
{
return ['database', 'broadcast'];
}
public function toArray($notifiable): array
{
$label = [
'pending' => 'pendiente',
'in_progress' => 'en progreso',
'completed' => 'completada',
'cancelled' => 'cancelada',
][$this->task->status] ?? $this->task->status;
$oldLabel = [
'pending' => 'pendiente',
'in_progress' => 'en progreso',
'completed' => 'completada',
'cancelled' => 'cancelada',
][$this->oldStatus] ?? $this->oldStatus;
return [
'type' => 'task_status_changed',
'task_id' => $this->task->id,
'project_id' => $this->task->project_id,
'old_status' => $this->oldStatus,
'new_status' => $this->task->status,
'message' => "La tarea '{$this->task->title}' cambió de {$oldLabel} a {$label}",
];
}
public function broadcastOn(): array
{
$userIds = [];
if ($this->task->assigned_to) $userIds[] = $this->task->assigned_to;
if ($this->task->created_by) $userIds[] = $this->task->created_by;
return array_map(fn ($id) => new \Illuminate\Broadcasting\PrivateChannel("user.{$id}"), array_unique($userIds));
}
}