There were some weird constants I stripped out from the Asset model

This commit is contained in:
Brady Wetherington 2024-09-20 20:03:14 +01:00
parent 7d858129d9
commit 4f68185dbd
4 changed files with 24 additions and 48 deletions

View file

@ -77,10 +77,6 @@ class AssetCheckinController extends Controller
$this->authorize('checkin', $asset);
if ($asset->assignedType() == Asset::USER) {
$user = $asset->assignedTo;
}
$asset->expected_checkin = null;
$asset->last_checkin = now();
$asset->assignedTo()->disassociate($asset);

View file

@ -84,7 +84,7 @@ class AccessoryCheckout extends Model
*/
public function checkedOutToUser(): bool
{
return $this->assignedType() === Asset::USER;
return $this->assignedType() === Asset::USER; //ARGH - delete me
}
public function scopeUserAssigned(Builder $query): void

View file

@ -35,8 +35,13 @@ class Asset extends Depreciable
use CompanyableTrait;
use HasFactory, Loggable, Requestable, Presentable, SoftDeletes, ValidatingTrait, UniqueUndeletedTrait;
public const LOCATION = 'location';
public const ASSET = 'asset';
/*
* When trying to find the effective 'location' of an asset -
* When you have an asset, assigned to another asset, assigned to another...how many of those assets do we
* recursively iterate before we give up, when trying to find the 'real' location?
*/
const MAX_ASSET_ITERATIONS_FOR_LOCATION = 10;
public const USER = 'user';
use Acceptable;
@ -484,17 +489,7 @@ class Asset extends Depreciable
*/
public function checkedOutToUser(): bool
{
return $this->assignedType() === self::USER;
}
public function checkedOutToLocation(): bool
{
return $this->assignedType() === self::LOCATION;
}
public function checkedOutToAsset(): bool
{
return $this->assignedType() === self::ASSET;
return $this->assigned_type === User::class;
}
/**
@ -535,52 +530,37 @@ class Asset extends Depreciable
*/
public function assetLoc($iterations = 1, $first_asset = null)
{
if (! empty($this->assignedType())) {
if ($this->assignedType() == self::ASSET) {
if (! $first_asset) {
if (empty($this->assigned_type)) {
return $this->defaultLoc;
}
switch ($this->assigned_type) {
case Asset::class:
if (!$first_asset) {
$first_asset = $this;
}
if ($iterations > 10) {
throw new \Exception('Asset assignment Loop for Asset ID: '.$first_asset->id);
if ($iterations > self::MAX_ASSET_ITERATIONS_FOR_LOCATION) {
throw new \Exception('Asset assignment Loop for Asset ID: ' . $first_asset->id);
}
$assigned_to = self::find($this->assigned_to); //have to do this this way because otherwise it errors
if ($assigned_to) {
return $assigned_to->assetLoc($iterations + 1, $first_asset);
} // Recurse until we have a final location
}
if ($this->assignedType() == self::LOCATION) {
break;
case Location::class:
if ($this->assignedTo) {
return $this->assignedTo;
}
}
if ($this->assignedType() == self::USER) {
break;
case User::class:
if (($this->assignedTo) && $this->assignedTo->userLoc) {
return $this->assignedTo->userLoc;
}
//this makes no sense
return $this->defaultLoc;
}
}
return $this->defaultLoc;
return null; //otherwise return null
}
/**
* Gets the lowercased name of the type of target the asset is assigned to
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v4.0]
* @return string
*/
public function assignedType()
{
return $this->assigned_type ? strtolower(class_basename($this->assigned_type)) : null;
}
/**
* This is annoying, but because we don't say "assets" in our route names, we have to make an exception here
* @todo - normalize the route names - API endpoint URLS can stay the same

View file

@ -483,7 +483,7 @@ class StoreAssetTest extends TestCase
$asset = Asset::find($response['payload']['id']);
$this->assertTrue($asset->adminuser->is($user));
$this->assertTrue($asset->checkedOutToLocation());
$this->assertEquals($asset->assigned_type, Location::class);
$this->assertTrue($asset->location->is($location));
}
@ -509,7 +509,7 @@ class StoreAssetTest extends TestCase
$apiAsset = Asset::find($response['payload']['id']);
$this->assertTrue($apiAsset->adminuser->is($user));
$this->assertTrue($apiAsset->checkedOutToAsset());
$this->assertEquals($apiAsset->assigned_type, Asset::class);
// I think this makes sense, but open to a sanity check
$this->assertTrue($asset->assignedAssets()->find($response['payload']['id'])->is($apiAsset));
}