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.

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