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.

93 lines
2.7 KiB

  1. <?php
  2. namespace App\Listeners;
  3. use App\Channels\FcmChannel;
  4. use App\Events\BusinessUserCreate;
  5. use App\Models\Business;
  6. use App\Models\User;
  7. use App\Notifications\DBNotification;
  8. use App\Notifications\FcmNotification;
  9. use App\Notifications\MailNotification;
  10. use Illuminate\Contracts\Queue\ShouldQueue;
  11. use Illuminate\Queue\InteractsWithQueue;
  12. use Illuminate\Support\Facades\Notification;
  13. class BusinessUserCreateNotif
  14. {
  15. /**
  16. * Create the event listener.
  17. *
  18. * @return void
  19. */
  20. public function __construct()
  21. {
  22. //
  23. }
  24. /**
  25. * Handle the event.
  26. *
  27. * @param BusinessUserCreate $event
  28. * @return void
  29. */
  30. public function handle(BusinessUserCreate $event)
  31. {
  32. $payload = $event->message;
  33. if ($payload->data->original->level === enum('levels.inactive.id')) {
  34. // When user level in business is zero, probably user added to business by system
  35. // And not necessary send notification to stockholders
  36. return;
  37. }
  38. $new_user = User::findOrFail($payload->data->original->user_id);
  39. $owners = Business::findOrFail($payload->business)->owners()->where('id', '!=', $new_user->id)->get();
  40. $notif = $this->makeNotif($payload,['business' => request('_business_info')['name'], 'user' => $new_user->name]);
  41. $users = $owners->prepend($new_user);
  42. $this->sendNotifications($users, $notif);
  43. }
  44. /**
  45. * Make notification object
  46. *
  47. * @param $payload
  48. * @param array $options
  49. * @return array
  50. */
  51. public function makeNotif($payload, $options = []) {
  52. return [
  53. 'greeting' => $this->getMessageLine($payload, 'greeting'),
  54. 'subject' => $this->getMessageLine($payload, 'subject'),
  55. 'title' => $this->getMessageLine($payload, 'title'),
  56. 'body' => $this->getMessageLine($payload, 'body', $options)
  57. ];
  58. }
  59. /**
  60. * Fetch message from notifications lang file
  61. *
  62. * @param $payload
  63. * @param $key
  64. * @param null $options
  65. * @return array|\Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Translation\Translator|string|null
  66. *
  67. */
  68. public function getMessageLine($payload, $key, $options = [])
  69. {
  70. return __('notification.'.$payload->data->table_name.'.'.enum('cruds.inverse.'.$payload->data->crud_id.'.singular_name').'.'.$key, $options);
  71. }
  72. /**
  73. * Call notifications
  74. *
  75. * @param $users
  76. * @param $notif
  77. */
  78. public function sendNotifications($users, $notif) {
  79. Notification::send($users, new MailNotification($notif));
  80. Notification::send($users, new DBNotification($notif));
  81. Notification::send($users, new FcmNotification($notif));
  82. }
  83. }