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.
This commit is contained in:
spencerrlongg 2024-12-03 17:51:12 -06:00
parent 0103f20193
commit 513c78a09f
5 changed files with 28 additions and 22 deletions

View file

@ -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);

View file

@ -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

View file

@ -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')));
}
}
}

View file

@ -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'));
}
}

View file

@ -1,6 +1,5 @@
<?php
use App\Actions\CheckoutRequests\CreateCheckoutRequest;
use App\Http\Controllers\Account;
use App\Http\Controllers\ActionlogController;
use App\Http\Controllers\CategoriesController;