Update asset model association and location logic handling

Refactor the asset update process to properly associate models and handle location ID changes based on request input. Introduce checks for model_id, company_id, and last_audit_date to ensure correct updates. Add commentary to clarify the handling of location ID logic and temporarily resolve issues with location assignment when requests do not expect JSON responses.
This commit is contained in:
spencerrlongg 2024-11-20 10:17:35 -06:00
parent 1704a07663
commit 03d9f936ab

View file

@ -6,6 +6,7 @@ use App\Events\CheckoutableCheckedIn;
use App\Exceptions\CustomFieldPermissionException; use App\Exceptions\CustomFieldPermissionException;
use App\Http\Requests\ImageUploadRequest; use App\Http\Requests\ImageUploadRequest;
use App\Models\Asset; use App\Models\Asset;
use App\Models\AssetModel;
use App\Models\Company; use App\Models\Company;
use App\Models\Location; use App\Models\Location;
use App\Models\Statuslabel; use App\Models\Statuslabel;
@ -101,7 +102,19 @@ class UpdateAssetAction
$asset->location_id = $location_id; $asset->location_id = $location_id;
!$isBulk ?? $asset->rtd_location_id = $rtd_location_id; $asset->rtd_location_id = $rtd_location_id ?? $asset->rtd_location_id;
if ($request->has('model_id')) {
$asset->model()->associate(AssetModel::find($request->validated()['model_id']));
}
if ($request->has('company_id')) {
$asset->company_id = Company::getIdForCurrentUser($request->validated()['company_id']);
}
if ($request->has('rtd_location_id') && !$request->has('location_id')) {
$asset->location_id = $request->validated()['rtd_location_id'];
}
if ($request->input('last_audit_date')) {
$asset->last_audit_date = Carbon::parse($request->input('last_audit_date'))->startOfDay()->format('Y-m-d H:i:s');
}
$asset->byod = $byod; $asset->byod = $byod;
$status = Statuslabel::find($status_id); $status = Statuslabel::find($status_id);
@ -117,7 +130,9 @@ class UpdateAssetAction
event(new CheckoutableCheckedIn($asset, $target, auth()->user(), 'Checkin on asset update', date('Y-m-d H:i:s'), $originalValues)); event(new CheckoutableCheckedIn($asset, $target, auth()->user(), 'Checkin on asset update', date('Y-m-d H:i:s'), $originalValues));
} }
if ($asset->assigned_to == '') { //this is causing an issue while setting location_id - this came from the gui but doesn't seem to work as expected in the api -
//throwing on !expectsJson for now until we can work out how to handle this better
if ($asset->assigned_to == '' && !$request->expectsJson()) {
$asset->location_id = $rtd_location_id; $asset->location_id = $rtd_location_id;
} }
@ -237,8 +252,17 @@ class UpdateAssetAction
private static function bulkUpdate($asset, $request): void private static function bulkUpdate($asset, $request): void
{ {
/**
* We're changing the location ID - figure out which location we should apply
* this change to:
*
* 0 - RTD location only
* 1 - location ID and RTD location ID
* 2 - location ID only
*
* Note: this is kinda dumb and we should just use human-readable values IMHO. - snipe
*/
if ($request->filled('rtd_location_id')) { if ($request->filled('rtd_location_id')) {
if (($request->filled('update_real_loc')) && (($request->input('update_real_loc')) == '0')) { if (($request->filled('update_real_loc')) && (($request->input('update_real_loc')) == '0')) {
$asset->rtd_location_id = $request->input('rtd_location_id'); $asset->rtd_location_id = $request->input('rtd_location_id');
} }
@ -251,7 +275,6 @@ class UpdateAssetAction
if (($request->filled('update_real_loc')) && (($request->input('update_real_loc')) == '2')) { if (($request->filled('update_real_loc')) && (($request->input('update_real_loc')) == '2')) {
$asset->location_id = $request->input('rtd_location_id'); $asset->location_id = $request->input('rtd_location_id');
} }
} }
} }