From 94aab63db698f00449f6bf8ed591dfefffd20e3f Mon Sep 17 00:00:00 2001 From: "m.razaghi" Date: Sun, 16 Aug 2026 15:11:11 +0330 Subject: [PATCH] Updated HomeController.php --- app/Http/Controllers/api/HomeController.php | 37 ++++++++++++--------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/app/Http/Controllers/api/HomeController.php b/app/Http/Controllers/api/HomeController.php index fcfed31..6dc20ca 100644 --- a/app/Http/Controllers/api/HomeController.php +++ b/app/Http/Controllers/api/HomeController.php @@ -7,7 +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\SubscribePlan; use App\Models\UserSubscriber; use App\Services\AppMarketPurchaseVerifier; use App\Traits\BaseApiResponse; @@ -25,7 +25,6 @@ class HomeController extends Controller { $user = auth()->user(); - // --- NEW: Check and grant free subscription for existing logged-in users --- $this->checkAndGrantFreeSubscription($user); $this->refreshMarketSubscriptionAfterResponse($user); @@ -124,8 +123,8 @@ class HomeController extends Controller return $this->success([ 'home' => $homeData, - 'hoqoqi_list' => $this->getCategoryList(1), // Category 1 - 'keyfari_list' => $this->getCategoryList(2), // Category 2 + 'hoqoqi_list' => $this->getCategoryList(1), + 'keyfari_list' => $this->getCategoryList(2), ]); } @@ -133,7 +132,6 @@ class HomeController extends Controller { $user = auth()->user(); - // --- NEW: Check and grant free subscription for existing logged-in users --- $this->checkAndGrantFreeSubscription($user); $this->refreshMarketSubscriptionAfterResponse($user); @@ -181,7 +179,7 @@ class HomeController extends Controller ]; }); - // --- V3 FIXED LOGIC: Fetch Active Subscription & Calculate Remaining Days --- + // --- V3 FIXED LOGIC --- $activeSubscribers = $user->userSubscribers() ->with('subscribe') ->where('expired_at', '>=', now()) @@ -191,11 +189,11 @@ class HomeController extends Controller if ($activeSubscribers->isNotEmpty()) { $latestSubscription = $activeSubscribers->sortByDesc('expired_at')->first(); - $expiredDays = $activeSubscribers->sum(function ($subscriber) { - return $subscriber->expired_at->diffInDays(now()); - }); - // Accurately check if the active plan is free + // FIX 1: Never sum the days. Calculate distance to the furthest expiration date. + $expiredDays = $latestSubscription->expired_at->diffInDays(now()); + + // FIX 2: Accurately check if the active plan is free $isFree = (bool) ( $latestSubscription->is_free || ($latestSubscription->subscribe && $latestSubscription->subscribe->is_free) @@ -223,8 +221,8 @@ class HomeController extends Controller return $this->success([ 'home' => $homeData, - 'hoqoqi_list' => $this->getCategoryList(1), // Category 1 - 'keyfari_list' => $this->getCategoryList(2), // Category 2 + 'hoqoqi_list' => $this->getCategoryList(1), + 'keyfari_list' => $this->getCategoryList(2), ]); } @@ -260,7 +258,6 @@ class HomeController extends Controller { $user = auth()->user(); - // --- NEW: Check and grant free subscription for existing logged-in users --- $this->checkAndGrantFreeSubscription($user); $this->refreshMarketSubscriptionAfterResponse($user); @@ -359,20 +356,28 @@ class HomeController extends Controller } /** - * Helper to automatically assign a free plan if the user has never had one. + * Helper to automatically assign a free plan safely. */ private function checkAndGrantFreeSubscription($user): void { + // 1. Have they ever used a free plan? $hasUsedFreePlan = $user->userSubscribers() ->whereHas('subscribe', function ($query) { $query->where('is_free', true); })->exists(); - if (!$hasUsedFreePlan) { + // 2. Do they currently have an active PAID plan? + $hasActivePaidPlan = $user->userSubscribers() + ->where('expired_at', '>', now()) + ->whereHas('subscribe', function ($query) { + $query->where('is_free', false); + })->exists(); + + // FIX 3: Only grant the free plan if they have never had one AND they aren't currently paying! + if (!$hasUsedFreePlan && !$hasActivePaidPlan) { $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),