update controllers logic
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Api\Support;
|
||||
|
||||
use App\Http\Controllers\Api\BaseApiController;
|
||||
use App\Http\Controllers\Api\Core\BaseApiController;
|
||||
use App\Http\Requests\Support\ContactSendRequest;
|
||||
use App\Http\Resources\Support\ContactMessageResource;
|
||||
use App\Services\Support\ContactMessageService;
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace App\Http\Controllers\Api\Support;
|
||||
|
||||
use App\Http\Controllers\Api\BaseApiController;
|
||||
use App\Http\Controllers\Api\Core\BaseApiController;
|
||||
use App\Http\Requests\Support\SupportRequestIndexRequest;
|
||||
use App\Http\Requests\Support\SupportRequestStoreRequest;
|
||||
use App\Http\Resources\Support\SupportRequestCollection;
|
||||
@@ -22,9 +22,9 @@ class SupportController extends BaseApiController
|
||||
|
||||
public function index(SupportRequestIndexRequest $request): JsonResponse
|
||||
{
|
||||
$userId = (int) (auth()->id() ?? 0);
|
||||
if ($userId <= 0) {
|
||||
return $this->error('Unauthorized.', Response::HTTP_UNAUTHORIZED);
|
||||
$guard = $this->authenticatedUserIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
$this->authorize('viewAny', SupportRequest::class);
|
||||
@@ -39,7 +39,7 @@ class SupportController extends BaseApiController
|
||||
->intersect(['administrator', 'admin', 'principal', 'vice_principal', 'vice-principal'])
|
||||
->isNotEmpty();
|
||||
|
||||
$paginator = $this->supportService->list($userId, $filters, $page, $perPage, $isAdmin);
|
||||
$paginator = $this->supportService->list($guard, $filters, $page, $perPage, $isAdmin);
|
||||
$collection = new SupportRequestCollection($paginator);
|
||||
|
||||
return $this->success([
|
||||
@@ -50,8 +50,13 @@ class SupportController extends BaseApiController
|
||||
|
||||
public function store(SupportRequestStoreRequest $request): JsonResponse
|
||||
{
|
||||
$guard = $this->authenticatedUserIdOrUnauthorized();
|
||||
if ($guard instanceof JsonResponse) {
|
||||
return $guard;
|
||||
}
|
||||
|
||||
$user = auth()->user();
|
||||
if (!$user) {
|
||||
if (! $user) {
|
||||
return $this->error('Unauthorized.', Response::HTTP_UNAUTHORIZED);
|
||||
}
|
||||
|
||||
@@ -62,7 +67,7 @@ class SupportController extends BaseApiController
|
||||
$payload['user_name'] = trim(($user->firstname ?? '') . ' ' . ($user->lastname ?? ''));
|
||||
|
||||
try {
|
||||
$result = $this->supportService->create($user->id, $payload);
|
||||
$result = $this->supportService->create((int) $user->id, $payload);
|
||||
} catch (\Throwable $e) {
|
||||
Log::error('Support request failed: ' . $e->getMessage());
|
||||
return $this->error('Unable to submit support request.', Response::HTTP_INTERNAL_SERVER_ERROR);
|
||||
@@ -80,4 +85,17 @@ class SupportController extends BaseApiController
|
||||
'page' => 'teacher_support',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int|JsonResponse
|
||||
*/
|
||||
private function authenticatedUserIdOrUnauthorized(): int|JsonResponse
|
||||
{
|
||||
$userId = (int) (auth()->id() ?? 0);
|
||||
if ($userId <= 0) {
|
||||
return $this->error('Unauthorized.', Response::HTTP_UNAUTHORIZED);
|
||||
}
|
||||
|
||||
return $userId;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user