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.

51 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 stockholders
  34. return;
  35. }
  36. $new_user = User::findOrFail($payload->data->original->user_id);
  37. $owners = Business::findOrFail($payload->business)->owners()->where('id', '!=', $new_user->id)->get();
  38. $users = $owners->prepend($new_user);
  39. $this->helper->makeNotif($payload, null,
  40. ['business' => request('_business_info')['name'], 'user' => $new_user->name]
  41. )->sendNotifications($users, 'critical');
  42. }
  43. }