Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asset Store/Update/Destroy Actions #15893

Open
wants to merge 41 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 40 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
10d4c4b
just a little scaffolding
spencerrlongg Oct 23, 2024
6e8ab37
bs
spencerrlongg Oct 23, 2024
eaa1024
alright, this mostly works.
spencerrlongg Oct 29, 2024
34886ee
almost wrapped up StoreAssetAction.php
spencerrlongg Oct 30, 2024
698434d
no more base action, scaffolded update and delete
spencerrlongg Oct 30, 2024
c0ea1fb
delete done
spencerrlongg Oct 30, 2024
b00f8b5
a couple more tests passing
spencerrlongg Oct 31, 2024
a57fffe
a few fixes, tests pass
spencerrlongg Nov 4, 2024
916c0c0
good progress on update action
spencerrlongg Nov 12, 2024
47d1327
some more good progress on UpdateAssetAction.php
spencerrlongg Nov 13, 2024
c91dfaf
gui seemingly working, other good progress - api next
spencerrlongg Nov 14, 2024
7ab0b98
var changes
spencerrlongg Nov 18, 2024
dd8fb6e
fixed some things, made bulk progress, tests pass
spencerrlongg Nov 19, 2024
6c1239e
cleaned - more work needs to happen
spencerrlongg Nov 19, 2024
3e97f02
rm extra code
spencerrlongg Nov 19, 2024
9f81599
tests 'n stuff
spencerrlongg Nov 19, 2024
3cf583a
Add CustomFieldPermissionException handling
spencerrlongg Nov 19, 2024
0353c7a
Fix error handling in BulkAssetsController
spencerrlongg Nov 19, 2024
94e7310
Fix bulk asset update error handling and improve access checks
spencerrlongg Nov 20, 2024
1704a07
Allow null values for asset dates in update action
spencerrlongg Nov 20, 2024
03d9f93
Update asset model association and location logic handling
spencerrlongg Nov 20, 2024
617fe85
Fix typos and use correct array notation in asset updates
spencerrlongg Nov 20, 2024
6ddaa0b
Handle exceptions when saving assets in AssetImporter
spencerrlongg Nov 20, 2024
10126b8
Fix error handling in BulkAssetsController
spencerrlongg Nov 20, 2024
a3cdb3b
Fix error message format in BulkAssetsController
spencerrlongg Nov 21, 2024
a4fefc2
get rid of comment
spencerrlongg Nov 21, 2024
5aa2f30
Remove debug dump statements
spencerrlongg Nov 23, 2024
93e42ce
Remove unused code from UpdateAssetAction.php
spencerrlongg Nov 25, 2024
705a852
Refactor error handling and remove redundant code
spencerrlongg Nov 26, 2024
aeb5e90
Add CheckoutNotAllowed exception in UpdateAssetAction
spencerrlongg Nov 26, 2024
233355a
Refactor UpdateAssetAction for clarity and remove comments
spencerrlongg Nov 26, 2024
233656d
Add tests for storing assets with all fields and multiple entries
spencerrlongg Nov 26, 2024
09d4e0c
Remove unnecessary parameter and add delete asset tests
spencerrlongg Nov 26, 2024
e056da6
Add permissions checks to asset tests
spencerrlongg Nov 26, 2024
ac1d09a
Fix authorization check in AssetsController
spencerrlongg Nov 26, 2024
0a1aba5
a little work for conflicts
spencerrlongg Nov 29, 2024
16740b4
Simplify asset handling logic for creation and update
spencerrlongg Dec 3, 2024
bdf1033
Merge remote-tracking branch 'upstream/develop' into refactor/asset-a…
spencerrlongg Dec 3, 2024
5e475c3
Remove unused imports from AssetsController files.
spencerrlongg Dec 3, 2024
19771d2
formatting
spencerrlongg Dec 3, 2024
38d8d30
Refactor asset handling and streamline code
spencerrlongg Dec 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions app/Actions/Assets/DestroyAssetAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Actions\Assets;

use App\Events\CheckoutableCheckedIn;
use App\Models\Asset;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Storage;

class DestroyAssetAction
{
public static function run(Asset $asset)
{
if ($asset->assignedTo) {

$target = $asset->assignedTo;
$checkin_at = date('Y-m-d H:i:s');
$originalValues = $asset->getRawOriginal();
event(new CheckoutableCheckedIn($asset, $target, auth()->user(), 'Checkin on delete', $checkin_at, $originalValues));
DB::table('assets')
->where('id', $asset->id)
->update(['assigned_to' => null]);
}


if ($asset->image) {
try {
Storage::disk('public')->delete('assets'.'/'.$asset->image);
} catch (\Exception $e) {
spencerrlongg marked this conversation as resolved.
Show resolved Hide resolved
Log::debug($e);
}
}

$asset->delete();
}

}
143 changes: 143 additions & 0 deletions app/Actions/Assets/StoreAssetAction.php
spencerrlongg marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php

