2024-02-08 10:45:19 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Livewire\Admin;
|
|
|
|
|
|
|
|
use App\Models\User;
|
2024-02-26 09:25:21 +00:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2024-02-08 10:45:19 +00:00
|
|
|
use Livewire\Component;
|
|
|
|
|
|
|
|
class Index extends Component
|
|
|
|
{
|
2024-04-02 13:00:01 +00:00
|
|
|
public $active_subscribers = [];
|
|
|
|
public $inactive_subscribers = [];
|
|
|
|
public $search = '';
|
|
|
|
public function submitSearch() {
|
|
|
|
if ($this->search !== "") {
|
|
|
|
$this->inactive_subscribers = User::whereDoesntHave('teams', function ($query) {
|
|
|
|
$query->whereRelation('subscription', 'stripe_subscription_id', '!=', null);
|
|
|
|
})->where(function ($query) {
|
|
|
|
$query->where('name', 'like', "%{$this->search}%")
|
|
|
|
->orWhere('email', 'like', "%{$this->search}%");
|
|
|
|
})->get()->filter(function ($user) {
|
|
|
|
return $user->id !== 0;
|
|
|
|
});
|
|
|
|
$this->active_subscribers = User::whereHas('teams', function ($query) {
|
|
|
|
$query->whereRelation('subscription', 'stripe_subscription_id', '!=', null);
|
|
|
|
})->where(function ($query) {
|
|
|
|
$query->where('name', 'like', "%{$this->search}%")
|
|
|
|
->orWhere('email', 'like', "%{$this->search}%");
|
|
|
|
})->get()->filter(function ($user) {
|
|
|
|
return $user->id !== 0;
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
$this->getSubscribers();
|
|
|
|
}
|
|
|
|
}
|
2024-02-08 10:45:19 +00:00
|
|
|
public function mount()
|
|
|
|
{
|
2024-02-08 13:01:16 +00:00
|
|
|
if (!isCloud()) {
|
|
|
|
return redirect()->route('dashboard');
|
|
|
|
}
|
2024-02-26 10:48:35 +00:00
|
|
|
if (auth()->user()->id !== 0) {
|
2024-02-08 10:45:19 +00:00
|
|
|
return redirect()->route('dashboard');
|
|
|
|
}
|
2024-04-02 13:00:01 +00:00
|
|
|
$this->getSubscribers();
|
|
|
|
}
|
|
|
|
public function getSubscribers() {
|
|
|
|
$this->inactive_subscribers = User::whereDoesntHave('teams', function ($query) {
|
|
|
|
$query->whereRelation('subscription', 'stripe_subscription_id', '!=', null);
|
|
|
|
})->get()->filter(function ($user) {
|
|
|
|
return $user->id !== 0;
|
|
|
|
});
|
|
|
|
$this->active_subscribers = User::whereHas('teams', function ($query) {
|
2024-02-08 10:45:19 +00:00
|
|
|
$query->whereRelation('subscription', 'stripe_subscription_id', '!=', null);
|
2024-02-26 10:48:35 +00:00
|
|
|
})->get()->filter(function ($user) {
|
|
|
|
return $user->id !== 0;
|
|
|
|
});
|
2024-02-08 10:45:19 +00:00
|
|
|
}
|
|
|
|
public function switchUser(int $user_id)
|
|
|
|
{
|
2024-02-26 10:48:35 +00:00
|
|
|
if (auth()->user()->id !== 0) {
|
|
|
|
return redirect()->route('dashboard');
|
|
|
|
}
|
2024-02-08 10:45:19 +00:00
|
|
|
$user = User::find($user_id);
|
2024-02-26 10:48:35 +00:00
|
|
|
$team_to_switch_to = $user->teams->first();
|
|
|
|
Cache::forget("team:{$user->id}");
|
2024-02-08 10:45:19 +00:00
|
|
|
auth()->login($user);
|
2024-02-26 10:48:35 +00:00
|
|
|
refreshSession($team_to_switch_to);
|
|
|
|
return redirect(request()->header('Referer'));
|
2024-02-08 10:45:19 +00:00
|
|
|
}
|
|
|
|
public function render()
|
|
|
|
{
|
|
|
|
return view('livewire.admin.index');
|
|
|
|
}
|
|
|
|
}
|