/ app / Http / Livewire / Notifications.php
Notifications.php
 1  <?php
 2  
 3  namespace App\Http\Livewire;
 4  
 5  use App\Http\Services\DossierService;
 6  use App\Http\Services\TaskService;
 7  use App\Models\Task;
 8  use Illuminate\Notifications\DatabaseNotification;
 9  use Livewire\Component;
10  use Livewire\Redirector;
11  use Illuminate\Support\Str;
12  
13  class Notifications extends Component
14  {
15      public bool $readyToLoad = false;
16  
17      public function fetchData()
18      {
19          $this->readyToLoad = true;
20      }
21  
22      protected function getListeners()
23      {
24          return [
25              'refresh' => '$refresh',
26              'notificationReaded' => '$refresh',
27          ];
28      }
29  
30      public function getNotificationsProperty()
31      {
32          return $this->readyToLoad ? auth()->user()->unreadNotifications : [];
33      }
34  
35      public function readed(string $notificationId)
36      {
37          auth()->user()->notifications->find($notificationId)->markAsRead();
38          $this->emit('notificationReaded');
39      }
40  
41      public function allReaded()
42      {
43          auth()->user()->unreadNotifications->markAsRead();
44          $this->emit('notificationReaded');
45      }
46  
47      public function viewEntity(string $notificationId)
48      {
49          $notification = DatabaseNotification::find($notificationId);
50          $data = $notification->data;
51  
52          $route = match ($notification->type) {
53              // Notificaciones de tareas
54              'App\\Notifications\\TaskAssigned' => isset($data['task_id']) ? route('tasks.task.view', ['task' => $data['task_id']]) : null,
55              'App\\Notifications\\TaskEndNotification' => isset($data['task_id']) ? route('tasks.task.view', ['task' => $data['task_id']]) : null,
56  
57              // Notificaciones de archivos
58              'App\\Notifications\\FileUploaded' => isset($data['dossier_id']) ? route('dossiers.dossier.view', ['dossier' => $data['dossier_id']]) : null,
59              'App\\Notifications\\FileReviewed' => isset($data['dossier_id']) ? route('dossiers.dossier.view', ['dossier' => $data['dossier_id']]) : null,
60  
61              // Notificaciones de vulnerabilidades
62              'App\\Notifications\\VulnerabilityCreated' => isset($data['toe_id']) ? route('toes.toe.view', ['toe' => $data['toe_id']]) : null,
63  
64              // Notificaciones de dossier
65              'App\\Notifications\\DossierComplete' => isset($data['dossier_id']) ? route('dossiers.dossier.view', ['dossier' => $data['dossier_id']]) : null,
66              'App\\Notifications\\DocumentForReview' => isset($data['dossier_id']) ? route('dossiers.dossier.view', ['dossier' => $data['dossier_id']]) : null,
67  
68              // Notificaciones de reuniones
69              'App\\Notifications\\ParticipantToMeetNotification' => isset($data['meet_id']) ? route('meets.meet.view', ['meet' => $data['meet_id']]) : null,
70  
71              // Notificaciones de rectificaciones
72              'App\\Notifications\\PendingRectifyNotification' => isset($data['dossier_id']) ? route('dossiers.dossier.view', ['dossier' => $data['dossier_id']]) : null,
73              'App\\Notifications\\RectifyNotReceivedNotification' => isset($data['dossier_id']) ? route('dossiers.dossier.view', ['dossier' => $data['dossier_id']]) : null,
74  
75              // Notificaciones de certificación
76              'App\\Notifications\\CertificationReportNotCreatedNotification' => isset($data['dossier_id']) ? route('dossiers.dossier.view', ['dossier' => $data['dossier_id']]) : null,
77              'App\\Notifications\\ORUploadedNotification' => isset($data['dossier_id']) ? route('dossiers.dossier.view', ['dossier' => $data['dossier_id']]) : null,
78  
79              default => null,
80          };
81  
82          if ($route === null) {
83              return;
84          }
85  
86          auth()->user()->notifications->find($notificationId)->markAsRead();
87          return redirect()->to($route);
88      }
89  
90      public function render()
91      {
92          return view('livewire.notifications');
93      }
94  }