fix free subscribe issue
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;
|
||||||
@@ -21,11 +21,206 @@ class HomeController extends Controller
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* LEGACY API: Standard Home (No auto-grant)
|
||||||
|
*/
|
||||||
|
public function index()
|
||||||
|
{
|
||||||
|
$user = auth()->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,
|
||||||
|
];
|
||||||
|
});
|
||||||
|
|
||||||
|
// 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();
|
||||||
|
|
||||||
|
return $this->success([
|
||||||
|
'recent' => $recent,
|
||||||
|
'laws' => $lawsByCategory,
|
||||||
|
'last_law' => $laws,
|
||||||
|
'free' => $free_law,
|
||||||
|
'current_plan' => $current_plan,
|
||||||
|
'unread_notifications_count' => $unread_notifications_count,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* V2 API: Home V2 (No auto-grant)
|
||||||
|
*/
|
||||||
public function homeV2()
|
public function homeV2()
|
||||||
{
|
{
|
||||||
$user = auth()->user();
|
$user = auth()->user();
|
||||||
|
|
||||||
// --- NEW: Check and grant free subscription for existing logged-in users ---
|
$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,
|
||||||
|
];
|
||||||
|
});
|
||||||
|
|
||||||
|
// 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
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* V3 API: Home V3 (WITH Auto-Grant Free Subscription Logic)
|
||||||
|
*/
|
||||||
|
public function homeV3()
|
||||||
|
{
|
||||||
|
$user = auth()->user();
|
||||||
|
|
||||||
|
// --- NEW IN V3: Check and grant free subscription for existing logged-in users ---
|
||||||
$this->checkAndGrantFreeSubscription($user);
|
$this->checkAndGrantFreeSubscription($user);
|
||||||
|
|
||||||
$this->refreshMarketSubscriptionAfterResponse($user);
|
$this->refreshMarketSubscriptionAfterResponse($user);
|
||||||
@@ -73,41 +268,32 @@ class HomeController extends Controller
|
|||||||
];
|
];
|
||||||
});
|
});
|
||||||
|
|
||||||
$freeSubscription = $user->userSubscribers()
|
// Fetch Active Subscription & Calculate Remaining Days
|
||||||
->whereHas('subscribe', function ($query) {
|
$activeSubscribers = $user->userSubscribers()
|
||||||
$query->where('is_free', true);
|
->with('subscribe')
|
||||||
})
|
|
||||||
->where('expired_at', '>=', now())
|
->where('expired_at', '>=', now())
|
||||||
->first();
|
->get();
|
||||||
|
|
||||||
$current_plan = null;
|
$current_plan = null;
|
||||||
|
|
||||||
if ($freeSubscription) {
|
if ($activeSubscribers->isNotEmpty()) {
|
||||||
$current_plan = [
|
$latestSubscription = $activeSubscribers->sortByDesc('expired_at')->first();
|
||||||
'id' => $freeSubscription->id,
|
$expiredDays = $activeSubscribers->sum(function ($subscriber) {
|
||||||
'name' => $freeSubscription->subscribe->name,
|
|
||||||
'price' => $freeSubscription->subscribe->price,
|
|
||||||
'expired_day' => $freeSubscription->expired_at->diffInDays(now()),
|
|
||||||
'is_free' => true,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
$latestSubscription = $user->userSubscribers()
|
|
||||||
->where('expired_at', '>=', now())
|
|
||||||
->latest('expired_at')
|
|
||||||
->first();
|
|
||||||
|
|
||||||
$expiredDays = UserSubscriber::query()->where('user_id', $user->id)->where('expired_at', '>=', now())->get()->sum(function ($subscriber) {
|
|
||||||
return $subscriber->expired_at->diffInDays(now());
|
return $subscriber->expired_at->diffInDays(now());
|
||||||
});
|
});
|
||||||
|
|
||||||
if ($latestSubscription) {
|
// Accurately check if the active plan is free
|
||||||
|
$isFree = (bool) (
|
||||||
|
$latestSubscription->is_free ||
|
||||||
|
($latestSubscription->subscribe && $latestSubscription->subscribe->is_free)
|
||||||
|
);
|
||||||
|
|
||||||
$current_plan = [
|
$current_plan = [
|
||||||
'id' => $latestSubscription->id,
|
'id' => $latestSubscription->id,
|
||||||
'name' => $latestSubscription->subscribe->name ?? 'Subscription',
|
'name' => $latestSubscription->subscribe->name ?? 'Subscription',
|
||||||
'price' => $latestSubscription->subscribe->price ?? 100,
|
'price' => $latestSubscription->subscribe->price ?? 0,
|
||||||
'expired_day' => $expiredDays,
|
'expired_day' => $expiredDays,
|
||||||
'is_free' => (bool)$latestSubscription->is_free,
|
'is_free' => $isFree,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -129,6 +315,9 @@ class HomeController extends Controller
|
|||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Helper methods below
|
||||||
|
*/
|
||||||
private function getCategoryList(int $categoryId)
|
private function getCategoryList(int $categoryId)
|
||||||
{
|
{
|
||||||
$type = $categoryId === 1 ? 'hagigi' : 'kifari';
|
$type = $categoryId === 1 ? 'hagigi' : 'kifari';
|
||||||
@@ -157,111 +346,6 @@ class HomeController extends Controller
|
|||||||
return $data;
|
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) {
|
|
||||||
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,
|
|
||||||
];
|
|
||||||
});
|
|
||||||
|
|
||||||
$freeSubscription = $user->userSubscribers()
|
|
||||||
->whereHas('subscribe', function ($query) {
|
|
||||||
$query->where('is_free', true);
|
|
||||||
})
|
|
||||||
->where('expired_at', '>=', now())
|
|
||||||
->first();
|
|
||||||
|
|
||||||
$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,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
$latestSubscription = $user->userSubscribers()
|
|
||||||
->where('expired_at', '>=', now())
|
|
||||||
->latest('expired_at')
|
|
||||||
->first();
|
|
||||||
|
|
||||||
$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,
|
|
||||||
'expired_day' => $expiredDays,
|
|
||||||
'is_free' => (bool)$latestSubscription->is_free,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
$unread_notifications_count = Notification::unreadForUser($user->id)->count();
|
|
||||||
|
|
||||||
return $this->success([
|
|
||||||
'recent' => $recent,
|
|
||||||
'laws' => $lawsByCategory,
|
|
||||||
'last_law' => $laws,
|
|
||||||
'free' => $free_law,
|
|
||||||
'current_plan' => $current_plan,
|
|
||||||
'unread_notifications_count' => $unread_notifications_count,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper to automatically assign a free plan if the user has never had one.
|
|
||||||
*/
|
|
||||||
private function checkAndGrantFreeSubscription($user): void
|
private function checkAndGrantFreeSubscription($user): void
|
||||||
{
|
{
|
||||||
$hasUsedFreePlan = $user->userSubscribers()
|
$hasUsedFreePlan = $user->userSubscribers()
|
||||||
@@ -273,7 +357,6 @@ class HomeController extends Controller
|
|||||||
$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),
|
||||||
|
|||||||
@@ -33,6 +33,7 @@ Route::prefix('v1')->group(function () {
|
|||||||
Route::prefix('v1')->middleware('auth:sanctum')->group(function () {
|
Route::prefix('v1')->middleware('auth:sanctum')->group(function () {
|
||||||
Route::get('home', [HomeController::class, 'index']);
|
Route::get('home', [HomeController::class, 'index']);
|
||||||
Route::get('home-v2', [HomeController::class, 'homeV2']);
|
Route::get('home-v2', [HomeController::class, 'homeV2']);
|
||||||
|
Route::get('home-v3', [HomeController::class, 'homeV3']); // <-- NEW V3 ENDPOINT
|
||||||
Route::get('notifications', [NotificationController::class, 'index']);
|
Route::get('notifications', [NotificationController::class, 'index']);
|
||||||
Route::get('art', [ArtController::class, 'index']);
|
Route::get('art', [ArtController::class, 'index']);
|
||||||
Route::get('categories', [CategoriesController::class, 'index']);
|
Route::get('categories', [CategoriesController::class, 'index']);
|
||||||
|
|||||||
Reference in New Issue
Block a user