Skip to content

Commit 97ec4a6

Browse files
committed
Allow including child assets in user.assets table
1 parent a7d0cb7 commit 97ec4a6

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

app/Http/Controllers/Api/AssetsController.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ public function index(Request $request, $action = null, $upcoming_status = null)
143143
'supplier'
144144
); // it might be tempting to add 'assetlog' here, but don't. It blows up update-heavy users.
145145

146+
if ($request->input('include_child_assets') === 'true') {
147+
$assets->with(['assignedAssets.assignedTo']);
148+
}
146149

147150
if ($filter_non_deprecable_assets) {
148151
$non_deprecable_models = AssetModel::select('id')->whereNotNull('depreciation_id')->get();
@@ -432,6 +435,16 @@ public function index(Request $request, $action = null, $upcoming_status = null)
432435
$total = $assets->count();
433436
$assets = $assets->skip($offset)->take($limit)->get();
434437

438+
if ($request->input('include_child_assets') === 'true') {
439+
// Each asset might have a collection of assignedAssets on it.
440+
// We need to move those up to the top level so the transformer runs on them.
441+
foreach ($assets as $asset) {
442+
if ($asset->assignedAssets->count()) {
443+
$assets = $assets->merge($asset->assignedAssets);
444+
}
445+
}
446+
}
447+
435448

436449
/**
437450
* Include additional associated relationships

resources/views/users/view.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -782,7 +782,7 @@
782782
data-bulk-form-id="#assetsBulkForm"
783783
id="userAssetsListingTable"
784784
class="table table-striped snipe-table"
785-
data-url="{{ route('api.assets.index',['assigned_to' => e($user->id), 'assigned_type' => 'App\Models\User']) }}"
785+
data-url="{{ route('api.assets.index',['assigned_to' => e($user->id), 'assigned_type' => 'App\Models\User', 'include_child_assets' => 'true']) }}"
786786
data-export-options='{
787787
"fileName": "export-{{ str_slug($user->present()->fullName()) }}-assets-{{ date('Y-m-d') }}",
788788
"ignoreColumn": ["actions","image","change","checkbox","checkincheckout","icon"]

tests/Feature/Assets/Api/AssetIndexTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,4 +171,35 @@ public function testAssetApiIndexAdheresToCompanyScoping()
171171
->assertResponseDoesNotContainInRows($assetA, 'asset_tag')
172172
->assertResponseContainsInRows($assetB, 'asset_tag');
173173
}
174+
175+
public function testCanIncludeChildAssetsForGivenUser()
176+
{
177+
$user = User::factory()->create();
178+
179+
$parentAsset = Asset::factory()->assignedToUser($user)->create(['asset_tag' => 'parent-asset-tag']);
180+
Asset::factory()->assignedToAsset($parentAsset)->create(['asset_tag' => 'child-asset-tag']);
181+
182+
$this->actingAsForApi(User::factory()->superuser()->create())
183+
->getJson(
184+
route('api.assets.index', [
185+
'sort' => 'name',
186+
'order' => 'asc',
187+
'offset' => '0',
188+
'limit' => '20',
189+
'assigned_to' => $user->id,
190+
'assigned_type' => 'App\Models\User',
191+
'include_child_assets' => 'true',
192+
]))
193+
->assertOk()
194+
->assertJsonStructure([
195+
'total',
196+
'rows',
197+
])
198+
->assertJson(function (AssertableJson $json) {
199+
return $json->has('rows', 2)
200+
->where('rows.0.asset_tag', 'parent-asset-tag')
201+
->where('rows.1.asset_tag', 'child-asset-tag')
202+
->etc();
203+
});
204+
}
174205
}

tests/Feature/Notifications/Email/CurrentInventoryTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ public function test_current_inventory_includes_child_assets_when_enabled()
8080
{
8181
$this->settings->enableShowingAssignedAssets();
8282

83-
8483
$user = User::factory()->create();
8584

8685
$parentAsset = Asset::factory()->assignedToUser($user)->create(['asset_tag' => 'parent-asset-tag']);

0 commit comments

Comments
 (0)