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

84 lines
2.8 KiB
PHP
Raw Normal View History

2023-06-09 15:55:21 +02:00
<?php
namespace App\Http\Livewire\Team;
use App\Models\TeamInvitation;
use App\Models\User;
2023-07-28 10:55:26 +02:00
use App\Notifications\TransactionalEmails\InvitationLink;
2023-06-09 15:55:21 +02:00
use Livewire\Component;
use Visus\Cuid2\Cuid2;
class InviteLink extends Component
{
public string $email;
2023-06-12 12:00:01 +02:00
public string $role = 'member';
2023-06-09 15:55:21 +02:00
public function mount()
{
2023-06-16 11:01:27 +02:00
$this->email = isDev() ? 'test3@example.com' : '';
2023-06-09 15:55:21 +02:00
}
2023-06-12 12:00:01 +02:00
public function viaEmail()
{
$this->generate_invite_link(isEmail: true);
}
2023-06-12 12:00:01 +02:00
private function generate_invite_link(bool $isEmail = false)
2023-06-09 15:55:21 +02:00
{
try {
2023-06-12 12:00:01 +02: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 15:55:21 +02: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 12:00:01 +02: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 15:55:21 +02:00
if ($invitation->exists()) {
$created_at = $invitation->first()->created_at;
$diff = $created_at->diffInMinutes(now());
2023-06-12 12:00:01 +02: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 15:55:21 +02:00
} else {
$invitation->delete();
}
}
2023-06-12 12:00:01 +02:00
TeamInvitation::firstOrCreate([
2023-06-09 15:55:21 +02:00
'team_id' => session('currentTeam')->id,
2023-06-12 12:00:01 +02:00
'uuid' => $uuid,
2023-06-09 15:55:21 +02:00
'email' => $this->email,
2023-06-12 12:00:01 +02:00
'role' => $this->role,
2023-06-09 15:55:21 +02:00
'link' => $link,
2023-06-12 12:00:01 +02:00
'via' => $isEmail ? 'email' : 'link',
2023-06-09 15:55:21 +02:00
]);
2023-06-12 12:00:01 +02:00
if ($isEmail) {
2023-07-28 10:55:26 +02:00
$user->first()->notify(new InvitationLink);
2023-06-15 10:48:13 +02:00
$this->emit('success', 'Invitation sent via email successfully.');
2023-06-12 13:10:34 +02:00
} else {
2023-06-15 10:48:13 +02:00
$this->emit('success', 'Invitation link generated.');
2023-06-12 12:00:01 +02:00
}
$this->emit('refreshInvitations');
2023-06-09 15:55:21 +02: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 20:54:29 +02:00
public function viaLink()
2023-06-12 12:00:01 +02:00
{
$this->generate_invite_link();
}
}