fix free subscribe issue
This commit is contained in:
@@ -6,6 +6,7 @@ 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;
|
||||||
@@ -91,6 +92,29 @@ 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);
|
||||||
|
|||||||
Reference in New Issue
Block a user