fix free subscribe issue

This commit is contained in:
m.razaghi
2026-08-14 20:22:53 +03:30
parent ec76bc9737
commit 52a13dcf6e

View File

@@ -6,6 +6,7 @@ use App\Http\Controllers\Controller;
use App\Http\Requests\AuthRequest;
use App\Models\User;
use App\Models\UserCode;
use App\Models\SubscribePlan;
use App\Traits\BaseApiResponse;
use Carbon\Carbon;
use Illuminate\Http\Request;
@@ -91,6 +92,29 @@ class AuthController extends Controller
if ($latestCode->code === $request->code) {
$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], 'ورود موفقیت امیز بود');
} else {
return $this->failed([], 'کد ورود اشتباه است', 404);