From db60980dce0e749fbe24e81eb736e5f130906534 Mon Sep 17 00:00:00 2001 From: Sevan Nerse Date: Tue, 28 Jun 2022 21:45:14 +0300 Subject: [PATCH] user creating flow updated --- app/Events/Auth/InvitationCreated.php | 20 ---------------- app/Jobs/Auth/CreateInvitation.php | 28 +++++++++++------------ app/Jobs/Auth/CreateUser.php | 8 +++---- app/Listeners/Auth/SendUserInvitation.php | 22 ------------------ 4 files changed, 17 insertions(+), 61 deletions(-) delete mode 100644 app/Events/Auth/InvitationCreated.php delete mode 100644 app/Listeners/Auth/SendUserInvitation.php diff --git a/app/Events/Auth/InvitationCreated.php b/app/Events/Auth/InvitationCreated.php deleted file mode 100644 index 07741f8a3..000000000 --- a/app/Events/Auth/InvitationCreated.php +++ /dev/null @@ -1,20 +0,0 @@ -invitation = $invitation; - } -} diff --git a/app/Jobs/Auth/CreateInvitation.php b/app/Jobs/Auth/CreateInvitation.php index 6ca036b88..3d0fb9f2d 100644 --- a/app/Jobs/Auth/CreateInvitation.php +++ b/app/Jobs/Auth/CreateInvitation.php @@ -3,9 +3,11 @@ namespace App\Jobs\Auth; use App\Abstracts\Job; -use App\Events\Auth\InvitationCreated; use App\Models\Auth\UserInvitation; +use App\Notifications\Auth\Invitation as Notification; +use Exception; use Illuminate\Support\Str; +use Symfony\Component\Mailer\Exception\TransportException; class CreateInvitation extends Job { @@ -13,31 +15,29 @@ class CreateInvitation extends Job protected $user; - protected $company; - - public function __construct($user, $company) + public function __construct($user) { $this->user = $user; - $this->company = $company; } public function handle(): UserInvitation { \DB::transaction(function () { - if ($this->user->hasPendingInvitation($this->company->id)) { - $pending_invitation = $this->user->getPendingInvitation($this->company->id); - - $this->dispatch(new DeleteInvitation($pending_invitation)); - } - $this->invitation = UserInvitation::create([ 'user_id' => $this->user->id, - 'company_id' => $this->company->id, 'token' => (string) Str::uuid(), ]); - }); - event(new InvitationCreated($this->invitation)); + $notification = new Notification($this->invitation); + + try { + $this->dispatch(new NotifyUser($this->user, $notification)); + } catch (TransportException $e) { + $message = trans('errors.title.500'); + + throw new Exception($message); + } + }); return $this->invitation; } diff --git a/app/Jobs/Auth/CreateUser.php b/app/Jobs/Auth/CreateUser.php index 2d653080c..1e688d146 100644 --- a/app/Jobs/Auth/CreateUser.php +++ b/app/Jobs/Auth/CreateUser.php @@ -69,12 +69,10 @@ class CreateUser extends Job implements HasOwner, HasSource, ShouldCreate 'user' => $this->model->id, 'company' => $company->id, ]); + } - if (app()->runningInConsole() || request()->isInstall()) { - continue; - } - - $this->dispatch(new CreateInvitation($this->model, $company)); + if (! app()->runningInConsole() && ! request()->isInstall()) { + $this->dispatch(new CreateInvitation($this->model)); } }); diff --git a/app/Listeners/Auth/SendUserInvitation.php b/app/Listeners/Auth/SendUserInvitation.php deleted file mode 100644 index 9d9d9cdf8..000000000 --- a/app/Listeners/Auth/SendUserInvitation.php +++ /dev/null @@ -1,22 +0,0 @@ -invitation; - - $invitation->user->notify(new Notification($invitation)); - } -}