fix free subscribe issue

This commit is contained in:
m.razaghi
2026-08-14 20:28:09 +03:30
parent 52a13dcf6e
commit d7f6131a33

View File

@@ -7,6 +7,7 @@ use App\Models\Law;
use App\Models\Notification;
use App\Models\Category;
use App\Models\RecentArt;
use App\Models\SubscribePlan; // <-- Added this import
use App\Models\UserSubscriber;
use App\Services\AppMarketPurchaseVerifier;
use App\Traits\BaseApiResponse;
@@ -23,6 +24,10 @@ class HomeController extends Controller
public function homeV2()
{
$user = auth()->user();
// --- NEW: Check and grant free subscription for existing logged-in users ---
$this->checkAndGrantFreeSubscription($user);
$this->refreshMarketSubscriptionAfterResponse($user);
$recent = RecentArt::query()->where('user_id', $user->id)->get()->map(function ($q) {
@@ -152,11 +157,13 @@ class HomeController extends Controller
return $data;
}
public function index()
{
$user = auth()->user();
// --- NEW: Check and grant free subscription for existing logged-in users ---
$this->checkAndGrantFreeSubscription($user);
$this->refreshMarketSubscriptionAfterResponse($user);
$recent = RecentArt::query()->where('user_id', $user->id)->get()->map(function ($q) {
@@ -252,6 +259,29 @@ class HomeController extends Controller
]);
}
/**
* Helper to automatically assign a free plan if the user has never had one.
*/
private function checkAndGrantFreeSubscription($user): void
{
$hasUsedFreePlan = $user->userSubscribers()
->whereHas('subscribe', function ($query) {
$query->where('is_free', true);
})->exists();
if (!$hasUsedFreePlan) {
$freePlan = SubscribePlan::where('is_free', true)->where('is_active', true)->first();
if ($freePlan) {
// Exactly matching the logic to keep the UI clean
$user->userSubscribers()->create([
'subscribe_plan_id' => $freePlan->id,
'expired_at' => now()->addDays($freePlan->expired_day + 1),
]);
}
}
}
private function refreshMarketSubscriptionAfterResponse($user): void
{
$cacheKey = "home-market-refresh-user-{$user->id}";