Merge remote-tracking branch 'origin/develop'

This commit is contained in:
snipe 2024-08-12 16:38:23 +01:00
commit 4dc57f95e2
2 changed files with 123 additions and 0 deletions

View file

@ -0,0 +1,66 @@
<?php
namespace App\Console\Commands;
use App\Models\Asset;
use Illuminate\Console\Command;
class FixupAssignedToWithoutAssignedType extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'snipeit:assigned-to-fixup
{--debug : Display debugging output}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Fixes up assets that have an assigned_to but no assigned_type';
/**
* Execute the console command.
*/
public function handle()
{
$assets = Asset::whereNull("assigned_type")->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");
}
}

View file

@ -0,0 +1,57 @@
<?php
namespace Tests\Feature;
use App\Models\Asset;
use App\Models\User;
use Tests\TestCase;
class FixupAssignedToAssignedTypeTest extends TestCase
{
public function testEmptyAssignedType()
{
$this->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);
}
}