-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
CreateInvitation.php
50 lines (38 loc) · 1.22 KB
/
CreateInvitation.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
namespace App\Jobs\Auth;
use App\Abstracts\Job;
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
{
protected $invitation;
protected $user;
public function __construct($user)
{
$this->user = $user;
}
public function handle(): UserInvitation
{
\DB::transaction(function () {
$invitations = UserInvitation::where('user_id', $this->user->id)->get();
foreach ($invitations as $invitation) {
$invitation->delete();
}
$this->invitation = UserInvitation::create([
'user_id' => $this->user->id,
'token' => (string) Str::uuid(),
]);
$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;
}
}