-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Add Form Request and Tests for Update Asset API Method
- Loading branch information
Showing
9 changed files
with
537 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
use App\Events\CheckoutableCheckedIn; | ||
use App\Http\Requests\StoreAssetRequest; | ||
use App\Http\Requests\UpdateAssetRequest; | ||
use App\Http\Traits\MigratesLegacyAssetLocations; | ||
use App\Models\CheckoutAcceptance; | ||
use App\Models\LicenseSeat; | ||
|
@@ -651,36 +652,35 @@ public function store(StoreAssetRequest $request): JsonResponse | |
* Accepts a POST request to update an asset | ||
* | ||
* @author [A. Gianotto] [<[email protected]>] | ||
* @param \App\Http\Requests\ImageUploadRequest $request | ||
* @since [v4.0] | ||
*/ | ||
public function update(ImageUploadRequest $request, $id) : JsonResponse | ||
public function update(UpdateAssetRequest $request, Asset $asset): JsonResponse | ||
{ | ||
$this->authorize('update', Asset::class); | ||
|
||
if ($asset = Asset::find($id)) { | ||
$asset->fill($request->all()); | ||
$asset->fill($request->validated()); | ||
|
||
($request->filled('model_id')) ? | ||
$asset->model()->associate(AssetModel::find($request->get('model_id'))) : null; | ||
($request->filled('rtd_location_id')) ? | ||
$asset->location_id = $request->get('rtd_location_id') : ''; | ||
($request->filled('company_id')) ? | ||
$asset->company_id = Company::getIdForCurrentUser($request->get('company_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'); | ||
} | ||
|
||
($request->filled('rtd_location_id')) ? | ||
$asset->location_id = $request->get('rtd_location_id') : null; | ||
/** | ||
* this is here just legacy reasons. Api\AssetController | ||
* used image_source once to allow encoded image uploads. | ||
*/ | ||
if ($request->has('image_source')) { | ||
$request->offsetSet('image', $request->offsetGet('image_source')); | ||
} | ||
|
||
/** | ||
* this is here just legacy reasons. Api\AssetController | ||
* used image_source once to allow encoded image uploads. | ||
*/ | ||
if ($request->has('image_source')) { | ||
$request->offsetSet('image', $request->offsetGet('image_source')); | ||
} | ||
|
||
$asset = $request->handleImages($asset); | ||
$model = AssetModel::find($asset->model_id); | ||
$asset = $request->handleImages($asset); | ||
$model = $asset->model; | ||
|
||
// Update custom fields | ||
$problems_updating_encrypted_custom_fields = false; | ||
|
@@ -706,15 +706,13 @@ public function update(ImageUploadRequest $request, $id) : JsonResponse | |
} | ||
} | ||
} | ||
|
||
|
||
if ($asset->save()) { | ||
if (($request->filled('assigned_user')) && ($target = User::find($request->get('assigned_user')))) { | ||
$location = $target->location_id; | ||
} elseif (($request->filled('assigned_asset')) && ($target = Asset::find($request->get('assigned_asset')))) { | ||
$location = $target->location_id; | ||
|
||
Asset::where('assigned_type', \App\Models\Asset::class)->where('assigned_to', $id) | ||
Asset::where('assigned_type', \App\Models\Asset::class)->where('assigned_to', $asset->id) | ||
->update(['location_id' => $target->location_id]); | ||
} elseif (($request->filled('assigned_location')) && ($target = Location::find($request->get('assigned_location')))) { | ||
$location = $target->id; | ||
|
@@ -728,17 +726,13 @@ public function update(ImageUploadRequest $request, $id) : JsonResponse | |
$asset->image = $asset->getImageUrl(); | ||
} | ||
|
||
if ($problems_updating_encrypted_custom_fields) { | ||
return response()->json(Helper::formatStandardApiResponse('success', $asset, trans('admin/hardware/message.update.encrypted_warning'))); | ||
} else { | ||
return response()->json(Helper::formatStandardApiResponse('success', $asset, trans('admin/hardware/message.update.success'))); | ||
} | ||
if ($problems_updating_encrypted_custom_fields) { | ||
return response()->json(Helper::formatStandardApiResponse('success', $asset, trans('admin/hardware/message.update.encrypted_warning'))); | ||
} else { | ||
return response()->json(Helper::formatStandardApiResponse('success', $asset, trans('admin/hardware/message.update.success'))); | ||
} | ||
|
||
return response()->json(Helper::formatStandardApiResponse('error', null, $asset->getErrors()), 200); | ||
} | ||
|
||
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist')), 200); | ||
return response()->json(Helper::formatStandardApiResponse('error', null, $asset->getErrors()), 200); | ||
} | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests; | ||
|
||
use App\Models\Asset; | ||
use Illuminate\Support\Facades\Gate; | ||
use Illuminate\Validation\Rule; | ||
|
||
class UpdateAssetRequest extends ImageUploadRequest | ||
{ | ||
/** | ||
* Determine if the user is authorized to make this request. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize() | ||
{ | ||
return Gate::allows('update', $this->asset); | ||
} | ||
|
||
/** | ||
* Get the validation rules that apply to the request. | ||
* | ||
* @return array | ||
*/ | ||
public function rules() | ||
{ | ||
$rules = array_merge( | ||
parent::rules(), | ||
(new Asset)->getRules(), | ||
// this is to overwrite rulesets that include required, and rewrite unique_undeleted | ||
[ | ||
'model_id' => ['integer', 'exists:models,id,deleted_at,NULL', 'not_array'], | ||
'status_id' => ['integer', 'exists:status_labels,id'], | ||
'asset_tag' => [ | ||
'min:1', 'max:255', 'not_array', | ||
Rule::unique('assets', 'asset_tag')->ignore($this->asset)->withoutTrashed() | ||
], | ||
], | ||
); | ||
|
||
return $rules; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.