Skip to content

Commit

Permalink
Merge pull request #15492 from snipe/fixes/edit_to_archived_warning
Browse files Browse the repository at this point in the history
Fix - warn user on changing status to undeployable when editing
  • Loading branch information
snipe authored Sep 12, 2024
2 parents ab3b655 + a975303 commit 8774da3
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
14 changes: 11 additions & 3 deletions app/Http/Controllers/Assets/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers\Assets;

use App\Events\CheckoutableCheckedIn;
use App\Helpers\Helper;
use App\Http\Controllers\Controller;
use App\Http\Requests\ImageUploadRequest;
Expand Down Expand Up @@ -330,14 +331,21 @@ public function update(ImageUploadRequest $request, $assetId = null) : RedirectR
$asset->expected_checkin = $request->input('expected_checkin', null);

// If the box isn't checked, it's not in the request at all.
$asset->requestable = $request->filled('requestable');
$asset->requestable = $request->filled('requestable', 0);
$asset->rtd_location_id = $request->input('rtd_location_id', null);
$asset->byod = $request->input('byod', 0);

$status = Statuslabel::find($asset->status_id);
$status = Statuslabel::find($request->input('status_id'));

if ($status && $status->archived) {
// This is a non-deployable status label - we should check the asset back in.
if (($status && $status->getStatuslabelType() != 'deployable') && ($target = $asset->assignedTo)) {

$originalValues = $asset->getRawOriginal();
$asset->assigned_to = null;
$asset->assigned_type = null;
$asset->accepted = null;

event(new CheckoutableCheckedIn($asset, $target, auth()->user(), 'Checkin on asset update', date('Y-m-d H:i:s'), $originalValues));
}

if ($asset->assigned_to == '') {
Expand Down
1 change: 1 addition & 0 deletions resources/lang/en-US/admin/hardware/form.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
'asset_location_update_default' => 'Update only default location',
'asset_location_update_actual' => 'Update only actual location',
'asset_not_deployable' => 'That asset status is not deployable. This asset cannot be checked out.',
'asset_not_deployable_checkin' => 'That asset status is not deployable. Using this status label will checkin the asset.',
'asset_deployable' => 'That status is deployable. This asset can be checked out.',
'processing_spinner' => 'Processing... (This might take a bit of time on large files)',
'optional_infos' => 'Optional Information',
Expand Down
2 changes: 1 addition & 1 deletion resources/views/hardware/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ function user_add(status_id) {
$("#assignto_selector").hide();
$("#selected_status_status").removeClass('text-success');
$("#selected_status_status").addClass('text-danger');
$("#selected_status_status").html('<x-icon type="warning" /> {{ trans('admin/hardware/form.asset_not_deployable')}} ');
$("#selected_status_status").html('<x-icon type="warning" /> {{ (($item->assigned_to!='') && ($item->assigned_type!='') && ($item->deleted_at == '')) ? trans('admin/hardware/form.asset_not_deployable_checkin') : trans('admin/hardware/form.asset_not_deployable') }} ');
}
}
});
Expand Down
40 changes: 39 additions & 1 deletion tests/Feature/Assets/Ui/EditAssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,20 @@

namespace Tests\Feature\Assets\Ui;

use App\Events\CheckoutableCheckedIn;
use App\Models\Asset;
use App\Models\AssetModel;
use App\Models\Location;
use App\Models\StatusLabel;
use App\Models\User;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Event;
use Tests\TestCase;

class EditAssetTest extends TestCase
{

public function testPermissionRequiredToViewLicense()
public function testPermissionRequiredToViewAsset()
{
$asset = Asset::factory()->create();
$this->actingAs(User::factory()->create())
Expand Down Expand Up @@ -64,4 +68,38 @@ public function testAssetEditPostIsRedirectedIfRedirectSelectionIsItem()
$this->assertDatabaseHas('assets', ['asset_tag' => 'New Asset Tag']);
}

public function testNewCheckinIsLoggedIfStatusChangedToUndeployable()
{
Event::fake([CheckoutableCheckedIn::class]);

$user = User::factory()->create();
$deployable_status = Statuslabel::factory()->rtd()->create();
$achived_status = Statuslabel::factory()->archived()->create();
$asset = Asset::factory()->assignedToUser($user)->create(['status_id' => $deployable_status->id]);
$this->assertTrue($asset->assignedTo->is($user));

$currentTimestamp = now();

$this->actingAs(User::factory()->viewAssets()->editAssets()->create())
->from(route('hardware.edit', $asset->id))
->put(route('hardware.update', $asset->id), [
'status_id' => $achived_status->id,
'model_id' => $asset->model_id,
'asset_tags' => $asset->asset_tag,
],
)
->assertStatus(302);
//->assertRedirect(route('hardware.show', ['hardware' => $asset->id]));;

// $asset->refresh();
$asset = Asset::find($asset->id);
$this->assertNull($asset->assigned_to);
$this->assertNull($asset->assigned_type);
$this->assertEquals($achived_status->id, $asset->status_id);

Event::assertDispatched(function (CheckoutableCheckedIn $event) use ($currentTimestamp) {
return Carbon::parse($event->action_date)->diffInSeconds($currentTimestamp) < 2;
}, 1);
}

}

0 comments on commit 8774da3

Please sign in to comment.