Skip to content

A free open source IT asset/license management system

stuff

spencerrlonggb59bf495e148
diff --git a/app/Actions/CheckoutRequests/CreateCheckoutRequest.php b/app/Actions/CheckoutRequests/CreateCheckoutRequest.php
index f0a36d46ab..c9fd4c9387 100644
--- a/app/Actions/CheckoutRequests/CreateCheckoutRequest.php
+++ b/app/Actions/CheckoutRequests/CreateCheckoutRequest.php
@@ -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()
+ {
+
}
diff --git a/app/Http/Controllers/Api/CheckoutRequest.php b/app/Http/Controllers/Api/CheckoutRequest.php
index f7a6023cd3..b1c976cf24 100644
--- a/app/Http/Controllers/Api/CheckoutRequest.php
+++ b/app/Http/Controllers/Api/CheckoutRequest.php
@@ -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 :)'));
+ }
}
}
diff --git a/app/Http/Controllers/ViewAssetsController.php b/app/Http/Controllers/ViewAssetsController.php
index 8c8b1e993f..605bbb8b99 100755
--- a/app/Http/Controllers/ViewAssetsController.php
+++ b/app/Http/Controllers/ViewAssetsController.php
@@ -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
diff --git a/routes/api.php b/routes/api.php
index 0581a04682..129e4d6ea1 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -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,
diff --git a/routes/web.php b/routes/web.php
index 137ed83b45..f719231c51 100644
--- a/routes/web.php
+++ b/routes/web.php
@@ -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(
diff --git a/tests/Feature/Checkouts/Api/AssetCheckoutTest.php b/tests/Feature/Checkouts/Api/AssetCheckoutTest.php
index ded3889642..0763478a98 100644
--- a/tests/Feature/Checkouts/Api/AssetCheckoutTest.php
+++ b/tests/Feature/Checkouts/Api/AssetCheckoutTest.php
@@ -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())