coolify/app/Http/Livewire/Waitlist/Index.php

61 lines
2.0 KiB
PHP
Raw Normal View History

2023-08-15 14:11:38 +02:00
<?php
2023-09-01 09:34:25 +02:00
namespace App\Http\Livewire\Waitlist;
2023-08-15 14:11:38 +02:00
use App\Jobs\SendConfirmationForWaitlistJob;
2023-08-15 14:27:45 +02:00
use App\Models\User;
2023-09-01 09:34:25 +02:00
use App\Models\Waitlist;
2023-08-15 14:11:38 +02:00
use Livewire\Component;
2023-09-15 11:19:36 +02:00
use Illuminate\Support\Str;
2023-08-15 14:11:38 +02:00
2023-09-01 09:34:25 +02:00
class Index extends Component
2023-08-15 14:11:38 +02:00
{
public string $email;
public int $users = 0;
2023-09-01 09:34:25 +02:00
public int $waitingInLine = 0;
2023-08-15 14:11:38 +02:00
protected $rules = [
'email' => 'required|email',
];
2023-09-01 09:34:25 +02:00
public function render()
{
return view('livewire.waitlist.index')->layout('layouts.simple');
}
2023-08-15 14:11:38 +02:00
public function mount()
{
2023-09-01 09:34:25 +02:00
$this->waitingInLine = Waitlist::whereVerified(true)->count();
$this->users = User::count();
if (isDev()) {
$this->email = 'waitlist@example.com';
2023-08-15 14:11:38 +02:00
}
}
public function submit()
{
$this->validate();
try {
2023-08-15 14:27:45 +02:00
$already_registered = User::whereEmail($this->email)->first();
if ($already_registered) {
throw new \Exception('You are already on the waitlist or registered. <br>Please check your email to verify your email address or contact support.');
2023-08-15 14:27:45 +02:00
}
2023-09-01 09:34:25 +02:00
$found = Waitlist::where('email', $this->email)->first();
2023-08-15 14:11:38 +02:00
if ($found) {
if (!$found->verified) {
$this->emit('error', 'You are already on the waitlist. <br>Please check your email to verify your email address.');
return;
}
$this->emit('error', 'You are already on the waitlist. <br>You will be notified when your turn comes. <br>Thank you.');
2023-08-15 14:11:38 +02:00
return;
}
2023-09-01 09:34:25 +02:00
$waitlist = Waitlist::create([
2023-09-13 20:48:13 +02:00
'email' => Str::lower($this->email),
2023-08-15 14:11:38 +02:00
'type' => 'registration',
]);
2023-08-15 16:09:40 +02:00
$this->emit('success', 'Check your email to verify your email address.');
2023-08-15 14:11:38 +02:00
dispatch(new SendConfirmationForWaitlistJob($this->email, $waitlist->uuid));
2023-09-11 17:36:30 +02:00
} catch (\Throwable $e) {
return handleError($e, $this);
2023-08-15 14:11:38 +02:00
}
}
}