Skip to content

A free open source IT asset/license management system

Move location migration logic to trait

Marcus Mooredba837b1d225
diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php
index 3d223248ab..f64e68621d 100644
--- a/app/Http/Controllers/Api/AssetsController.php
+++ b/app/Http/Controllers/Api/AssetsController.php
@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Api;
use App\Events\CheckoutableCheckedIn;
use App\Http\Requests\StoreAssetRequest;
+use App\Http\Traits\MigratesLegacyLocations;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Gate;
@@ -45,6 +46,8 @@ use Route;
*/
class AssetsController extends Controller
{
+ use MigratesLegacyLocations;
+
/**
* Returns JSON listing of all assets
*
@@ -886,24 +889,7 @@ class AssetsController extends Controller
$asset->name = $request->input('name');
}
- // This is just meant to correct legacy issues where some user data would have 0
- // as a location ID, which isn't valid. Later versions of Snipe-IT have stricter validation
- // rules, so it's necessary to fix this for long-time users. It's kinda gross, but will help
- // people (and their data) in the long run
-
- if ($asset->rtd_location_id == '0') {
- \Log::debug('Manually override the RTD location IDs');
- \Log::debug('Original RTD Location ID: '.$asset->rtd_location_id);
- $asset->rtd_location_id = '';
- \Log::debug('New RTD Location ID: '.$asset->rtd_location_id);
- }
-
- if ($asset->location_id == '0') {
- \Log::debug('Manually override the location IDs');
- \Log::debug('Original Location ID: '.$asset->location_id);
- $asset->location_id = '';
- \Log::debug('New Location ID: '.$asset->location_id);
- }
+ $this->migrateLegacyLocations($asset);
$asset->location_id = $asset->rtd_location_id;
diff --git a/app/Http/Controllers/Assets/AssetCheckinController.php b/app/Http/Controllers/Assets/AssetCheckinController.php
index 891480f570..0a54a070f7 100644
--- a/app/Http/Controllers/Assets/AssetCheckinController.php
+++ b/app/Http/Controllers/Assets/AssetCheckinController.php
@@ -6,6 +6,7 @@ use App\Events\CheckoutableCheckedIn;
use App\Helpers\Helper;
use App\Http\Controllers\Controller;
use App\Http\Requests\AssetCheckinRequest;
+use App\Http\Traits\MigratesLegacyLocations;
use App\Models\Asset;
use App\Models\CheckoutAcceptance;
use Illuminate\Database\Eloquent\Builder;
@@ -15,6 +16,8 @@ use Illuminate\Support\Facades\View;
class AssetCheckinController extends Controller
{
+ use MigratesLegacyLocations;
+
/**
* Returns a view that presents a form to check an asset back into inventory.
*
@@ -77,24 +80,7 @@ class AssetCheckinController extends Controller
$asset->status_id = e($request->get('status_id'));
}
- // This is just meant to correct legacy issues where some user data would have 0
- // as a location ID, which isn't valid. Later versions of Snipe-IT have stricter validation
- // rules, so it's necessary to fix this for long-time users. It's kinda gross, but will help
- // people (and their data) in the long run
-
- if ($asset->rtd_location_id == '0') {
- \Log::debug('Manually override the RTD location IDs');
- \Log::debug('Original RTD Location ID: '.$asset->rtd_location_id);
- $asset->rtd_location_id = '';
- \Log::debug('New RTD Location ID: '.$asset->rtd_location_id);
- }
-
- if ($asset->location_id == '0') {
- \Log::debug('Manually override the location IDs');
- \Log::debug('Original Location ID: '.$asset->location_id);
- $asset->location_id = '';
- \Log::debug('New Location ID: '.$asset->location_id);
- }
+ $this->migrateLegacyLocations($asset);
$asset->location_id = $asset->rtd_location_id;
diff --git a/app/Http/Traits/MigratesLegacyLocations.php b/app/Http/Traits/MigratesLegacyLocations.php
new file mode 100644
index 0000000000..68d170f080
--- /dev/null
+++ b/app/Http/Traits/MigratesLegacyLocations.php
@@ -0,0 +1,33 @@
+<?php
+
+namespace App\Http\Traits;
+
+use App\Models\Asset;
+
+trait MigratesLegacyLocations
+{
+ /**
+ * This is just meant to correct legacy issues where some user data would have 0
+ * as a location ID, which isn't valid. Later versions of Snipe-IT have stricter validation
+ * rules, so it's necessary to fix this for long-time users. It's kinda gross, but will help
+ * people (and their data) in the long run
+ * @param Asset $asset
+ * @return void
+ */
+ private function migrateLegacyLocations(Asset $asset): void
+ {
+ if ($asset->rtd_location_id == '0') {
+ \Log::debug('Manually override the RTD location IDs');
+ \Log::debug('Original RTD Location ID: ' . $asset->rtd_location_id);
+ $asset->rtd_location_id = '';
+ \Log::debug('New RTD Location ID: ' . $asset->rtd_location_id);
+ }
+
+ if ($asset->location_id == '0') {
+ \Log::debug('Manually override the location IDs');
+ \Log::debug('Original Location ID: ' . $asset->location_id);
+ $asset->location_id = '';
+ \Log::debug('New Location ID: ' . $asset->location_id);
+ }
+ }
+}