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

98 lines
3.6 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-09-15 09:19:36 +00:00
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Support\Facades\Artisan;
2023-06-09 13:55:21 +00:00
use Livewire\Component;
use Visus\Cuid2\Cuid2;
2023-09-15 09:19:36 +00:00
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
2023-06-09 13:55:21 +00:00
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()
{
$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()
{
2023-09-15 09:19:36 +00:00
$this->generate_invite_link(sendEmail: true);
2023-06-12 10:00:01 +00:00
}
2023-09-15 09:19:36 +00:00
public function viaLink()
{
$this->generate_invite_link(sendEmail: false);
}
private function generate_invite_link(bool $sendEmail = false)
2023-06-09 13:55:21 +00:00
{
try {
2023-08-22 15:44:49 +00:00
$member_emails = currentTeam()->members()->get()->pluck('email');
2023-06-12 10:00:01 +00:00
if ($member_emails->contains($this->email)) {
2023-08-22 15:44:49 +00:00
return general_error_handler(that: $this, customErrorMessage: "$this->email is already a member of " . currentTeam()->name . ".");
2023-06-12 10:00:01 +00:00
}
2023-09-15 09:19:36 +00:00
$uuid = new Cuid2(32);
$link = url('/') . config('constants.invitation.link.base_url') . $uuid;
$user = User::whereEmail($this->email)->first();
2023-06-12 10:00:01 +00:00
2023-09-15 09:19:36 +00:00
if (is_null($user)) {
$password = Str::password();
$user = User::create([
'name' => Str::of($this->email)->before('@'),
'email' => $this->email,
'password' => Hash::make($password),
'force_password_reset' => true,
]);
$token = Crypt::encryptString("{$user->email}@@@$password");
$link = route('auth.link', ['token' => $token]);
}
$invitation = TeamInvitation::whereEmail($this->email)->first();
if (!is_null($invitation)) {
$invitationValid = $invitation->isValid();
if ($invitationValid) {
return general_error_handler(that: $this, customErrorMessage: "Pending invitation already exists for $this->email.");
2023-06-09 13:55:21 +00:00
} else {
$invitation->delete();
}
}
2023-06-12 10:00:01 +00:00
2023-09-15 09:19:36 +00:00
$invitation = TeamInvitation::firstOrCreate([
2023-08-22 15:44:49 +00:00
'team_id' => 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-09-15 09:19:36 +00:00
'via' => $sendEmail ? 'email' : 'link',
2023-06-09 13:55:21 +00:00
]);
2023-09-15 09:19:36 +00:00
if ($sendEmail) {
$mail = new MailMessage();
$mail->view('emails.invitation-link', [
'team' => currentTeam()->name,
'invitation_link' => $link,
]);
$mail->subject('You have been invited to ' . currentTeam()->name . ' on ' . config('app.name') . '.');
send_user_an_email($mail, $this->email);
2023-06-15 08:48:13 +00:00
$this->emit('success', 'Invitation sent via email successfully.');
2023-09-15 09:19:36 +00:00
$this->emit('refreshInvitations');
return;
2023-06-12 11:10:34 +00:00
} else {
2023-06-15 08:48:13 +00:00
$this->emit('success', 'Invitation link generated.');
2023-09-15 09:19:36 +00:00
$this->emit('refreshInvitations');
2023-06-12 10:00:01 +00:00
}
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);
}
}
}