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\ProjectUserCreate;
  4. use App\Models\Project;
  5. use App\Models\User;
  6. use App\Utilities\HelperClass\NotificationHelper;
  7. class ProjectUserCreateNotif
  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 ProjectUserCreate $event
  26. * @return void
  27. */
  28. public function handle(ProjectUserCreate $event)
  29. {
  30. $payload = $event->message;
  31. $new_user = User::findOrFail($payload->data->original->user_id);
  32. $project = Project::findOrFail($payload->data->original->project_id);
  33. $owners_id = request('_business_info')['info']['projects'][$project->id]['members']->reject(function ($item, $key) use ($new_user) {
  34. return $item['level'] < enum('levels.owner.id') || $key === $new_user->id;
  35. })->toArray();
  36. $owners = User::whereIn('id', array_keys($owners_id))->get();
  37. $users = $owners->prepend($new_user);
  38. $this->helper->makeNotif($payload, null,
  39. ['project' => $project->name, 'user' => $new_user->name]
  40. )->sendNotifications($users, 'critical');
  41. }
  42. }