Updated HomeController.php
This commit is contained in:
@@ -7,7 +7,7 @@ use App\Models\Law;
|
|||||||
use App\Models\Notification;
|
use App\Models\Notification;
|
||||||
use App\Models\Category;
|
use App\Models\Category;
|
||||||
use App\Models\RecentArt;
|
use App\Models\RecentArt;
|
||||||
use App\Models\SubscribePlan; // <-- Added this import
|
use App\Models\SubscribePlan;
|
||||||
use App\Models\UserSubscriber;
|
use App\Models\UserSubscriber;
|
||||||
use App\Services\AppMarketPurchaseVerifier;
|
use App\Services\AppMarketPurchaseVerifier;
|
||||||
use App\Traits\BaseApiResponse;
|
use App\Traits\BaseApiResponse;
|
||||||
@@ -25,7 +25,6 @@ class HomeController extends Controller
|
|||||||
{
|
{
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
|
|
||||||
// --- NEW: Check and grant free subscription for existing logged-in users ---
|
|
||||||
$this->checkAndGrantFreeSubscription($user);
|
$this->checkAndGrantFreeSubscription($user);
|
||||||
|
|
||||||
$this->refreshMarketSubscriptionAfterResponse($user);
|
$this->refreshMarketSubscriptionAfterResponse($user);
|
||||||
@@ -124,8 +123,8 @@ class HomeController extends Controller
|
|||||||
|
|
||||||
return $this->success([
|
return $this->success([
|
||||||
'home' => $homeData,
|
'home' => $homeData,
|
||||||
'hoqoqi_list' => $this->getCategoryList(1), // Category 1
|
'hoqoqi_list' => $this->getCategoryList(1),
|
||||||
'keyfari_list' => $this->getCategoryList(2), // Category 2
|
'keyfari_list' => $this->getCategoryList(2),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -133,7 +132,6 @@ class HomeController extends Controller
|
|||||||
{
|
{
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
|
|
||||||
// --- NEW: Check and grant free subscription for existing logged-in users ---
|
|
||||||
$this->checkAndGrantFreeSubscription($user);
|
$this->checkAndGrantFreeSubscription($user);
|
||||||
|
|
||||||
$this->refreshMarketSubscriptionAfterResponse($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()
|
$activeSubscribers = $user->userSubscribers()
|
||||||
->with('subscribe')
|
->with('subscribe')
|
||||||
->where('expired_at', '>=', now())
|
->where('expired_at', '>=', now())
|
||||||
@@ -191,11 +189,11 @@ class HomeController extends Controller
|
|||||||
|
|
||||||
if ($activeSubscribers->isNotEmpty()) {
|
if ($activeSubscribers->isNotEmpty()) {
|
||||||
$latestSubscription = $activeSubscribers->sortByDesc('expired_at')->first();
|
$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) (
|
$isFree = (bool) (
|
||||||
$latestSubscription->is_free ||
|
$latestSubscription->is_free ||
|
||||||
($latestSubscription->subscribe && $latestSubscription->subscribe->is_free)
|
($latestSubscription->subscribe && $latestSubscription->subscribe->is_free)
|
||||||
@@ -223,8 +221,8 @@ class HomeController extends Controller
|
|||||||
|
|
||||||
return $this->success([
|
return $this->success([
|
||||||
'home' => $homeData,
|
'home' => $homeData,
|
||||||
'hoqoqi_list' => $this->getCategoryList(1), // Category 1
|
'hoqoqi_list' => $this->getCategoryList(1),
|
||||||
'keyfari_list' => $this->getCategoryList(2), // Category 2
|
'keyfari_list' => $this->getCategoryList(2),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -260,7 +258,6 @@ class HomeController extends Controller
|
|||||||
{
|
{
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
|
|
||||||
// --- NEW: Check and grant free subscription for existing logged-in users ---
|
|
||||||
$this->checkAndGrantFreeSubscription($user);
|
$this->checkAndGrantFreeSubscription($user);
|
||||||
|
|
||||||
$this->refreshMarketSubscriptionAfterResponse($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
|
private function checkAndGrantFreeSubscription($user): void
|
||||||
{
|
{
|
||||||
|
// 1. Have they ever used a free plan?
|
||||||
$hasUsedFreePlan = $user->userSubscribers()
|
$hasUsedFreePlan = $user->userSubscribers()
|
||||||
->whereHas('subscribe', function ($query) {
|
->whereHas('subscribe', function ($query) {
|
||||||
$query->where('is_free', true);
|
$query->where('is_free', true);
|
||||||
})->exists();
|
})->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();
|
$freePlan = SubscribePlan::where('is_free', true)->where('is_active', true)->first();
|
||||||
|
|
||||||
if ($freePlan) {
|
if ($freePlan) {
|
||||||
// Exactly matching the logic to keep the UI clean
|
|
||||||
$user->userSubscribers()->create([
|
$user->userSubscribers()->create([
|
||||||
'subscribe_plan_id' => $freePlan->id,
|
'subscribe_plan_id' => $freePlan->id,
|
||||||
'expired_at' => now()->addDays($freePlan->expired_day + 1),
|
'expired_at' => now()->addDays($freePlan->expired_day + 1),
|
||||||
|
|||||||
Reference in New Issue
Block a user