mirror of
https://github.com/snipe/snipe-it.git
synced 2025-03-05 20:52:15 -08:00
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.
54 lines
1.6 KiB
PHP
54 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\CheckoutRequests;
|
|
|
|
use App\Exceptions\AssetNotRequestable;
|
|
use App\Models\Actionlog;
|
|
use App\Models\Asset;
|
|
use App\Models\Company;
|
|
use App\Models\Setting;
|
|
use App\Models\User;
|
|
use App\Notifications\RequestAssetNotification;
|
|
use Illuminate\Auth\Access\AuthorizationException;
|
|
use Log;
|
|
|
|
class CreateCheckoutRequestAction
|
|
{
|
|
/**
|
|
* @throws AssetNotRequestable
|
|
* @throws AuthorizationException
|
|
*/
|
|
public static function run(Asset $asset, User $user): string
|
|
{
|
|
if (is_null(Asset::RequestableAssets()->find($asset->id))) {
|
|
throw new AssetNotRequestable($asset);
|
|
}
|
|
if (!Company::isCurrentUserHasAccess($asset)) {
|
|
throw new AuthorizationException();
|
|
}
|
|
|
|
$data['item'] = $asset;
|
|
$data['target'] = $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');
|
|
$logaction->target_id = $data['user_id'] = auth()->id();
|
|
$logaction->target_type = User::class;
|
|
$logaction->location_id = $user->location_id ?? null;
|
|
$logaction->logaction('requested');
|
|
|
|
$asset->request();
|
|
$asset->increment('requests_counter', 1);
|
|
try {
|
|
$settings->notify(new RequestAssetNotification($data));
|
|
} catch (\Exception $e) {
|
|
Log::warning($e);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
} |