Xboard/app/Http/Controllers/V1/User/PlanController.php

39 lines
1.1 KiB
PHP
Raw Normal View History

2023-11-17 01:44:01 -05:00
<?php
namespace App\Http\Controllers\V1\User;
2023-12-04 07:40:49 -05:00
use App\Exceptions\ApiException;
2023-11-17 01:44:01 -05:00
use App\Http\Controllers\Controller;
2025-01-06 12:20:11 -05:00
use App\Http\Resources\PlanResources;
2023-11-17 01:44:01 -05:00
use App\Models\Plan;
use App\Models\User;
use App\Services\PlanService;
use Illuminate\Http\Request;
class PlanController extends Controller
{
2025-01-06 12:20:11 -05:00
protected PlanService $planService;
public function __construct(PlanService $planService)
{
$this->planService = $planService;
}
2023-11-17 01:44:01 -05:00
public function fetch(Request $request)
{
2025-01-06 12:20:11 -05:00
$user = User::find($request->user()->id);
2023-11-17 01:44:01 -05:00
if ($request->input('id')) {
$plan = Plan::where('id', $request->input('id'))->first();
if (!$plan) {
return $this->fail([400, __('Subscription plan does not exist')]);
2023-11-17 01:44:01 -05:00
}
2025-01-06 12:20:11 -05:00
if (!$this->planService->isPlanAvailableForUser($plan, $user)) {
return $this->fail([400, __('Subscription plan does not exist')]);
2023-11-17 01:44:01 -05:00
}
2025-01-06 12:20:11 -05:00
return $this->success(PlanResources::make($plan));
2023-11-17 01:44:01 -05:00
}
2025-01-06 12:20:11 -05:00
$plans = $this->planService->getAvailablePlans();
return $this->success(PlanResources::collection($plans));
2023-11-17 01:44:01 -05:00
}
}