fix free subscribe issue

This commit is contained in:
m.razaghi
2026-08-14 21:26:39 +03:30
parent b041736a22
commit 88813a4f86

View File

@@ -6,7 +6,6 @@ use App\Http\Controllers\Controller;
use App\Http\Requests\AuthRequest; use App\Http\Requests\AuthRequest;
use App\Models\User; use App\Models\User;
use App\Models\UserCode; use App\Models\UserCode;
use App\Models\SubscribePlan;
use App\Traits\BaseApiResponse; use App\Traits\BaseApiResponse;
use Carbon\Carbon; use Carbon\Carbon;
use Illuminate\Http\Request; use Illuminate\Http\Request;
@@ -92,29 +91,6 @@ class AuthController extends Controller
if ($latestCode->code === $request->code) { if ($latestCode->code === $request->code) {
$token = $user->createToken('mobile')->plainTextToken; $token = $user->createToken('mobile')->plainTextToken;
// --- AUTO 30-DAY SUBSCRIPTION LOGIC ---
// 1. Check if they have ever used a free plan before
$hasUsedFreePlan = $user->userSubscribers()
->whereHas('subscribe', function ($query) {
$query->where('is_free', true);
})->exists();
// 2. If not, find the active free plan and assign it
if (!$hasUsedFreePlan) {
$freePlan = SubscribePlan::where('is_free', true)->where('is_active', true)->first();
if ($freePlan) {
// We intentionally DO NOT pass 'is_free' => true here,
// matching SubscribePlanController to ensure the frontend hides the banner!
$user->userSubscribers()->create([
'subscribe_plan_id' => $freePlan->id,
'expired_at' => now()->addDays($freePlan->expired_day + 1),
]);
}
}
// ----------------------------------------------
return $this->success(['token' => $token], 'ورود موفقیت امیز بود'); return $this->success(['token' => $token], 'ورود موفقیت امیز بود');
} else { } else {
return $this->failed([], 'کد ورود اشتباه است', 404); return $this->failed([], 'کد ورود اشتباه است', 404);