Skip to content

Commit 90c242a

Browse files
authored
Merge pull request #17897 from marcusmoore/fixes/17896-prevent-bulk-checkout-of-checked-out-assets
Fixed #17896 - Prevent assigned assets from being bulk checked out
2 parents 5216dd7 + 52239a8 commit 90c242a

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

app/Http/Controllers/Assets/BulkAssetsController.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,15 @@ public function storeCheckout(AssetCheckoutRequest $request) : RedirectResponse
647647

648648
$assets = Asset::findOrFail($asset_ids);
649649

650+
// Prevent checking out assets that are already checked out
651+
if ($assets->pluck('assigned_to')->unique()->filter()->isNotEmpty()) {
652+
// re-add the asset ids so the assets select is re-populated
653+
$request->session()->flashInput(['selected_assets' => $asset_ids]);
654+
655+
return redirect(route('hardware.bulkcheckout.show'))
656+
->with('error', trans('general.error_assets_already_checked_out'));
657+
}
658+
650659
if (request('checkout_to_type') == 'asset') {
651660
foreach ($asset_ids as $asset_id) {
652661
if ($target->id == $asset_id) {

resources/lang/en-US/general.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@
520520
'item_name_var' => ':item Name',
521521
'error_user_company' => 'Checkout target company and asset company do not match',
522522
'error_user_company_accept_view' => 'An Asset assigned to you belongs to a different company so you can\'t accept nor deny it, please check with your manager',
523+
'error_assets_already_checked_out' => 'One or more of the assets are already checked out',
523524
'importer' => [
524525
'checked_out_to_fullname' => 'Checked Out to: Full Name',
525526
'checked_out_to_first_name' => 'Checked Out to: First Name',

tests/Feature/Checkouts/Ui/BulkAssetCheckoutTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
use App\Mail\CheckoutAssetMail;
66
use App\Models\Asset;
7+
use App\Models\Location;
78
use App\Models\User;
89
use Illuminate\Support\Facades\Mail;
10+
use PHPUnit\Framework\Attributes\DataProvider;
911
use PHPUnit\Framework\ExpectationFailedException;
1012
use Tests\TestCase;
1113

@@ -89,4 +91,63 @@ public function testHandleMissingModelBeingIncluded()
8991
$this->fail('Asset checkout email was sent when the entire checkout failed.');
9092
}
9193
}
94+
95+
public static function checkoutTargets()
96+
{
97+
yield 'Checkout to user' => [
98+
function () {
99+
return [
100+
'type' => 'user',
101+
'target' => User::factory()->forCompany()->create(),
102+
];
103+
}
104+
];
105+
106+
yield 'Checkout to asset' => [
107+
function () {
108+
return [
109+
'type' => 'asset',
110+
'target' => Asset::factory()->forCompany()->create(),
111+
];
112+
}
113+
];
114+
115+
yield 'Checkout to location' => [
116+
function () {
117+
return [
118+
'type' => 'location',
119+
'target' => Location::factory()->forCompany()->create(),
120+
];
121+
}
122+
];
123+
}
124+
125+
#[DataProvider('checkoutTargets')]
126+
public function test_prevents_checkouts_of_checked_out_items($data)
127+
{
128+
['type' => $type, 'target' => $target] = $data();
129+
130+
$asset = Asset::factory()->create();
131+
$checkedOutAsset = Asset::factory()->assignedToUser()->create();
132+
$existingUserId = $checkedOutAsset->assigned_to;
133+
134+
$response = $this->actingAs(User::factory()->superuser()->create())
135+
->post(route('hardware.bulkcheckout.store'), [
136+
'selected_assets' => [
137+
$asset->id,
138+
$checkedOutAsset->id,
139+
],
140+
'checkout_to_type' => $type,
141+
"assigned_$type" => $target->id,
142+
]);
143+
144+
$this->assertEquals(
145+
$existingUserId,
146+
$checkedOutAsset->fresh()->assigned_to,
147+
'Asset was checked out when it should have been prevented.'
148+
);
149+
150+
// ensure redirected back
151+
$response->assertRedirectToRoute('hardware.bulkcheckout.show');
152+
}
92153
}

0 commit comments

Comments
 (0)