more work

This commit is contained in:
spencerrlongg 2024-10-16 18:48:15 -05:00
parent b1d62cc478
commit a524c0b418
3 changed files with 37 additions and 54 deletions

View file

@ -1,44 +0,0 @@
<?php
namespace App\Actions\CheckoutRequests;
/**
* @method static \Lorisleiva\Actions\Decorators\JobDecorator|\Lorisleiva\Actions\Decorators\UniqueJobDecorator makeJob(mixed $assetId)
* @method static \Lorisleiva\Actions\Decorators\UniqueJobDecorator makeUniqueJob(mixed $assetId)
* @method static \Illuminate\Foundation\Bus\PendingDispatch dispatch(mixed $assetId)
* @method static \Illuminate\Foundation\Bus\PendingDispatch|\Illuminate\Support\Fluent dispatchIf(bool $boolean, mixed $assetId)
* @method static \Illuminate\Foundation\Bus\PendingDispatch|\Illuminate\Support\Fluent dispatchUnless(bool $boolean, mixed $assetId)
* @method static dispatchSync(mixed $assetId)
* @method static dispatchNow(mixed $assetId)
* @method static dispatchAfterResponse(mixed $assetId)
* @method static mixed run(mixed $assetId)
*/
class CreateCheckoutRequest
{
}
namespace Lorisleiva\Actions\Concerns;
/**
* @method void asController()
*/
trait AsController
{
}
/**
* @method void asListener()
*/
trait AsListener
{
}
/**
* @method void asJob()
*/
trait AsJob
{
}
/**
* @method void asCommand(\Illuminate\Console\Command $command)
*/
trait AsCommand
{
}

View file

@ -2,6 +2,7 @@
namespace App\Actions\CheckoutRequests;
use App\Helpers\Helper;
use App\Models\Actionlog;
use App\Models\Asset;
use App\Models\Company;
@ -9,25 +10,28 @@ 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 Illuminate\Log;
class CreateCheckoutRequest
{
use AsAction;
public string $status;
public function handle($assetId)
{
$user = auth()->user();
// Check if the asset exists and is requestable
if (is_null($asset = Asset::RequestableAssets()->find($assetId))) {
return redirect()->route('requestable-assets')
->with('error', trans('admin/hardware/message.does_not_exist_or_not_requestable'));
$this->status = 'doesNotExist';
return false;
}
if (!Company::isCurrentUserHasAccess($asset)) {
return redirect()->route('requestable-assets')
->with('error', trans('general.insufficient_permissions'));
$this->status = 'accessDenied';
return false;
}
$data['item'] = $asset;
@ -57,8 +61,8 @@ class CreateCheckoutRequest
} catch (\Exception $e) {
Log::warning($e);
}
return redirect()->route('requestable-assets')
->with('success')->with('success', trans('admin/hardware/message.requests.canceled'));
$this->status = 'cancelled';
return true;
}
$logaction->logaction('requested');
@ -67,9 +71,28 @@ class CreateCheckoutRequest
try {
$settings->notify(new RequestAssetNotification($data));
} catch (\Exception $e) {
Log::warning($e);
\Log::warning($e);
}
return redirect()->route('requestable-assets')->with('success')->with('success', trans('admin/hardware/message.requests.success'));
return true;
}
public function jsonResponse(): JsonResponse
{
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
{
return match ($this->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')),
};
}
}

View file

@ -145,7 +145,11 @@ class ViewAssetsController extends Controller
*/
public function getRequestAsset($assetId = null): void
{
CreateCheckoutRequest::run($assetId);
$request = CreateCheckoutRequest::run($assetId);
if (!$request) {
\Log::debug('problem');
}
}
public function getRequestedAssets() : View