namespace App\Actions\Assets;

use App\Exceptions\CheckoutNotAllowed;
use App\Http\Requests\ImageUploadRequest;
use App\Models\Asset;
use App\Models\AssetModel;
use App\Models\Company;
use App\Models\Location;
use App\Models\Setting;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\MessageBag;
spencerrlongg marked this conversation as resolved.
Show resolved Hide resolved

class StoreAssetAction
{
/**
* @throws CheckoutNotAllowed
*/
public static function run(
$model_id,
$status_id,//
ImageUploadRequest $request, //temp for handleImages - i'd like to see that moved to a helper or something - or maybe just invoked at the extended request level so that it doesn't need to be done in the action?
$name = null,
$serial = null,
$company_id = null,
$asset_tag = null,
$order_number = null,
$notes = null,
$user_id = null,
spencerrlongg marked this conversation as resolved.
Show resolved Hide resolved
$warranty_months = null,
$purchase_cost = null,
$asset_eol_date = null,
$purchase_date = null,
$assigned_to = null,
$supplier_id = null,
$requestable = null,
$rtd_location_id = null,
$location_id = null, //do something with this
spencerrlongg marked this conversation as resolved.
Show resolved Hide resolved
$files = null,
spencerrlongg marked this conversation as resolved.
Show resolved Hide resolved
$byod = 0,
$assigned_user = null,
$assigned_asset = null,
$assigned_location = null,
$last_audit_date = null,
$next_audit_date = null,
): Asset|bool
{
$settings = Setting::getSettings();

// initial setting up of asset
$asset = new Asset();
$asset->model()->associate(AssetModel::find($model_id));
$asset->name = $name;
$asset->serial = $serial;
$asset->asset_tag = $asset_tag;
$asset->company_id = Company::getIdForCurrentUser($company_id);
$asset->model_id = $model_id;
$asset->order_number = $order_number;
$asset->notes = $notes;
$asset->created_by = auth()->id();
$asset->status_id = $status_id;
$asset->warranty_months = $warranty_months;
$asset->purchase_cost = $purchase_cost;
$asset->purchase_date = $purchase_date;
$asset->asset_eol_date = $asset_eol_date;
$asset->assigned_to = $assigned_to;
$asset->supplier_id = $supplier_id;
$asset->requestable = $requestable;
$asset->rtd_location_id = $rtd_location_id;
$asset->byod = $byod;
$asset->last_audit_date = $last_audit_date;
$asset->next_audit_date = $next_audit_date;
$asset->location_id = $location_id;

// set up next audit date
if (!empty($settings->audit_interval) && is_null($next_audit_date)) {
$asset->next_audit_date = Carbon::now()->addMonths($settings->audit_interval)->toDateString();
}

// Set location_id to rtd_location_id ONLY if the asset isn't being checked out
if (!$assigned_user && !$assigned_asset && !$assigned_location) {
$asset->location_id = $rtd_location_id;
}

//api only
if ($request->has('image_source')) {
$request->offsetSet('image', $request->offsetGet('image_source'));
}

if ($request->has('image')) {
$asset = $request->handleImages($asset);
}
spencerrlongg marked this conversation as resolved.
Show resolved Hide resolved

$model = AssetModel::find($model_id);

if (($model) && ($model instanceof AssetModel) && ($model->fieldset)) {
foreach ($model->fieldset->fields as $field) {
if ($field->field_encrypted == '1') {
if (Gate::allows('assets.view.encrypted_custom_fields')) {
if (is_array($request->input($field->db_column))) {
$asset->{$field->db_column} = Crypt::encrypt(implode(', ', $request->input($field->db_column)));
} else {
$asset->{$field->db_column} = Crypt::encrypt($request->input($field->db_column));
}
}
} else {
if (is_array($request->input($field->db_column))) {
$asset->{$field->db_column} = implode(', ', $request->input($field->db_column));
} else {
$asset->{$field->db_column} = $request->input($field->db_column);
}
}
}
}
spencerrlongg marked this conversation as resolved.
Show resolved Hide resolved

$asset->save();
if (request('assigned_user')) {
$target = User::find(request('assigned_user'));
// the api doesn't have these location-y bits - good reason?
$location = $target->location_id;
} elseif (request('assigned_asset')) {
$target = Asset::find(request('assigned_asset'));
$location = $target->location_id;
} elseif (request('assigned_location')) {
$target = Location::find(request('assigned_location'));
$location = $target->id;
}

if (isset($target)) {
$asset->checkOut($target, auth()->user(), date('Y-m-d H:i:s'), $request->input('expected_checkin', null), 'Checked out on asset creation', $request->get('name'), $location);
}
spencerrlongg marked this conversation as resolved.
Show resolved Hide resolved
//this was in api and not gui
if ($asset->image) {
$asset->image = $asset->getImageUrl();
}
return $asset;
}
}
Loading
Loading