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.

155 lines
4.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\Notifications\DBNotification;
  7. use App\Notifications\FcmNotification;
  8. use App\Notifications\MailNotification;
  9. use Illuminate\Support\Facades\Notification;
  10. class TaskUpdateNotif
  11. {
  12. /**
  13. * Create the event listener.
  14. *
  15. * @return void
  16. */
  17. public function __construct()
  18. {
  19. //
  20. }
  21. /**
  22. * Handle the event.
  23. *
  24. * @param TaskUpdate $event
  25. * @return void
  26. */
  27. public function handle(TaskUpdate $event)
  28. {
  29. $payload = $event->message;
  30. if (isset($payload->data->diff->assignee_id)) {
  31. $this->assigneeNotifHandler($payload);
  32. }
  33. if (isset($payload->data->diff->approver_id)) {
  34. $this->approverNotifHandler($payload);
  35. }
  36. if (isset($payload->data->diff->completed_at)) {
  37. $this->completedNotifHandler($payload);
  38. }
  39. if (isset($payload->data->diff->ready_to_test)) {
  40. $this->readyNotifHandler($payload);
  41. }
  42. }
  43. public function assigneeNotifHandler($payload) {
  44. $user = User::findOrFail($payload->data->diff->assignee_id);
  45. $task = Task::findOrFail($payload->data->original->id);
  46. $notif = $this->makeNotif($payload, 'assignee', ['task' => $task->title]);
  47. $this->sendNotifications($user, $notif);
  48. }
  49. public function approverNotifHandler($payload) {
  50. $user = User::findOrFail($payload->data->diff->approver_id);
  51. $task = Task::findOrFail($payload->data->original->id);
  52. $notif = $this->makeNotif($payload, 'approver', ['task' => $task->title]);
  53. $this->sendNotifications($user, $notif);
  54. }
  55. public function completedNotifHandler($payload){
  56. $task = Task::findOrFail($payload->data->original->id);
  57. $user_ids = array_filter([
  58. $task->approver_id,
  59. $task->assignee_id,
  60. $task->creator_id
  61. ]);
  62. $user_ids = array_unique(array_merge($user_ids, $task->watchers));
  63. $users = User::whereIn('id', $user_ids)->where('id', '!=', auth()->id())->get();
  64. $notif = $this->makeNotif($payload, 'completed', ['task' => $task->title]);
  65. $this->sendNotifications($users, $notif);
  66. }
  67. public function readyNotifHandler($payload)
  68. {
  69. $task = Task::findOrFail($payload->data->original->id);
  70. $user_ids = array_unique(array_filter([
  71. $task->approver_id,
  72. $task->assignee_id,
  73. $task->creator_id
  74. ]));
  75. $users = User::whereIn('id', $user_ids)->where('id', '!=', auth()->id())->get();
  76. $notif = $this->makeNotif($payload, 'ready', ['task' => $task->title]);
  77. $this->sendNotifications($users, $notif);
  78. }
  79. /**
  80. * Make notification object
  81. *
  82. * @param $payload
  83. * @param null $route
  84. * @param array $options
  85. * @return array
  86. */
  87. public function makeNotif($payload, $route = null, $options = []) {
  88. $route = $route == null ? "" : $route.'.';
  89. return [
  90. 'greeting' => $this->getMessageLine($payload, $route.'greeting'),
  91. 'subject' => $this->getMessageLine($payload, $route.'subject'),
  92. 'title' => $this->getMessageLine($payload, $route.'title'),
  93. 'body' => $this->getMessageLine($payload, $route.'body', $options)
  94. ];
  95. }
  96. /**
  97. * Fetch message from notifications lang file
  98. *
  99. * @param $payload
  100. * @param $key
  101. * @param null $options
  102. * @return array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Translation\Translator|string|null
  103. *
  104. */
  105. public function getMessageLine($payload, $key, $options = [])
  106. {
  107. return __('notification.'.$payload->data->table_name.'.'.enum('cruds.inverse.'.$payload->data->crud_id.'.singular_name').'.'.$key, $options);
  108. }
  109. /**
  110. * Call notifications
  111. *
  112. * @param $users
  113. * @param $notif
  114. */
  115. public function sendNotifications($users, $notif) {
  116. // switch ($level) {
  117. // case "emergency":
  118. //// Notification::send($users, new SmsNotification($notif));
  119. // case "critical":
  120. // Notification::send($users, new MailNotification($notif));
  121. // case "high":
  122. // Notification::send($users, new DBNotification($notif));
  123. // case "medium":
  124. // Notification::send($users, new FcmNotification($notif));
  125. // case "low":
  126. //// Notification::send($users, new SocketNotification($notif));
  127. // default:
  128. //// Notification::send($users, new SocketNotification($notif));
  129. // }
  130. Notification::send($users, new MailNotification($notif));
  131. Notification::send($users, new DBNotification($notif));
  132. Notification::send($users, new FcmNotification($notif));
  133. }
  134. }