Skip to content
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
13 changes: 13 additions & 0 deletions app/Http/Controllers/Api/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Http\Requests\StoreAssetRequest;
use App\Http\Requests\UpdateAssetRequest;
use App\Http\Traits\MigratesLegacyAssetLocations;
use App\Http\Transformers\ComponentsTransformer;
use App\Models\AccessoryCheckout;
use App\Models\CheckoutAcceptance;
use App\Models\LicenseSeat;
Expand Down Expand Up @@ -1322,6 +1323,18 @@ public function assignedAccessories(Request $request, Asset $asset) : JsonRespon
return (new AssetsTransformer)->transformCheckedoutAccessories($accessory_checkouts, $total);
}

public function assignedComponents(Request $request, Asset $asset): JsonResponse|array
{
$this->authorize('view', Asset::class);
$this->authorize('view', $asset);

$asset->loadCount('components');
$total = $asset->components_count;

$components = $asset->load(['components' => fn($query) => $query->applyOffsetAndLimit($total)])->components;

return (new ComponentsTransformer)->transformComponents($components, $total);
}

/**
* Generate asset labels by tag
Expand Down
7 changes: 7 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,13 @@
'assignedAccessories'
]
)->name('api.assets.assigned_accessories');

Route::get('{asset}/assigned/components',
[
Api\AssetsController::class,
'assignedComponents'
]
)->name('api.assets.assigned_components');
/** End assigned routes */

});
Expand Down
79 changes: 79 additions & 0 deletions tests/Feature/Assets/Api/AssignedComponentsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

namespace Tests\Feature\Assets\Api;

use App\Models\Asset;
use App\Models\Company;
use App\Models\Component;
use App\Models\User;
use Illuminate\Testing\Fluent\AssertableJson;
use Tests\TestCase;

class AssignedComponentsTest extends TestCase
{
public function test_requires_permission()
{
$this->actingAsForApi(User::factory()->create())
->getJson(route('api.assets.assigned_components', Asset::factory()->create()))
->assertForbidden();
}

public function test_adheres_to_company_scoping()
{
$this->settings->enableMultipleFullCompanySupport();

[$companyA, $companyB] = Company::factory()->count(2)->create();

$asset = Asset::factory()->for($companyA)->create();

$user = User::factory()->for($companyB)->viewAssets()->create();

$this->actingAsForApi($user)
->getJson(route('api.assets.assigned_components', $asset))
->assertOk()
->assertStatusMessageIs('error')
->assertMessagesAre('Asset not found');
}

public function test_can_get_components_assigned_to_specific_asset()
{
$unassociatedComponent = Component::factory()->create();

$asset = Asset::factory()->hasComponents(2)->create();

$componentsAssignedToAsset = $asset->components;

$this->actingAsForApi(User::factory()->viewAssets()->create())
->getJson(route('api.assets.assigned_components', $asset))
->assertOk()
->assertResponseContainsInRows($componentsAssignedToAsset)
->assertResponseDoesNotContainInRows($unassociatedComponent)
->assertJson(function (AssertableJson $json) {
$json->where('total', 2)
->count('rows', 2)
->etc();
});
}

public function test_adheres_to_offset_and_limit()
{
$asset = Asset::factory()->hasComponents(2)->create();

$componentsAssignedToAsset = $asset->components;

$this->actingAsForApi(User::factory()->viewAssets()->create())
->getJson(route('api.assets.assigned_components', [
'asset' => $asset,
'offset' => 1,
'limit' => 1,
]))
->assertOk()
->assertResponseDoesNotContainInRows($componentsAssignedToAsset->first())
->assertResponseContainsInRows($componentsAssignedToAsset->last())
->assertJson(function (AssertableJson $json) {
$json->where('total', 2)
->count('rows', 1)
->etc();
});
}
}
Loading