akaunting/app/Jobs/Auth/CreateInvitation.php

56 lines
1.4 KiB
PHP
Raw Normal View History

2022-06-01 10:15:55 +03:00
<?php
namespace App\Jobs\Auth;
use App\Abstracts\Job;
use App\Models\Auth\UserInvitation;
2022-06-28 21:45:14 +03:00
use App\Notifications\Auth\Invitation as Notification;
use App\Traits\Sources;
2022-06-28 21:45:14 +03:00
use Exception;
2022-06-01 10:15:55 +03:00
use Illuminate\Support\Str;
2022-06-28 21:45:14 +03:00
use Symfony\Component\Mailer\Exception\TransportException;
2022-06-01 10:15:55 +03:00
class CreateInvitation extends Job
{
use Sources;
2022-06-01 10:15:55 +03:00
protected $invitation;
protected $user;
2022-06-28 21:45:14 +03:00
public function __construct($user)
2022-06-01 10:15:55 +03:00
{
$this->user = $user;
}
public function handle(): UserInvitation
{
\DB::transaction(function () {
2023-04-28 13:36:11 +03:00
$invitations = UserInvitation::where('user_id', $this->user->id)->get();
foreach ($invitations as $invitation) {
$invitation->delete();
}
2022-06-01 10:15:55 +03:00
$this->invitation = UserInvitation::create([
'user_id' => $this->user->id,
'token' => (string) Str::uuid(),
'created_by' => user_id(),
'created_from' => $this->getSourceName(request()),
2022-06-01 10:15:55 +03:00
]);
2022-06-28 21:45:14 +03:00
$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);
}
});
2022-06-01 10:15:55 +03:00
return $this->invitation;
}
}