From 513c78a09f36817d8454304d03b7750928499d09 Mon Sep 17 00:00:00 2001 From: spencerrlongg Date: Tue, 3 Dec 2024 17:51:12 -0600 Subject: [PATCH] Refactor CheckoutRequest actions for consistency Renamed CheckoutRequest action classes to include "Action" in their names for consistency and clarity. Enhanced error handling in controllers to standardize error responses with translations. Updated usage of the renamed action classes throughout the code to ensure proper integration. --- ...st.php => CancelCheckoutRequestAction.php} | 8 +++++++- ...st.php => CreateCheckoutRequestAction.php} | 4 +--- app/Http/Controllers/Api/CheckoutRequest.php | 19 +++++++++++-------- app/Http/Controllers/ViewAssetsController.php | 18 +++++++++--------- routes/web.php | 1 - 5 files changed, 28 insertions(+), 22 deletions(-) rename app/Actions/CheckoutRequests/{CancelCheckoutRequest.php => CancelCheckoutRequestAction.php} (83%) rename app/Actions/CheckoutRequests/{CreateCheckoutRequest.php => CreateCheckoutRequestAction.php} (93%) diff --git a/app/Actions/CheckoutRequests/CancelCheckoutRequest.php b/app/Actions/CheckoutRequests/CancelCheckoutRequestAction.php similarity index 83% rename from app/Actions/CheckoutRequests/CancelCheckoutRequest.php rename to app/Actions/CheckoutRequests/CancelCheckoutRequestAction.php index ccad6b2e6..2d6dd68a0 100644 --- a/app/Actions/CheckoutRequests/CancelCheckoutRequest.php +++ b/app/Actions/CheckoutRequests/CancelCheckoutRequestAction.php @@ -4,14 +4,20 @@ namespace App\Actions\CheckoutRequests; use App\Models\Actionlog; use App\Models\Asset; +use App\Models\Company; use App\Models\Setting; use App\Models\User; use App\Notifications\RequestAssetCancelation; +use Illuminate\Auth\Access\AuthorizationException; -class CancelCheckoutRequest +class CancelCheckoutRequestAction { public static function run(Asset $asset, User $user) { + if (!Company::isCurrentUserHasAccess($asset)) { + throw new AuthorizationException(); + } + $asset->cancelRequest(); $asset->decrement('requests_counter', 1); diff --git a/app/Actions/CheckoutRequests/CreateCheckoutRequest.php b/app/Actions/CheckoutRequests/CreateCheckoutRequestAction.php similarity index 93% rename from app/Actions/CheckoutRequests/CreateCheckoutRequest.php rename to app/Actions/CheckoutRequests/CreateCheckoutRequestAction.php index 9d1ba332f..6870cfba2 100644 --- a/app/Actions/CheckoutRequests/CreateCheckoutRequest.php +++ b/app/Actions/CheckoutRequests/CreateCheckoutRequestAction.php @@ -3,18 +3,16 @@ namespace App\Actions\CheckoutRequests; use App\Exceptions\AssetNotRequestable; -use App\Exceptions\ThereIsNoUser; use App\Models\Actionlog; use App\Models\Asset; use App\Models\Company; use App\Models\Setting; use App\Models\User; -use App\Notifications\RequestAssetCancelation; use App\Notifications\RequestAssetNotification; use Illuminate\Auth\Access\AuthorizationException; use Log; -class CreateCheckoutRequest +class CreateCheckoutRequestAction { /** * @throws AssetNotRequestable diff --git a/app/Http/Controllers/Api/CheckoutRequest.php b/app/Http/Controllers/Api/CheckoutRequest.php index 1ee9c4c02..9b66531ae 100644 --- a/app/Http/Controllers/Api/CheckoutRequest.php +++ b/app/Http/Controllers/Api/CheckoutRequest.php @@ -2,40 +2,43 @@ namespace App\Http\Controllers\Api; -use App\Actions\CheckoutRequests\CancelCheckoutRequest; -use App\Actions\CheckoutRequests\CreateCheckoutRequest; +use App\Actions\CheckoutRequests\CancelCheckoutRequestAction; +use App\Actions\CheckoutRequests\CreateCheckoutRequestAction; use App\Exceptions\AssetNotRequestable; use App\Helpers\Helper; use App\Http\Controllers\Controller; use App\Models\Asset; use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Http\JsonResponse; +use Exception; class CheckoutRequest extends Controller { public function store(Asset $asset): JsonResponse { try { - CreateCheckoutRequest::run($asset, auth()->user()); + CreateCheckoutRequestAction::run($asset, auth()->user()); return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/hardware/message.requests.success'))); } catch (AssetNotRequestable $e) { return response()->json(Helper::formatStandardApiResponse('error', 'Asset is not requestable')); } catch (AuthorizationException $e) { return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.insufficient_permissions'))); - } catch (\Exception $e) { + } catch (Exception $e) { report($e); - return response()->json(Helper::formatStandardApiResponse('error', null, 'Something terrible has gone wrong and we\'re not sure if we can help - may god have mercy on your soul. Contact your admin :)')); + return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.something_went_wrong'))); } } public function destroy(Asset $asset): JsonResponse { try { - CancelCheckoutRequest::run($asset, auth()->user()); + CancelCheckoutRequestAction::run($asset, auth()->user()); return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/hardware/message.requests.canceled'))); - } catch (\Exception $e) { + } catch (AuthorizationException $e) { + return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.insufficient_permissions'))); + } catch (Exception $e) { report($e); - return response()->json(Helper::formatStandardApiResponse('error', null, $e->getMessage())); + return response()->json(Helper::formatStandardApiResponse('error', null, trans('general.something_went_wrong'))); } } } diff --git a/app/Http/Controllers/ViewAssetsController.php b/app/Http/Controllers/ViewAssetsController.php index 56d1f8b39..bbff6ba4f 100755 --- a/app/Http/Controllers/ViewAssetsController.php +++ b/app/Http/Controllers/ViewAssetsController.php @@ -2,8 +2,8 @@ namespace App\Http\Controllers; -use App\Actions\CheckoutRequests\CancelCheckoutRequest; -use App\Actions\CheckoutRequests\CreateCheckoutRequest; +use App\Actions\CheckoutRequests\CancelCheckoutRequestAction; +use App\Actions\CheckoutRequests\CreateCheckoutRequestAction; use App\Exceptions\AssetNotRequestable; use App\Models\Actionlog; use App\Models\Asset; @@ -16,7 +16,7 @@ use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Http\Request; use Illuminate\Http\RedirectResponse; use \Illuminate\Contracts\View\View; -use Illuminate\Support\Facades\Log; +use Exception; /** * This controller handles all actions related to the ability for users @@ -150,26 +150,26 @@ class ViewAssetsController extends Controller public function store(Asset $asset): RedirectResponse { try { - CreateCheckoutRequest::run($asset, auth()->user()); + CreateCheckoutRequestAction::run($asset, auth()->user()); return redirect()->route('requestable-assets')->with('success')->with('success', trans('admin/hardware/message.requests.success')); } catch (AssetNotRequestable $e) { return redirect()->back()->with('error', 'Asset is not requestable'); } catch (AuthorizationException $e) { return redirect()->back()->with('error', trans('admin/hardware/message.requests.error')); - } catch (\Exception $e) { + } catch (Exception $e) { report($e); - return redirect()->back()->with('error', 'Something terrible has gone wrong and we\'re not sure if we can help - may god have mercy on your soul. Contact your admin :)'); + return redirect()->back()->with('error', trans('general.something_went_wrong')); } } public function destroy(Asset $asset): RedirectResponse { try { - CancelCheckoutRequest::run($asset, auth()->user()); + CancelCheckoutRequestAction::run($asset, auth()->user()); return redirect()->route('requestable-assets')->with('success')->with('success', trans('admin/hardware/message.requests.canceled')); - } catch (\Exception $e) { + } catch (Exception $e) { report($e); - return redirect()->back()->with('error', 'something bad happened'); + return redirect()->back()->with('error', trans('general.something_went_wrong')); } } diff --git a/routes/web.php b/routes/web.php index 5482de9ca..473ac9a2b 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1,6 +1,5 @@