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.

104 lines
2.9 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\Notifications\DBNotification;
  7. use App\Notifications\FcmNotification;
  8. use App\Notifications\MailNotification;
  9. use Illuminate\Support\Facades\Notification;
  10. class TaskCreateNotif
  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 TaskCreate $event
  25. * @return void
  26. */
  27. public function handle(TaskCreate $event)
  28. {
  29. $payload = $event->message;
  30. if ($payload->data->original->assignee_id !== null) {
  31. $this->assigneeNotifHandler($payload);
  32. }
  33. if ($payload->data->original->approver_id !== null) {
  34. $this->approverNotifHandler($payload);
  35. }
  36. }
  37. public function assigneeNotifHandler($payload) {
  38. $user = User::findOrFail($payload->data->original->assignee_id);
  39. $task = Task::findOrFail($payload->data->original->id);
  40. $notif = $this->makeNotif($payload, 'assignee', ['task' => $task->title]);
  41. $this->sendNotifications($user, $notif);
  42. }
  43. public function approverNotifHandler($payload) {
  44. $user = User::findOrFail($payload->data->original->approver_id);
  45. $task = Task::findOrFail($payload->data->original->id);
  46. $notif = $this->makeNotif($payload, 'approver', ['task' => $task->title]);
  47. $this->sendNotifications($user, $notif);
  48. }
  49. /**
  50. * Make notification object
  51. *
  52. * @param $payload
  53. * @param null $route
  54. * @param array $options
  55. * @return array
  56. */
  57. public function makeNotif($payload, $route = null, $options = []) {
  58. $route = $route == null ? "" : $route.'.';
  59. return [
  60. 'greeting' => $this->getMessageLine($payload, $route.'greeting'),
  61. 'subject' => $this->getMessageLine($payload, $route.'subject'),
  62. 'title' => $this->getMessageLine($payload, $route.'title'),
  63. 'body' => $this->getMessageLine($payload, $route.'body', $options)
  64. ];
  65. }
  66. /**
  67. * Fetch message from notifications lang file
  68. *
  69. * @param $payload
  70. * @param $key
  71. * @param null $options
  72. * @return array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Translation\Translator|string|null
  73. *
  74. */
  75. public function getMessageLine($payload, $key, $options = [])
  76. {
  77. return __('notification.'.$payload->data->table_name.'.'.enum('cruds.inverse.'.$payload->data->crud_id.'.singular_name').'.'.$key, $options);
  78. }
  79. /**
  80. * Call notifications
  81. *
  82. * @param $users
  83. * @param $notif
  84. */
  85. public function sendNotifications($users, $notif) {
  86. Notification::send($users, new MailNotification($notif));
  87. Notification::send($users, new DBNotification($notif));
  88. Notification::send($users, new FcmNotification($notif));
  89. }
  90. }