Skip to content

Commit

Permalink
Merge pull request #16003 from snipe/asset_maintenance_api_fix
Browse files Browse the repository at this point in the history
Check for valid asset before accessing properties
  • Loading branch information
snipe authored Dec 26, 2024
2 parents 3edb501 + d410f16 commit 8fc1227
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 8 deletions.
8 changes: 4 additions & 4 deletions app/Http/Controllers/Api/AssetMaintenancesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public function index(Request $request) : JsonResponse | array
* @version v1.0
* @since [v1.8]
*/
public function store(Request $request) : JsonResponse
public function store(Request $request) : JsonResponse | array
{
$this->authorize('update', Asset::class);
// create a new model instance
Expand All @@ -149,7 +149,7 @@ public function store(Request $request) : JsonResponse
* @version v1.0
* @since [v4.0]
*/
public function update(Request $request, $id) : JsonResponse
public function update(Request $request, $id) : JsonResponse | array
{
$this->authorize('update', Asset::class);

Expand Down Expand Up @@ -186,7 +186,7 @@ public function update(Request $request, $id) : JsonResponse
* @version v1.0
* @since [v4.0]
*/
public function destroy($assetMaintenanceId) : JsonResponse
public function destroy($assetMaintenanceId) : JsonResponse | array
{
$this->authorize('update', Asset::class);
// Check if the asset maintenance exists
Expand All @@ -208,7 +208,7 @@ public function destroy($assetMaintenanceId) : JsonResponse
* @version v1.0
* @since [v4.0]
*/
public function show($assetMaintenanceId) : JsonResponse
public function show($assetMaintenanceId) : JsonResponse | array
{
$this->authorize('view', Asset::class);
$assetMaintenance = AssetMaintenance::findOrFail($assetMaintenanceId);
Expand Down
9 changes: 5 additions & 4 deletions app/Http/Transformers/AssetMaintenancesTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@ public function transformAssetMaintenance(AssetMaintenance $assetmaintenance)
'name'=> ($assetmaintenance->asset->name) ? e($assetmaintenance->asset->name) : null,
'asset_tag'=> e($assetmaintenance->asset->asset_tag),
'serial'=> e($assetmaintenance->asset->serial),
'deleted_at'=> e($assetmaintenance->asset->deleted_at),
'created_at'=> e($assetmaintenance->asset->created_at),
'deleted_at'=> Helper::getFormattedDateObject($assetmaintenance->asset->deleted_at, 'datetime'),
'created_at' => Helper::getFormattedDateObject($assetmaintenance->asset->created_at, 'datetime'),
'updated_at' => Helper::getFormattedDateObject($assetmaintenance->asset->updated_at, 'datetime'),
] : null,
'model' => (($assetmaintenance->asset) && ($assetmaintenance->asset->model)) ? [
'id' => (int) $assetmaintenance->asset->model->id,
'name'=> ($assetmaintenance->asset->model->name) ? e($assetmaintenance->asset->model->name).' '.e($assetmaintenance->asset->model->model_number) : null,
] : null,
'status_label' => ($assetmaintenance->asset->assetstatus) ? [
'status_label' => (($assetmaintenance->asset) && ($assetmaintenance->asset->assetstatus)) ? [
'id' => (int) $assetmaintenance->asset->assetstatus->id,
'name'=> e($assetmaintenance->asset->assetstatus->name),
'status_type'=> e($assetmaintenance->asset->assetstatus->getStatuslabelType()),
Expand Down Expand Up @@ -79,7 +80,7 @@ public function transformAssetMaintenance(AssetMaintenance $assetmaintenance)
];

$permissions_array['available_actions'] = [
'update' => (Gate::allows('update', Asset::class) && ($assetmaintenance->asset->deleted_at=='')) ? true : false,
'update' => (Gate::allows('update', Asset::class) && ((($assetmaintenance->asset) && $assetmaintenance->asset->deleted_at==''))) ? true : false,
'delete' => Gate::allows('delete', Asset::class),
];

Expand Down

0 comments on commit 8fc1227

Please sign in to comment.