updates
This commit is contained in:
@@ -4,42 +4,64 @@ namespace App\Http\Livewire\Team;
|
||||
|
||||
use App\Models\TeamInvitation;
|
||||
use App\Models\User;
|
||||
use App\Notifications\TransactionalEmails\InvitationLinkEmail;
|
||||
use Livewire\Component;
|
||||
use Visus\Cuid2\Cuid2;
|
||||
|
||||
class InviteLink extends Component
|
||||
{
|
||||
public string $email;
|
||||
public string $role = 'member';
|
||||
public function mount()
|
||||
{
|
||||
$this->email = config('app.env') === 'local' ? 'test@example.com' : '';
|
||||
$this->email = config('app.env') === 'local' ? 'test3@example.com' : '';
|
||||
}
|
||||
public function inviteByLink()
|
||||
public function viaEmail()
|
||||
{
|
||||
$this->generate_invite_link(isEmail: true);
|
||||
}
|
||||
private function generate_invite_link(bool $isEmail = false)
|
||||
{
|
||||
$uuid = new Cuid2(32);
|
||||
$link = url('/') . '/api/invitation/' . $uuid;
|
||||
try {
|
||||
$user_exists = User::whereEmail($this->email)->exists();
|
||||
if (!$user_exists) {
|
||||
$uuid = new Cuid2(32);
|
||||
$link = url('/') . config('constants.invitation.link.base_url') . $uuid;
|
||||
|
||||
$user = User::whereEmail($this->email);
|
||||
|
||||
if (!$user->exists()) {
|
||||
return general_error_handler(that: $this, customErrorMessage: "$this->email must be registered first (or activate transactional emails to invite via email).");
|
||||
}
|
||||
$invitation = TeamInvitation::where('email', $this->email);
|
||||
|
||||
$member_emails = session('currentTeam')->members()->get()->pluck('email');
|
||||
if ($member_emails->contains($this->email)) {
|
||||
return general_error_handler(that: $this, customErrorMessage: "$this->email is already a member of " . session('currentTeam')->name . ".");
|
||||
}
|
||||
|
||||
$invitation = TeamInvitation::whereEmail($this->email);
|
||||
|
||||
if ($invitation->exists()) {
|
||||
$created_at = $invitation->first()->created_at;
|
||||
$diff = $created_at->diffInMinutes(now());
|
||||
if ($diff < 11) {
|
||||
return general_error_handler(that: $this, customErrorMessage: "Invitation already sent and active for $this->email.");
|
||||
if ($diff <= config('constants.invitation.link.expiration')) {
|
||||
return general_error_handler(that: $this, customErrorMessage: "Invitation already sent to $this->email and waiting for action.");
|
||||
} else {
|
||||
$invitation->delete();
|
||||
}
|
||||
}
|
||||
$invitation = TeamInvitation::firstOrCreate([
|
||||
|
||||
TeamInvitation::firstOrCreate([
|
||||
'team_id' => session('currentTeam')->id,
|
||||
'uuid' => $uuid,
|
||||
'email' => $this->email,
|
||||
'role' => 'readonly',
|
||||
'role' => $this->role,
|
||||
'link' => $link,
|
||||
'via' => $isEmail ? 'email' : 'link',
|
||||
]);
|
||||
$this->emit('reloadWindow');
|
||||
if ($isEmail) {
|
||||
$user->first()->notify(new InvitationLinkEmail());
|
||||
}
|
||||
$this->emit('refreshInvitations');
|
||||
$this->emit('message', 'Invitation sent successfully.');
|
||||
} catch (\Throwable $e) {
|
||||
$error_message = $e->getMessage();
|
||||
if ($e->getCode() === '23505') {
|
||||
@@ -48,4 +70,8 @@ class InviteLink extends Component
|
||||
return general_error_handler(err: $e, that: $this, customErrorMessage: $error_message);
|
||||
}
|
||||
}
|
||||
public function inviteByLink()
|
||||
{
|
||||
$this->generate_invite_link();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user