fix free subscribe issue
This commit is contained in:
@@ -129,6 +129,105 @@ class HomeController extends Controller
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function homeV3()
|
||||||
|
{
|
||||||
|
$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) {
|
||||||
|
return [
|
||||||
|
'id' => $q->law?->id,
|
||||||
|
'name' => $q->law?->title,
|
||||||
|
'is_locked' => $q->law?->is_locked,
|
||||||
|
];
|
||||||
|
});
|
||||||
|
|
||||||
|
$laws = Law::orderBy('created_at')->get()->map(function ($q) {
|
||||||
|
return [
|
||||||
|
'id' => $q->id,
|
||||||
|
'name' => $q->title,
|
||||||
|
'is_locked' => $q->is_locked,
|
||||||
|
];
|
||||||
|
});
|
||||||
|
|
||||||
|
$categories = ['hagigi', 'kifari'];
|
||||||
|
$lawsByCategory = [];
|
||||||
|
|
||||||
|
foreach ($categories as $category) {
|
||||||
|
$lawsByCategory[$category] = Law::whereHas('category', function ($q) use ($category) {
|
||||||
|
$q->where('type', $category);
|
||||||
|
})->get()->map(function ($q) {
|
||||||
|
return [
|
||||||
|
'id' => $q->id,
|
||||||
|
'title' => $q->title,
|
||||||
|
'is_locked' => $q->is_locked,
|
||||||
|
'category_id' => $q->category_id,
|
||||||
|
'price' => $q->price,
|
||||||
|
'image' => $q->image,
|
||||||
|
'type' => 'law',
|
||||||
|
];
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
$free_law = Law::where('is_locked', false)->orderBy('created_at')->get()->map(function ($q) {
|
||||||
|
return [
|
||||||
|
'id' => $q->id,
|
||||||
|
'name' => $q->title,
|
||||||
|
'is_locked' => $q->is_locked,
|
||||||
|
];
|
||||||
|
});
|
||||||
|
|
||||||
|
// --- V3 FIXED LOGIC: Fetch Active Subscription & Calculate Remaining Days ---
|
||||||
|
$activeSubscribers = $user->userSubscribers()
|
||||||
|
->with('subscribe')
|
||||||
|
->where('expired_at', '>=', now())
|
||||||
|
->get();
|
||||||
|
|
||||||
|
$current_plan = null;
|
||||||
|
|
||||||
|
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
|
||||||
|
$isFree = (bool) (
|
||||||
|
$latestSubscription->is_free ||
|
||||||
|
($latestSubscription->subscribe && $latestSubscription->subscribe->is_free)
|
||||||
|
);
|
||||||
|
|
||||||
|
$current_plan = [
|
||||||
|
'id' => $latestSubscription->id,
|
||||||
|
'name' => $latestSubscription->subscribe->name ?? 'Subscription',
|
||||||
|
'price' => $latestSubscription->subscribe->price ?? 0,
|
||||||
|
'expired_day' => $expiredDays,
|
||||||
|
'is_free' => $isFree,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
$unread_notifications_count = Notification::unreadForUser($user->id)->count();
|
||||||
|
|
||||||
|
$homeData = [
|
||||||
|
'recent' => $recent,
|
||||||
|
'laws' => $lawsByCategory,
|
||||||
|
'last_law' => $laws,
|
||||||
|
'free' => $free_law,
|
||||||
|
'current_plan' => $current_plan,
|
||||||
|
'unread_notifications_count' => $unread_notifications_count,
|
||||||
|
];
|
||||||
|
|
||||||
|
return $this->success([
|
||||||
|
'home' => $homeData,
|
||||||
|
'hoqoqi_list' => $this->getCategoryList(1), // Category 1
|
||||||
|
'keyfari_list' => $this->getCategoryList(2), // Category 2
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
private function getCategoryList(int $categoryId)
|
private function getCategoryList(int $categoryId)
|
||||||
{
|
{
|
||||||
$type = $categoryId === 1 ? 'hagigi' : 'kifari';
|
$type = $categoryId === 1 ? 'hagigi' : 'kifari';
|
||||||
|
|||||||
Reference in New Issue
Block a user