lasthourcloud/app/Http/Livewire/Team/InviteLink.php

84 lines
2.8 KiB
PHP
Raw Normal View History

2023-06-09 13:55:21 +00:00
<?php
namespace App\Http\Livewire\Team;
use App\Models\TeamInvitation;
use App\Models\User;
2023-07-28 08:55:26 +00:00
use App\Notifications\TransactionalEmails\InvitationLink;
2023-06-09 13:55:21 +00:00
use Livewire\Component;
use Visus\Cuid2\Cuid2;
class InviteLink extends Component
{
public string $email;
2023-06-12 10:00:01 +00:00
public string $role = 'member';
2023-06-09 13:55:21 +00:00
public function mount()
{
2023-06-16 09:01:27 +00:00
$this->email = isDev() ? 'test3@example.com' : '';
2023-06-09 13:55:21 +00:00
}
2023-06-12 10:00:01 +00:00
public function viaEmail()
{
$this->generate_invite_link(isEmail: true);
}
2023-06-12 10:00:01 +00:00
private function generate_invite_link(bool $isEmail = false)
2023-06-09 13:55:21 +00:00
{
try {
2023-06-12 10:00:01 +00:00
$uuid = new Cuid2(32);
$link = url('/') . config('constants.invitation.link.base_url') . $uuid;
$user = User::whereEmail($this->email);
if (!$user->exists()) {
2023-06-09 13:55:21 +00:00
return general_error_handler(that: $this, customErrorMessage: "$this->email must be registered first (or activate transactional emails to invite via email).");
}
2023-06-12 10:00:01 +00:00
$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);
2023-06-09 13:55:21 +00:00
if ($invitation->exists()) {
$created_at = $invitation->first()->created_at;
$diff = $created_at->diffInMinutes(now());
2023-06-12 10:00:01 +00:00
if ($diff <= config('constants.invitation.link.expiration')) {
return general_error_handler(that: $this, customErrorMessage: "Invitation already sent to $this->email and waiting for action.");
2023-06-09 13:55:21 +00:00
} else {
$invitation->delete();
}
}
2023-06-12 10:00:01 +00:00
TeamInvitation::firstOrCreate([
2023-06-09 13:55:21 +00:00
'team_id' => session('currentTeam')->id,
2023-06-12 10:00:01 +00:00
'uuid' => $uuid,
2023-06-09 13:55:21 +00:00
'email' => $this->email,
2023-06-12 10:00:01 +00:00
'role' => $this->role,
2023-06-09 13:55:21 +00:00
'link' => $link,
2023-06-12 10:00:01 +00:00
'via' => $isEmail ? 'email' : 'link',
2023-06-09 13:55:21 +00:00
]);
2023-06-12 10:00:01 +00:00
if ($isEmail) {
2023-07-28 08:55:26 +00:00
$user->first()->notify(new InvitationLink);
2023-06-15 08:48:13 +00:00
$this->emit('success', 'Invitation sent via email successfully.');
2023-06-12 11:10:34 +00:00
} else {
2023-06-15 08:48:13 +00:00
$this->emit('success', 'Invitation link generated.');
2023-06-12 10:00:01 +00:00
}
$this->emit('refreshInvitations');
2023-06-09 13:55:21 +00:00
} catch (\Throwable $e) {
$error_message = $e->getMessage();
if ($e->getCode() === '23505') {
$error_message = 'Invitation already sent.';
}
return general_error_handler(err: $e, that: $this, customErrorMessage: $error_message);
}
}
2023-06-12 18:54:29 +00:00
public function viaLink()
2023-06-12 10:00:01 +00:00
{
$this->generate_invite_link();
}
}