2023-06-09 13:55:21 +00:00
< ? php
namespace App\Http\Livewire\Team ;
use App\Models\TeamInvitation ;
use App\Models\User ;
2023-06-12 10:00:01 +00:00
use App\Notifications\TransactionalEmails\InvitationLinkEmail ;
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-12 10:00:01 +00:00
$this -> email = config ( 'app.env' ) === 'local' ? '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:21:48 +00:00
$this -> emit ( 'message' , 'Invitation sent via email successfully.' );
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 ) {
$user -> first () -> notify ( new InvitationLinkEmail ());
}
$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 10:00:01 +00:00
public function inviteByLink ()
{
$this -> generate_invite_link ();
2023-06-12 10:21:48 +00:00
$this -> emit ( 'message' , 'Invitation link generated.' );
2023-06-12 10:00:01 +00:00
}
2023-06-09 13:55:21 +00:00
}