From 714fc630506aff802b326326dbc47bfabc3bfa05 Mon Sep 17 00:00:00 2001 From: Marcus Moore Date: Thu, 22 Feb 2024 13:14:30 -0800 Subject: [PATCH] Have legacy locations updated upon api asset checkin --- app/Http/Controllers/Api/AssetsController.php | 19 +++++++++++++++++++ tests/Feature/Api/Assets/AssetCheckinTest.php | 11 ++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index 2967471501..3d223248ab 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -886,6 +886,25 @@ 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); + } + $asset->location_id = $asset->rtd_location_id; if ($request->filled('location_id')) { diff --git a/tests/Feature/Api/Assets/AssetCheckinTest.php b/tests/Feature/Api/Assets/AssetCheckinTest.php index b7dda0d4b8..befac2e4ce 100644 --- a/tests/Feature/Api/Assets/AssetCheckinTest.php +++ b/tests/Feature/Api/Assets/AssetCheckinTest.php @@ -116,7 +116,16 @@ class AssetCheckinTest extends TestCase public function testLegacyLocationValuesSetToZeroAreUpdated() { - $this->markTestIncomplete('Not currently in controller'); + $asset = Asset::factory()->canBeInvalidUponCreation()->assignedToUser()->create([ + 'rtd_location_id' => 0, + 'location_id' => 0, + ]); + + $this->actingAsForApi(User::factory()->checkinAssets()->create()) + ->postJson(route('api.asset.checkin', $asset)); + + $this->assertNull($asset->refresh()->rtd_location_id); + $this->assertEquals($asset->location_id, $asset->rtd_location_id); } public function testPendingCheckoutAcceptancesAreClearedUponCheckin()