You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.5 KiB

  1. <?php
  2. namespace App\Listeners;
  3. use App\Events\TaskCreate;
  4. use App\Models\Task;
  5. use App\Models\User;
  6. use App\Utilities\HelperClass\NotificationHelper;
  7. class TaskCreateNotif
  8. {
  9. /**
  10. * @var NotificationHelper
  11. */
  12. public $helper;
  13. /**
  14. * Create the event listener.
  15. *
  16. * @return void
  17. */
  18. public function __construct(NotificationHelper $helper)
  19. {
  20. $this->helper = $helper;
  21. }
  22. /**
  23. * Handle the event.
  24. *
  25. * @param TaskCreate $event
  26. * @return void
  27. */
  28. public function handle(TaskCreate $event)
  29. {
  30. $payload = $event->message;
  31. if ($payload->data->original->assignee_id !== null) {
  32. $this->assigneeNotifHandler($payload);
  33. }
  34. if ($payload->data->original->approver_id !== null) {
  35. $this->approverNotifHandler($payload);
  36. }
  37. }
  38. public function assigneeNotifHandler($payload) {
  39. $user = User::findOrFail($payload->data->original->assignee_id);
  40. $task = Task::findOrFail($payload->data->original->id);
  41. $this->helper->makeNotif($payload, 'assignee', ['task' => $task->title])
  42. ->sendNotifications($user, 'critical');
  43. }
  44. public function approverNotifHandler($payload) {
  45. $user = User::findOrFail($payload->data->original->approver_id);
  46. $task = Task::findOrFail($payload->data->original->id);
  47. $this->helper->makeNotif($payload, 'approver', ['task' => $task->title])
  48. ->sendNotifications($user, 'critical');
  49. }
  50. }