This commit is contained in:
spencerrlongg 2024-10-22 15:09:35 -05:00
parent e40849c910
commit b59bf495e1
6 changed files with 39 additions and 19 deletions

View file

@ -3,6 +3,7 @@
namespace App\Actions\CheckoutRequests;
use App\Exceptions\AssetNotRequestable;
use App\Exceptions\ThereIsNoUser;
use App\Models\Actionlog;
use App\Models\Asset;
use App\Models\Company;
@ -11,7 +12,6 @@ use App\Models\User;
use App\Notifications\RequestAssetCancelation;
use App\Notifications\RequestAssetNotification;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
class CreateCheckoutRequest
{
@ -21,7 +21,7 @@ class CreateCheckoutRequest
*/
public static function run(Asset $asset, User $user): string
{
// Check if asset is requestable
//throw new \Exception();
if (is_null(Asset::RequestableAssets()->find($asset->id))) {
throw new AssetNotRequestable($asset);
}
@ -46,6 +46,7 @@ class CreateCheckoutRequest
$logaction->target_type = User::class;
// If it's already requested, cancel the request.
// this is going into another action class
if ($asset->isRequestedBy(auth()->user())) {
$asset->cancelRequest();
$asset->decrement('requests_counter', 1);
@ -68,7 +69,12 @@ class CreateCheckoutRequest
\Log::warning($e);
}
return $asset;
return true; // or $asset, or whatever
}
public function doSomethingElse()
{
}

View file

@ -3,14 +3,27 @@
namespace App\Http\Controllers\Api;
use App\Actions\CheckoutRequests\CreateCheckoutRequest;
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;
class CheckoutRequest extends Controller
{
public function store($assetId): JsonResponse
public function store(CheckoutRequestRequest $request, Asset $asset): JsonResponse
{
CreateCheckoutRequest::run($assetId);
try {
CreateCheckoutRequest::run($asset, $request->validated()['user_id']);
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) {
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 :)'));
}
}
}

View file

@ -146,29 +146,19 @@ class ViewAssetsController extends Controller
* Process a specific requested asset
* @param null $assetId
*/
public function getRequestAsset(Asset $asset): RedirectResponse
public function store(Asset $asset): RedirectResponse
{
try {
CreateCheckoutRequest::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', 'poop');
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) {
report($e);
return redirect()->back()->with('error', 'generic error message');
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 :)');
}
//$status = CreateCheckoutRequest::run($asset, auth()->user());
//
//return match ($status) {
// 'doesNotExist' => redirect()->route('requestable-assets')->with('error', trans('admin/hardware/message.does_not_exist_or_not_requestable')),
// 'accessDenied' => redirect()->route('requestable-assets')->with('error', trans('general.insufficient_permissions')),
// 'cancelled' => redirect()->route('requestable-assets')->with('success')->with('success', trans('admin/hardware/message.requests.canceled')),
// default => redirect()->route('requestable-assets')->with('success')->with('success', trans('admin/hardware/message.requests.success')),
//};
}
//public function destroy(Asset $asset): RedirectResponse

View file

@ -40,6 +40,8 @@ Route::group(['prefix' => 'v1', 'middleware' => ['api', 'throttle:api']], functi
]
)->name('api.assets.requested');
Route::post('request/{asset}', [Api\CheckoutRequest::class, 'store'])->name('api.assets.requests.store');
Route::get('requestable/hardware',
[
Api\AssetsController::class,

View file

@ -305,7 +305,7 @@ Route::group(['prefix' => 'account', 'middleware' => ['auth']], function () {
)->name('requestable-assets');
Route::post(
'request-asset/{asset}',
[ViewAssetsController::class, 'getRequestAsset']
[ViewAssetsController::class, 'store']
)->name('account/request-asset');
Route::post(

View file

@ -21,6 +21,15 @@ class AssetCheckoutTest extends TestCase
Event::fake([CheckoutableCheckedOut::class]);
}
public function testCheckoutRequest()
{
$asset = Asset::factory()->create();
$this->actingAsForApi(User::factory()->create())
->post(route('api.assets.requests.store', $asset->id))
->assertOk();
}
public function testCheckingOutAssetRequiresCorrectPermission()
{
$this->actingAsForApi(User::factory()->create())