diff --git a/app/Actions/BaseAction.php b/app/Actions/BaseAction.php deleted file mode 100644 index bb48b336d..000000000 --- a/app/Actions/BaseAction.php +++ /dev/null @@ -1,19 +0,0 @@ -$method(...$args); - } - - //abstract static function run($parameters = null) - //{ - // return call_user_func($this->run(...$parameters)); - //} - -} diff --git a/app/Actions/CheckoutRequests/CreateCheckoutRequest.php b/app/Actions/CheckoutRequests/CreateCheckoutRequest.php index e0caafa27..f0a36d46a 100644 --- a/app/Actions/CheckoutRequests/CreateCheckoutRequest.php +++ b/app/Actions/CheckoutRequests/CreateCheckoutRequest.php @@ -2,7 +2,7 @@ namespace App\Actions\CheckoutRequests; -use App\Helpers\Helper; +use App\Exceptions\AssetNotRequestable; use App\Models\Actionlog; use App\Models\Asset; use App\Models\Company; @@ -10,34 +10,27 @@ use App\Models\Setting; use App\Models\User; use App\Notifications\RequestAssetCancelation; use App\Notifications\RequestAssetNotification; -use Illuminate\Http\JsonResponse; -use Illuminate\Http\RedirectResponse; -use Lorisleiva\Actions\Concerns\AsAction; -use Lorisleiva\Actions\Concerns\AsController; -use Lorisleiva\Actions\Concerns\WithAttributes; +use Illuminate\Auth\Access\AuthorizationException; +use Illuminate\Database\Eloquent\ModelNotFoundException; class CreateCheckoutRequest { - use AsAction; - use WithAttributes; - - public string $status = ''; - - public function handle($assetId) + /** + * @throws AssetNotRequestable + * @throws AuthorizationException + */ + public static function run(Asset $asset, User $user): string { - dump($assetId); - $user = auth()->user(); - - // Check if the asset exists and is requestable - if (is_null($asset = Asset::RequestableAssets()->find($assetId))) { - return $this->status = 'doesNotExist'; + // Check if asset is requestable + if (is_null(Asset::RequestableAssets()->find($asset->id))) { + throw new AssetNotRequestable($asset); } if (!Company::isCurrentUserHasAccess($asset)) { - return $this->status = 'accessDenied'; + throw new AuthorizationException(); } $data['item'] = $asset; - $data['target'] = auth()->user(); + $data['target'] = $user; $data['item_quantity'] = 1; $settings = Setting::getSettings(); @@ -63,7 +56,7 @@ class CreateCheckoutRequest } catch (\Exception $e) { \Log::warning($e); } - return $this->status = 'cancelled'; + return $status = 'cancelled'; } $logaction->logaction('requested'); @@ -74,37 +67,9 @@ class CreateCheckoutRequest } catch (\Exception $e) { \Log::warning($e); } - dump('handle end'); - return $this->status = 'success'; + return $asset; } - public function asController($assetId) - { - dump('asController'); - $asset = $this->handle($assetId); - //return $asset; - } - //public function jsonResponse(): JsonResponse - //{ - // dump('json'); - // return match ($this->status) { - // 'doesNotExist' => response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist_or_not_requestable'))), - // 'accessDenied' => response()->json(Helper::formatStandardApiResponse('error', null, trans('general.insufficient_permissions'))), - // default => response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.request_successfully_created'))), - // }; - //} - - //public function htmlResponse(): RedirectResponse - //{ - // dump('redirects'); - // return match ($this->status) { - // dump('redirects'), - // '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')), - // }; - //} -} +} \ No newline at end of file diff --git a/app/Actions/CheckoutRequests/CreateCheckoutRequestNew.php b/app/Actions/CheckoutRequests/CreateCheckoutRequestNew.php deleted file mode 100644 index 6b5a7565c..000000000 --- a/app/Actions/CheckoutRequests/CreateCheckoutRequestNew.php +++ /dev/null @@ -1,75 +0,0 @@ -user(); - - // Check if the asset exists and is requestable - if (is_null($asset = Asset::RequestableAssets()->find($assetId))) { - return $status = 'doesNotExist'; - } - if (!Company::isCurrentUserHasAccess($asset)) { - return $status = 'accessDenied'; - } - - $data['item'] = $asset; - $data['target'] = auth()->user(); - $data['item_quantity'] = 1; - $settings = Setting::getSettings(); - - $logaction = new Actionlog(); - $logaction->item_id = $data['asset_id'] = $asset->id; - $logaction->item_type = $data['item_type'] = Asset::class; - $logaction->created_at = $data['requested_date'] = date('Y-m-d H:i:s'); - - if ($user->location_id) { - $logaction->location_id = $user->location_id; - } - $logaction->target_id = $data['user_id'] = auth()->id(); - $logaction->target_type = User::class; - - // If it's already requested, cancel the request. - if ($asset->isRequestedBy(auth()->user())) { - $asset->cancelRequest(); - $asset->decrement('requests_counter', 1); - - $logaction->logaction('request canceled'); - try { - $settings->notify(new RequestAssetCancelation($data)); - } catch (\Exception $e) { - \Log::warning($e); - } - return $status = 'cancelled'; - } - - $logaction->logaction('requested'); - $asset->request(); - $asset->increment('requests_counter', 1); - try { - $settings->notify(new RequestAssetNotification($data)); - } catch (\Exception $e) { - \Log::warning($e); - } - dump('handle end'); - - return $status = 'success'; - } - - -} \ No newline at end of file diff --git a/app/Exceptions/AssetNotRequestable.php b/app/Exceptions/AssetNotRequestable.php new file mode 100644 index 000000000..8c3daa7cf --- /dev/null +++ b/app/Exceptions/AssetNotRequestable.php @@ -0,0 +1,27 @@ +errorMessage = $errorMessage; + + parent::__construct($errorMessage); + } + + public function __toString() + { + return is_null($this->errorMessage) ? 'This asset is not requestable.' : $this->errorMessage; + } + + public function getTranslatedMessage() + { + return trans($this->errorMessage); + } +} diff --git a/app/Exceptions/UserDoestExistException.php b/app/Exceptions/UserDoestExistException.php new file mode 100644 index 000000000..e14069dca --- /dev/null +++ b/app/Exceptions/UserDoestExistException.php @@ -0,0 +1,10 @@ + response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist_or_not_requestable'))), - 'accessDenied' => response()->json(Helper::formatStandardApiResponse('error', null, trans('general.insufficient_permissions'))), - default => response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.request_successfully_created'))), - }; + CreateCheckoutRequest::run($assetId); } } diff --git a/app/Http/Controllers/ViewAssetsController.php b/app/Http/Controllers/ViewAssetsController.php index 67c3ee1c8..8c8b1e993 100755 --- a/app/Http/Controllers/ViewAssetsController.php +++ b/app/Http/Controllers/ViewAssetsController.php @@ -3,7 +3,7 @@ namespace App\Http\Controllers; use App\Actions\CheckoutRequests\CreateCheckoutRequest; -use App\Actions\CheckoutRequests\CreateCheckoutRequestNew; +use App\Exceptions\AssetNotRequestable; use App\Models\Actionlog; use App\Models\Asset; use App\Models\AssetModel; @@ -11,9 +11,11 @@ use App\Models\Setting; use App\Models\User; use App\Notifications\RequestAssetCancelation; use App\Notifications\RequestAssetNotification; +use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Http\Request; use Illuminate\Http\RedirectResponse; use \Illuminate\Contracts\View\View; +use Illuminate\Support\Facades\Log; /** * This controller handles all actions related to the ability for users @@ -144,20 +146,39 @@ class ViewAssetsController extends Controller * Process a specific requested asset * @param null $assetId */ - public function getRequestAsset(CreateCheckoutRequestNew $checkoutRequestNew, $assetId = null): RedirectResponse + public function getRequestAsset(Asset $asset): RedirectResponse { - $status = CreateCheckoutRequestNew::run($assetId); - //$status = $checkoutRequestNew->run($assetId); + 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'); + } 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 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')), - 'success' => redirect()->route('requestable-assets')->with('success')->with('success', trans('admin/hardware/message.requests.success')), - default => redirect()->route('requestable-assets')->with('success')->with('success', trans('admin/hardware/message.requests.success')), - }; + + //$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 + //{ + // try { + // CancelCheckoutRequest($asset, auth()->user()); + // } + //} + + public function getRequestedAssets() : View { return view('account/requested'); diff --git a/app/Models/Asset.php b/app/Models/Asset.php index ce8b870eb..5bf49186f 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -1433,7 +1433,7 @@ class Asset extends Depreciable * @return \Illuminate\Database\Query\Builder Modified query builder */ - public function scopeRequestableAssets($query) + public function scopeRequestableAssets($query): Builder { $table = $query->getModel()->getTable(); diff --git a/composer.json b/composer.json index c96b11e1b..6d8931257 100644 --- a/composer.json +++ b/composer.json @@ -50,7 +50,6 @@ "league/csv": "^9.7", "league/flysystem-aws-s3-v3": "^3.0", "livewire/livewire": "^3.5", - "lorisleiva/laravel-actions": "^2.8", "neitanod/forceutf8": "^2.0", "nesbot/carbon": "^2.32", "nunomaduro/collision": "^7.0", @@ -85,8 +84,7 @@ "squizlabs/php_codesniffer": "^3.5", "symfony/css-selector": "^4.4", "symfony/dom-crawler": "^4.4", - "vimeo/psalm": "^5.13", - "wulfheart/laravel-actions-ide-helper": "^0.8.0" + "vimeo/psalm": "^5.13" }, "extra": { "laravel": { diff --git a/composer.lock b/composer.lock index 09504209f..3f79921b2 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "9bbdc0f3c60b21a77208b4c65c2f5bdb", + "content-hash": "3819ab4ef72eb77fabe494c0e746b83b", "packages": [ { "name": "alek13/slack", @@ -4535,154 +4535,6 @@ ], "time": "2024-10-01T12:40:06+00:00" }, - { - "name": "lorisleiva/laravel-actions", - "version": "v2.8.4", - "source": { - "type": "git", - "url": "https://github.com/lorisleiva/laravel-actions.git", - "reference": "5a168bfdd3b75dd6ff259019d4aeef784bbd5403" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/lorisleiva/laravel-actions/zipball/5a168bfdd3b75dd6ff259019d4aeef784bbd5403", - "reference": "5a168bfdd3b75dd6ff259019d4aeef784bbd5403", - "shasum": "" - }, - "require": { - "illuminate/contracts": "^10.0|^11.0", - "lorisleiva/lody": "^0.5", - "php": "^8.1" - }, - "require-dev": { - "orchestra/testbench": "^8.0|^9.0", - "pestphp/pest": "^1.23|^2.34", - "phpunit/phpunit": "^9.6|^10.0" - }, - "type": "library", - "extra": { - "laravel": { - "providers": [ - "Lorisleiva\\Actions\\ActionServiceProvider" - ], - "aliases": { - "Action": "Lorisleiva\\Actions\\Facades\\Actions" - } - } - }, - "autoload": { - "psr-4": { - "Lorisleiva\\Actions\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Loris Leiva", - "email": "loris.leiva@gmail.com", - "homepage": "https://lorisleiva.com", - "role": "Developer" - } - ], - "description": "Laravel components that take care of one specific task", - "homepage": "https://github.com/lorisleiva/laravel-actions", - "keywords": [ - "action", - "command", - "component", - "controller", - "job", - "laravel", - "listener", - "object" - ], - "support": { - "issues": "https://github.com/lorisleiva/laravel-actions/issues", - "source": "https://github.com/lorisleiva/laravel-actions/tree/v2.8.4" - }, - "funding": [ - { - "url": "https://github.com/sponsors/lorisleiva", - "type": "github" - } - ], - "time": "2024-09-10T09:57:29+00:00" - }, - { - "name": "lorisleiva/lody", - "version": "v0.5.0", - "source": { - "type": "git", - "url": "https://github.com/lorisleiva/lody.git", - "reference": "c2f51b070e99f3a240d66cf68ef1f232036917fe" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/lorisleiva/lody/zipball/c2f51b070e99f3a240d66cf68ef1f232036917fe", - "reference": "c2f51b070e99f3a240d66cf68ef1f232036917fe", - "shasum": "" - }, - "require": { - "illuminate/contracts": "^9.0|^10.0|^11.0", - "php": "^8.0" - }, - "require-dev": { - "orchestra/testbench": "^9.0", - "pestphp/pest": "^1.20|^2.34", - "phpunit/phpunit": "^9.5.10|^10.5" - }, - "type": "library", - "extra": { - "laravel": { - "providers": [ - "Lorisleiva\\Lody\\LodyServiceProvider" - ], - "aliases": { - "Lody": "Lorisleiva\\Lody\\Lody" - } - } - }, - "autoload": { - "psr-4": { - "Lorisleiva\\Lody\\": "src" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Loris Leiva", - "email": "loris.leiva@gmail.com", - "homepage": "https://lorisleiva.com", - "role": "Developer" - } - ], - "description": "Load files and classes as lazy collections in Laravel.", - "homepage": "https://github.com/lorisleiva/lody", - "keywords": [ - "classes", - "collection", - "files", - "laravel", - "load" - ], - "support": { - "issues": "https://github.com/lorisleiva/lody/issues", - "source": "https://github.com/lorisleiva/lody/tree/v0.5.0" - }, - "funding": [ - { - "url": "https://github.com/sponsors/lorisleiva", - "type": "github" - } - ], - "time": "2024-03-13T12:08:59+00:00" - }, { "name": "masterminds/html5", "version": "2.9.0", @@ -13923,73 +13775,6 @@ }, "time": "2024-03-27T12:14:49+00:00" }, - { - "name": "phpdocumentor/reflection", - "version": "6.0.0", - "source": { - "type": "git", - "url": "https://github.com/phpDocumentor/Reflection.git", - "reference": "61e2f1fe7683e9647b9ed8d9e53d08699385267d" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/phpDocumentor/Reflection/zipball/61e2f1fe7683e9647b9ed8d9e53d08699385267d", - "reference": "61e2f1fe7683e9647b9ed8d9e53d08699385267d", - "shasum": "" - }, - "require": { - "nikic/php-parser": "~4.18 || ^5.0", - "php": "8.1.*|8.2.*|8.3.*", - "phpdocumentor/reflection-common": "^2.1", - "phpdocumentor/reflection-docblock": "^5", - "phpdocumentor/type-resolver": "^1.2", - "symfony/polyfill-php80": "^1.28", - "webmozart/assert": "^1.7" - }, - "require-dev": { - "dealerdirect/phpcodesniffer-composer-installer": "^1.0", - "doctrine/coding-standard": "^12.0", - "mikey179/vfsstream": "~1.2", - "mockery/mockery": "~1.6.0", - "phpspec/prophecy-phpunit": "^2.0", - "phpstan/extension-installer": "^1.1", - "phpstan/phpstan": "^1.8", - "phpstan/phpstan-webmozart-assert": "^1.2", - "phpunit/phpunit": "^10.0", - "psalm/phar": "^5.24", - "rector/rector": "^1.0.0", - "squizlabs/php_codesniffer": "^3.8" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-5.x": "5.3.x-dev", - "dev-6.x": "6.0.x-dev" - } - }, - "autoload": { - "psr-4": { - "phpDocumentor\\": "src/phpDocumentor" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "description": "Reflection library to do Static Analysis for PHP Projects", - "homepage": "http://www.phpdoc.org", - "keywords": [ - "phpDocumentor", - "phpdoc", - "reflection", - "static analysis" - ], - "support": { - "issues": "https://github.com/phpDocumentor/Reflection/issues", - "source": "https://github.com/phpDocumentor/Reflection/tree/6.0.0" - }, - "time": "2024-05-23T19:28:12+00:00" - }, { "name": "phpmyadmin/sql-parser", "version": "5.10.0", @@ -15087,60 +14872,6 @@ ], "time": "2024-06-11T12:45:25+00:00" }, - { - "name": "riimu/kit-pathjoin", - "version": "v1.2.0", - "source": { - "type": "git", - "url": "https://github.com/Riimu/Kit-PathJoin.git", - "reference": "8ad2656c79527dba9f7f20e1229dcd38abfe8cee" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/Riimu/Kit-PathJoin/zipball/8ad2656c79527dba9f7f20e1229dcd38abfe8cee", - "reference": "8ad2656c79527dba9f7f20e1229dcd38abfe8cee", - "shasum": "" - }, - "require": { - "php": ">=5.4.0" - }, - "require-dev": { - "friendsofphp/php-cs-fixer": "^2.3", - "phpunit/phpunit": "^5.7 || ^6.2", - "squizlabs/php_codesniffer": "^3.0" - }, - "type": "library", - "autoload": { - "psr-4": { - "Riimu\\Kit\\PathJoin\\": "src/" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Riikka Kalliomäki", - "email": "riikka.kalliomaki@gmail.com", - "homepage": "http://riimu.net" - } - ], - "description": "Cross-platform library for normalizing and joining file system paths", - "homepage": "http://kit.riimu.net", - "keywords": [ - "file", - "join", - "normalize", - "path", - "system" - ], - "support": { - "issues": "https://github.com/Riimu/Kit-PathJoin/issues", - "source": "https://github.com/Riimu/Kit-PathJoin/tree/master" - }, - "time": "2017-07-09T14:41:04+00:00" - }, { "name": "sebastian/cli-parser", "version": "2.0.1", @@ -16909,81 +16640,6 @@ "source": "https://github.com/vimeo/psalm" }, "time": "2024-09-08T18:53:08+00:00" - }, - { - "name": "wulfheart/laravel-actions-ide-helper", - "version": "v0.8.0", - "source": { - "type": "git", - "url": "https://github.com/Wulfheart/laravel-actions-ide-helper.git", - "reference": "48e9d7c89804fcd459a73cc22adee533e7a88deb" - }, - "dist": { - "type": "zip", - "url": "https://api.github.com/repos/Wulfheart/laravel-actions-ide-helper/zipball/48e9d7c89804fcd459a73cc22adee533e7a88deb", - "reference": "48e9d7c89804fcd459a73cc22adee533e7a88deb", - "shasum": "" - }, - "require": { - "illuminate/contracts": "^10.0|^11.0", - "lorisleiva/laravel-actions": "^2.3", - "lorisleiva/lody": "^0.5.0", - "php": "^8.1|^8.2", - "phpdocumentor/reflection": "^5.1|^6.0", - "riimu/kit-pathjoin": "^1.2", - "spatie/laravel-package-tools": "^1.14" - }, - "require-dev": { - "brianium/paratest": "^6.8|^7.4", - "nunomaduro/collision": "^6.1|^8.0", - "orchestra/testbench": "^8.0|^9.0", - "pestphp/pest": "^1.22|^2.34", - "phpunit/phpunit": "^9.5.10|^10.5", - "spatie/invade": "^1.1|^2.0", - "spatie/laravel-ray": "^1.32", - "spatie/pest-plugin-snapshots": "^1.1|^2.1", - "vimeo/psalm": "^5.6" - }, - "type": "library", - "extra": { - "laravel": { - "providers": [ - "Wulfheart\\LaravelActionsIdeHelper\\LaravelActionsIdeHelperServiceProvider" - ], - "aliases": { - "LaravelActionsIdeHelper": "Wulfheart\\LaravelActionsIdeHelper\\LaravelActionsIdeHelperFacade" - } - } - }, - "autoload": { - "psr-4": { - "Wulfheart\\LaravelActionsIdeHelper\\": "src", - "Wulfheart\\LaravelActionsIdeHelper\\Database\\Factories\\": "database/factories" - } - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Alexander Wulf", - "email": "dev@alexfwulf.de", - "role": "Developer" - } - ], - "description": "Generate a new IDE Helper file for Laravel Actions.", - "homepage": "https://github.com/wulfheart/laravel-actions-ide-helper", - "keywords": [ - "laravel", - "laravel-actions-ide-helper", - "wulfheart" - ], - "support": { - "issues": "https://github.com/Wulfheart/laravel-actions-ide-helper/issues", - "source": "https://github.com/Wulfheart/laravel-actions-ide-helper/tree/v0.8.0" - }, - "time": "2024-08-30T14:32:22+00:00" } ], "aliases": [], diff --git a/routes/web.php b/routes/web.php index 76df83e11..137ed83b4 100644 --- a/routes/web.php +++ b/routes/web.php @@ -304,7 +304,7 @@ Route::group(['prefix' => 'account', 'middleware' => ['auth']], function () { [ViewAssetsController::class, 'getRequestableIndex'] )->name('requestable-assets'); Route::post( - 'request-asset/{assetId}', + 'request-asset/{asset}', [ViewAssetsController::class, 'getRequestAsset'] )->name('account/request-asset');