From fcaa51626d5216b536f840b8816f93ab319003cb Mon Sep 17 00:00:00 2001 From: "m.razaghi" Date: Fri, 14 Aug 2026 21:05:21 +0330 Subject: [PATCH] fix free subscribe issue --- app/Http/Controllers/api/HomeController.php | 93 ++++++++------------- 1 file changed, 37 insertions(+), 56 deletions(-) diff --git a/app/Http/Controllers/api/HomeController.php b/app/Http/Controllers/api/HomeController.php index 7bf5a80..5556954 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,7 @@ class HomeController extends Controller { $user = auth()->user(); - // --- NEW: Check and grant free subscription for existing logged-in users --- + // 1. Check and grant 30-day free subscription if eligible $this->checkAndGrantFreeSubscription($user); $this->refreshMarketSubscriptionAfterResponse($user); @@ -73,41 +73,32 @@ class HomeController extends Controller ]; }); - $freeSubscription = $user->userSubscribers() - ->whereHas('subscribe', function ($query) { - $query->where('is_free', true); - }) + // 2. Fetch Active Subscription & Calculate Remaining Days + $activeSubscribers = $user->userSubscribers() + ->with('subscribe') ->where('expired_at', '>=', now()) - ->first(); + ->get(); $current_plan = null; - if ($freeSubscription) { - $current_plan = [ - 'id' => $freeSubscription->id, - 'name' => $freeSubscription->subscribe->name, - 'price' => $freeSubscription->subscribe->price, - 'expired_day' => $freeSubscription->expired_at->diffInDays(now()), - 'is_free' => true, - ]; - } + if ($activeSubscribers->isNotEmpty()) { + $latestSubscription = $activeSubscribers->sortByDesc('expired_at')->first(); + $expiredDays = $activeSubscribers->sum(function ($subscriber) { + return $subscriber->expired_at->diffInDays(now()); + }); - $latestSubscription = $user->userSubscribers() - ->where('expired_at', '>=', now()) - ->latest('expired_at') - ->first(); + // Accurately check if the active plan is free + $isFree = (bool) ( + $latestSubscription->is_free || + ($latestSubscription->subscribe && $latestSubscription->subscribe->is_free) + ); - $expiredDays = UserSubscriber::query()->where('user_id', $user->id)->where('expired_at', '>=', now())->get()->sum(function ($subscriber) { - return $subscriber->expired_at->diffInDays(now()); - }); - - if ($latestSubscription) { $current_plan = [ 'id' => $latestSubscription->id, 'name' => $latestSubscription->subscribe->name ?? 'Subscription', - 'price' => $latestSubscription->subscribe->price ?? 100, + 'price' => $latestSubscription->subscribe->price ?? 0, 'expired_day' => $expiredDays, - 'is_free' => (bool)$latestSubscription->is_free, + 'is_free' => $isFree, ]; } @@ -124,8 +115,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), ]); } @@ -161,7 +152,7 @@ class HomeController extends Controller { $user = auth()->user(); - // --- NEW: Check and grant free subscription for existing logged-in users --- + // 1. Check and grant 30-day free subscription if eligible $this->checkAndGrantFreeSubscription($user); $this->refreshMarketSubscriptionAfterResponse($user); @@ -209,41 +200,32 @@ class HomeController extends Controller ]; }); - $freeSubscription = $user->userSubscribers() - ->whereHas('subscribe', function ($query) { - $query->where('is_free', true); - }) + // 2. Fetch Active Subscription & Calculate Remaining Days + $activeSubscribers = $user->userSubscribers() + ->with('subscribe') ->where('expired_at', '>=', now()) - ->first(); + ->get(); $current_plan = null; - if ($freeSubscription) { - $current_plan = [ - 'id' => $freeSubscription->id, - 'name' => $freeSubscription->subscribe->name, - 'price' => $freeSubscription->subscribe->price, - 'expired_day' => $freeSubscription->expired_at->diffInDays(now()), - 'is_free' => true, - ]; - } + if ($activeSubscribers->isNotEmpty()) { + $latestSubscription = $activeSubscribers->sortByDesc('expired_at')->first(); + $expiredDays = $activeSubscribers->sum(function ($subscriber) { + return $subscriber->expired_at->diffInDays(now()); + }); - $latestSubscription = $user->userSubscribers() - ->where('expired_at', '>=', now()) - ->latest('expired_at') - ->first(); + // Accurately check if the active plan is free + $isFree = (bool) ( + $latestSubscription->is_free || + ($latestSubscription->subscribe && $latestSubscription->subscribe->is_free) + ); - $expiredDays = UserSubscriber::query()->where('user_id', $user->id)->where('expired_at', '>=', now())->get()->sum(function ($subscriber) { - return $subscriber->expired_at->diffInDays(now()); - }); - - if ($latestSubscription) { $current_plan = [ 'id' => $latestSubscription->id, 'name' => $latestSubscription->subscribe->name ?? 'Subscription', - 'price' => $latestSubscription->subscribe->price ?? 100, + 'price' => $latestSubscription->subscribe->price ?? 0, 'expired_day' => $expiredDays, - 'is_free' => (bool)$latestSubscription->is_free, + 'is_free' => $isFree, ]; } @@ -273,7 +255,6 @@ class HomeController extends Controller $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),