feat(myket): add myket
This commit is contained in:
@@ -3,40 +3,26 @@
|
||||
namespace App\Http\Controllers\api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
// 1. IMPORT THE JOB WE CREATED
|
||||
use App\Jobs\CheckBazaarSubscription;
|
||||
|
||||
// These are your existing imports
|
||||
use App\Models\Law;
|
||||
use App\Models\Notification;
|
||||
use App\Models\RecentArt;
|
||||
use App\Models\UserSubscriber;
|
||||
use App\Services\AppMarketPurchaseVerifier;
|
||||
use App\Traits\BaseApiResponse;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
|
||||
class HomeController extends Controller
|
||||
{
|
||||
use BaseApiResponse;
|
||||
|
||||
public function __construct(private AppMarketPurchaseVerifier $marketVerifier)
|
||||
{
|
||||
}
|
||||
|
||||
public function index()
|
||||
{
|
||||
$user = auth()->user();
|
||||
|
||||
// 2. DISPATCH THE JOB AT THE VERY BEGINNING
|
||||
// ===================================================================
|
||||
// Find the user's latest subscription that has a purchase token
|
||||
$latestSubscription = $user->userSubscribers()->whereNotNull('purchase_token')->latest()->first();
|
||||
|
||||
// If such a subscription exists, create a new job and hand it to the queue
|
||||
// if ($latestSubscription) {
|
||||
// CheckBazaarSubscription::dispatch($latestSubscription);
|
||||
// }
|
||||
// ===================================================================
|
||||
// Your API now continues immediately without waiting for the check to finish.
|
||||
|
||||
|
||||
// --- ALL THE REST OF YOUR CODE REMAINS EXACTLY THE SAME ---
|
||||
$this->refreshMarketSubscription($user);
|
||||
|
||||
$recent = RecentArt::query()->where('user_id', $user->id)->get()->map(function ($q) {
|
||||
return [
|
||||
@@ -81,8 +67,6 @@ class HomeController extends Controller
|
||||
];
|
||||
});
|
||||
|
||||
$current_plan = null;
|
||||
|
||||
$freeSubscription = $user->userSubscribers()
|
||||
->whereHas('subscribe', function ($query) {
|
||||
$query->where('is_free', true);
|
||||
@@ -90,11 +74,9 @@ class HomeController extends Controller
|
||||
->where('expired_at', '>=', now())
|
||||
->first();
|
||||
|
||||
$expiredAt = null;
|
||||
$current_plan = null;
|
||||
|
||||
if ($freeSubscription) {
|
||||
$expiredAt = $freeSubscription->expired_at;
|
||||
$current_plan = [
|
||||
'id' => $freeSubscription->id,
|
||||
'name' => $freeSubscription->subscribe->name,
|
||||
@@ -104,18 +86,22 @@ class HomeController extends Controller
|
||||
];
|
||||
}
|
||||
|
||||
$latestSubscription = $user->userSubscribers()->latest()->first();
|
||||
$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());});
|
||||
|
||||
$purchase_token = $latestSubscription?->purchase_token;
|
||||
$current_plan = [
|
||||
'id' => $latestSubscription->id,
|
||||
'name' => $latestSubscription->subscribe->name ?? 'Subscription',
|
||||
'price' => $latestSubscription->subscribe->price ?? 100,
|
||||
'expired_day' => $expiredDays,
|
||||
'is_free' => false
|
||||
];
|
||||
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();
|
||||
|
||||
@@ -128,4 +114,32 @@ class HomeController extends Controller
|
||||
'unread_notifications_count' => $unread_notifications_count,
|
||||
]);
|
||||
}
|
||||
|
||||
private function refreshMarketSubscription($user): void
|
||||
{
|
||||
$bazaarSubscription = $user->userSubscribers()
|
||||
->whereNotNull('purchase_token')
|
||||
->whereNotNull('subscription_id')
|
||||
->where(function ($query) {
|
||||
$query->where('market_provider', 'bazaar')
|
||||
->orWhereNull('market_provider');
|
||||
})
|
||||
->latest()
|
||||
->first();
|
||||
|
||||
if ($bazaarSubscription && $this->marketVerifier->refreshBazaarSubscriber($bazaarSubscription)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$myketSubscription = $user->userSubscribers()
|
||||
->where('market_provider', 'myket')
|
||||
->whereNotNull('purchase_token')
|
||||
->whereNotNull('product_id')
|
||||
->latest()
|
||||
->first();
|
||||
|
||||
if ($myketSubscription) {
|
||||
$this->marketVerifier->refreshMyketSubscriber($myketSubscription);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user