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.

52 lines
1.3 KiB

  1. <?php
  2. namespace App\Listeners;
  3. use App\Events\BusinessUserCreate;
  4. use App\Models\Business;
  5. use App\Models\User;
  6. use App\Utilities\HelperClass\NotificationHelper;
  7. class BusinessUserCreateNotif
  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 BusinessUserCreate $event
  26. * @return void
  27. */
  28. public function handle(BusinessUserCreate $event)
  29. {
  30. $payload = $event->message;
  31. if ($payload->data->original->level === enum('levels.inactive.id')) {
  32. // When user level in business is zero, probably user added to business by system
  33. // And not necessary send notification to stakeholders
  34. return;
  35. }
  36. $new_user = User::findOrFail($payload->data->original->user_id);
  37. $owners = Business::findOrFail($payload->business)->owners()
  38. ->whereNotIn('id', [$new_user->id, auth()->id()])->get();
  39. $users = $owners->prepend($new_user);
  40. $this->helper->makeNotif($payload, null,
  41. ['business' => request('_business_info')['name'], 'user' => $new_user->name]
  42. )->sendNotifications($users, 'critical');
  43. }
  44. }