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.

95 lines
2.8 KiB

  1. <?php
  2. namespace App\Listeners;
  3. use App\Events\TaskUpdate;
  4. use App\Models\Task;
  5. use App\Models\User;
  6. use App\Utilities\HelperClass\NotificationHelper;
  7. class TaskUpdateNotif
  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 TaskUpdate $event
  26. * @return void
  27. */
  28. public function handle(TaskUpdate $event)
  29. {
  30. $payload = $event->message;
  31. if (isset($payload->data->diff->assignee_id)) {
  32. $this->assigneeNotifHandler($payload);
  33. }
  34. if (isset($payload->data->diff->approver_id)) {
  35. $this->approverNotifHandler($payload);
  36. }
  37. if (isset($payload->data->diff->completed_at)) {
  38. $this->completedNotifHandler($payload);
  39. }
  40. if (isset($payload->data->diff->ready_to_test)) {
  41. $this->readyNotifHandler($payload);
  42. }
  43. }
  44. public function assigneeNotifHandler($payload) {
  45. $user = User::findOrFail($payload->data->diff->assignee_id);
  46. $task = Task::findOrFail($payload->data->original->id);
  47. $this->helper->makeNotif($payload, 'assignee', ['task' => $task->title])
  48. ->sendNotifications($user, 'critical');
  49. }
  50. public function approverNotifHandler($payload) {
  51. $user = User::findOrFail($payload->data->diff->approver_id);
  52. $task = Task::findOrFail($payload->data->original->id);
  53. $this->helper->makeNotif($payload, 'approver', ['task' => $task->title])
  54. ->sendNotifications($user, 'critical');
  55. }
  56. public function completedNotifHandler($payload){
  57. $task = Task::findOrFail($payload->data->original->id);
  58. $user_ids = array_filter([
  59. $task->approver_id,
  60. $task->assignee_id,
  61. $task->creator_id
  62. ]);
  63. $user_ids = array_unique(array_merge($user_ids, $task->watchers));
  64. $users = User::whereIn('id', $user_ids)->where('id', '!=', auth()->id())->get();
  65. $this->helper->makeNotif($payload, 'completed', ['task' => $task->title])
  66. ->sendNotifications($users, 'critical');
  67. }
  68. public function readyNotifHandler($payload)
  69. {
  70. $task = Task::findOrFail($payload->data->original->id);
  71. $user_ids = array_unique(array_filter([
  72. $task->approver_id,
  73. $task->assignee_id,
  74. $task->creator_id
  75. ]));
  76. $users = User::whereIn('id', $user_ids)->where('id', '!=', auth()->id())->get();
  77. $this->helper->makeNotif($payload, 'ready', ['task' => $task->title])
  78. ->sendNotifications($users, 'critical');
  79. }
  80. }