diff --git a/app/Console/Commands/FixupAssignedToWithoutAssignedType.php b/app/Console/Commands/FixupAssignedToWithoutAssignedType.php new file mode 100644 index 0000000000..ea3a55439a --- /dev/null +++ b/app/Console/Commands/FixupAssignedToWithoutAssignedType.php @@ -0,0 +1,66 @@ +whereNotNull("assigned_to")->withTrashed(); + $this->withProgressBar($assets->get(), function (Asset $asset) { + //now check each action log, from the most recent backwards, to find the last checkin or checkout + foreach($asset->log()->orderBy("id","desc")->get() as $action_log) { + if($this->option("debug")) { + $this->info("Asset id: " . $asset->id . " action log, action type is: " . $action_log->action_type); + } + switch($action_log->action_type) { + case 'checkin from': + if($this->option("debug")) { + $this->info("Doing a checkin for ".$asset->id); + } + $asset->assigned_to = null; + // if you have a required custom field, we still want to save, and we *don't* want an action_log + $asset->saveQuietly(); + return; + + case 'checkout': + if($this->option("debug")) { + $this->info("Doing a checkout for " . $asset->id . " picking target type: " . $action_log->target_type); + } + if($asset->assigned_to != $action_log->target_id) { + $this->error("Asset's assigned_to does *NOT* match Action Log's target_id. \$asset->assigned_to=".$asset->assigned_to." vs. \$action_log->target_id=".$action_log->target_id); + //FIXME - do we abort here? Do we try to keep looking? I don't know, this means your data is *really* messed up... + } + $asset->assigned_type = $action_log->target_type; + $asset->saveQuietly(); // see above + return; + } + } + $asset->assigned_to = null; //asset was never checked in or out in its lifetime - it stays 'checked in' + $asset->saveQuietly(); //see above + }); + $this->newLine(); + $this->info("Assets assigned_type are fixed"); + } +} diff --git a/tests/Feature/Console/FixupAssignedToAssignedTypeTest.php b/tests/Feature/Console/FixupAssignedToAssignedTypeTest.php new file mode 100644 index 0000000000..d4420145d2 --- /dev/null +++ b/tests/Feature/Console/FixupAssignedToAssignedTypeTest.php @@ -0,0 +1,57 @@ +markTestIncomplete(); + $asset = Asset::factory()->create(); + $user = User::factory()->create(); + $admin = User::factory()->admin()->create(); + + $asset->checkOut($user, $admin); + $asset->assigned_type=null; //blank out the assigned type + $asset->save(); + print "Okay we set everything up~!!!\n"; + + $output = $this->artisan('snipeit:assigned-to-fixup --debug')->assertExitCode(0); + print "artisan ran!\n"; + dump($output); + $asset = Asset::find($asset->id); + print "\n we refreshed the asset?"; + dump($asset); + $this->assertEquals(User::class, $asset->assigned_type); + } + + public function testInvalidAssignedTo() + { + $this->markTestIncomplete(); + $asset = Asset::factory()->create(); + $user = User::factory()->create(); + $admin = User::factory()->admin()->create(); + + $asset->checkOut($user, $admin); +// $asset->checkIn($user, $admin); //no such method btw + $asset->assigned_type=null; + $asset->assigned_to=null; + $asset->saveOrFail(); //*should* generate a 'checkin'? + + $asset->assigned_to=$user->id; //incorrectly mark asset as partially checked-out + $asset->saveOrFail(); + print "Okay we set everything up for test TWO~!!!\n"; + + $output = $this->artisan('snipeit:assigned-to-fixup --debug')->assertExitCode(0); + print "artisan ran TWO!\n"; + dump($output); + $asset = Asset::find($asset->id); + print "\n we refreshed the asset?"; + dump($asset); + $this->assertNull($asset->assigned_to); + } +}