diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index 61e70adf86..268c499537 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -9,6 +9,7 @@ use App\Http\Requests\AssetCheckoutRequest; use App\Http\Transformers\AssetsTransformer; use App\Http\Transformers\LicensesTransformer; use App\Http\Transformers\SelectlistTransformer; +use App\Models\Actionlog; use App\Models\Asset; use App\Models\AssetModel; use App\Models\Company; @@ -640,6 +641,39 @@ class AssetsController extends Controller return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist')), 200); } + + + /** + * Restore a soft-deleted asset. + * + * @author [A. Gianotto] [] + * @param int $assetId + * @since [v5.1.18] + * @return JsonResponse + */ + public function restore($assetId = null) + { + // Get asset information + $asset = Asset::withTrashed()->find($assetId); + $this->authorize('delete', $asset); + if (isset($asset->id)) { + // Restore the asset + Asset::withTrashed()->where('id', $assetId)->restore(); + + $logaction = new Actionlog(); + $logaction->item_type = Asset::class; + $logaction->item_id = $asset->id; + $logaction->created_at = date("Y-m-d H:i:s"); + $logaction->user_id = Auth::user()->id; + $logaction->logaction('restored'); + + return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/hardware/message.restore.success'))); + + + } + return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist')), 200); + } + /** diff --git a/routes/api.php b/routes/api.php index 4fb252aaa4..51032feaac 100644 --- a/routes/api.php +++ b/routes/api.php @@ -445,6 +445,20 @@ Route::group(['prefix' => 'v1','namespace' => 'Api', 'middleware' => 'auth:api'] ] ); + Route::post('{asset_id}/restore', + [ + 'as' => 'api.assets.restore', + 'uses' => 'AssetsController@restore' + ] + ); + + Route::post('{asset_id}/destroy', + [ + 'as' => 'api.assets.destroy', + 'uses' => 'AssetsController@destroy' + ] + ); + }); /*--- Asset Maintenances API ---*/