2023-08-17 13:19:37 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
|
|
|
|
use App\Models\User;
|
|
|
|
use App\Models\Waitlist;
|
|
|
|
use Illuminate\Console\Command;
|
|
|
|
use Illuminate\Notifications\Messages\MailMessage;
|
2023-09-06 10:07:34 +00:00
|
|
|
use Illuminate\Support\Facades\Crypt;
|
2023-08-17 13:19:37 +00:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
2023-09-05 08:22:24 +00:00
|
|
|
class WaitlistInvite extends Command
|
2023-08-17 13:19:37 +00:00
|
|
|
{
|
2023-09-06 10:07:34 +00:00
|
|
|
public Waitlist|User|null $next_patient = null;
|
2023-08-17 13:19:37 +00:00
|
|
|
public string|null $password = null;
|
|
|
|
/**
|
|
|
|
* The name and signature of the console command.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2023-09-06 10:07:34 +00:00
|
|
|
protected $signature = 'waitlist:invite {email?} {--only-email}';
|
2023-08-17 13:19:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The console command description.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2023-08-17 14:12:08 +00:00
|
|
|
protected $description = 'Send invitation to the next user (or by email) in the waitlist';
|
2023-08-17 13:19:37 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the console command.
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2023-08-17 14:12:08 +00:00
|
|
|
if ($this->argument('email')) {
|
2023-09-06 10:07:34 +00:00
|
|
|
if ($this->option('only-email')) {
|
|
|
|
$this->next_patient = User::whereEmail($this->argument('email'))->first();
|
|
|
|
$this->password = Str::password();
|
|
|
|
$this->next_patient->update([
|
|
|
|
'password' => Hash::make($this->password),
|
|
|
|
'force_password_reset' => true,
|
|
|
|
]);
|
|
|
|
} else {
|
|
|
|
$this->next_patient = Waitlist::where('email', $this->argument('email'))->first();
|
|
|
|
}
|
2023-08-17 14:12:08 +00:00
|
|
|
if (!$this->next_patient) {
|
|
|
|
$this->error("{$this->argument('email')} not found in the waitlist.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->next_patient = Waitlist::orderBy('created_at', 'asc')->where('verified', true)->first();
|
|
|
|
}
|
2023-08-17 13:19:37 +00:00
|
|
|
if ($this->next_patient) {
|
2023-09-06 10:07:34 +00:00
|
|
|
if ($this->option('only-email')) {
|
|
|
|
$this->send_email();
|
|
|
|
return;
|
|
|
|
}
|
2023-08-17 13:19:37 +00:00
|
|
|
$this->register_user();
|
|
|
|
$this->remove_from_waitlist();
|
|
|
|
$this->send_email();
|
|
|
|
} else {
|
2023-08-17 13:24:26 +00:00
|
|
|
$this->info('No verified user found in the waitlist. 👀');
|
2023-08-17 13:19:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
private function register_user()
|
|
|
|
{
|
|
|
|
$already_registered = User::whereEmail($this->next_patient->email)->first();
|
|
|
|
if (!$already_registered) {
|
|
|
|
$this->password = Str::password();
|
2023-09-06 10:07:34 +00:00
|
|
|
User::create([
|
2023-08-17 13:19:37 +00:00
|
|
|
'name' => Str::of($this->next_patient->email)->before('@'),
|
|
|
|
'email' => $this->next_patient->email,
|
|
|
|
'password' => Hash::make($this->password),
|
|
|
|
'force_password_reset' => true,
|
|
|
|
]);
|
|
|
|
$this->info("User registered ({$this->next_patient->email}) successfully. 🎉");
|
|
|
|
} else {
|
|
|
|
throw new \Exception('User already registered');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private function remove_from_waitlist()
|
|
|
|
{
|
|
|
|
$this->next_patient->delete();
|
|
|
|
$this->info("User removed from waitlist successfully.");
|
|
|
|
}
|
|
|
|
private function send_email()
|
|
|
|
{
|
2023-09-06 10:07:34 +00:00
|
|
|
ray($this->next_patient->email, $this->password);
|
|
|
|
$token = Crypt::encryptString("{$this->next_patient->email}@@@$this->password");
|
|
|
|
$loginLink = route('auth.link', ['token' => $token]);
|
2023-08-17 13:19:37 +00:00
|
|
|
$mail = new MailMessage();
|
|
|
|
$mail->view('emails.waitlist-invitation', [
|
|
|
|
'email' => $this->next_patient->email,
|
|
|
|
'password' => $this->password,
|
2023-09-06 10:07:34 +00:00
|
|
|
'loginLink' => $loginLink,
|
2023-08-17 13:19:37 +00:00
|
|
|
]);
|
|
|
|
$mail->subject('Congratulations! You are invited to join Coolify Cloud.');
|
|
|
|
send_user_an_email($mail, $this->next_patient->email);
|
|
|
|
$this->info("Email sent successfully. 📧");
|
|
|
|
}
|
|
|
|
}
|