Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Checkout-to fixer #15266

Merged
merged 3 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions app/Console/Commands/FixupAssignedToWithoutAssignedType.php
Original file line number Diff line number Diff line change
@@ -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");
}
}
57 changes: 57 additions & 0 deletions tests/Feature/Console/FixupAssignedToAssignedTypeTest.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
Loading