Merge pull request #15162 from snipe/fixes/added_location_assets_endpoint

Added assets endpoint for locations
This commit is contained in:
snipe 2024-07-24 21:03:43 +01:00 committed by GitHub
commit 962ae5231d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 56 additions and 1 deletions

View file

@ -5,8 +5,10 @@ namespace App\Http\Controllers\Api;
use App\Helpers\Helper;
use App\Http\Requests\ImageUploadRequest;
use App\Http\Controllers\Controller;
use App\Http\Transformers\AssetsTransformer;
use App\Http\Transformers\LocationsTransformer;
use App\Http\Transformers\SelectlistTransformer;
use App\Models\Asset;
use App\Models\Location;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
@ -222,6 +224,15 @@ class LocationsController extends Controller
return response()->json(Helper::formatStandardApiResponse('error', null, $location->getErrors()));
}
public function assets(Request $request, Location $location) : JsonResponse | array
{
$this->authorize('view', Asset::class);
$this->authorize('view', $location);
$assets = Asset::where('assigned_to', '=', $location->id)->where('assigned_type', '=', Location::class)->with('model', 'model.category', 'assetstatus', 'location', 'company', 'defaultLoc');
$assets = $assets->get();
return (new AssetsTransformer)->transformAssets($assets, $assets->count(), $request);
}
/**
* Remove the specified resource from storage.
*

View file

@ -712,7 +712,7 @@ Route::group(['prefix' => 'v1', 'middleware' => ['api', 'throttle:api']], functi
Route::get('{location}/assets',
[
Api\LocationsController::class,
'getDataViewAssets'
'assets'
]
)->name('api.locations.viewassets');

View file

@ -0,0 +1,44 @@
<?php
namespace Tests\Feature\Consumables\Api;
use App\Models\Location;
use App\Models\Asset;
use App\Models\User;
use Tests\TestCase;
class LocationsViewTest extends TestCase
{
public function testViewingLocationRequiresPermission()
{
$location = Location::factory()->create();
$this->actingAsForApi(User::factory()->create())
->getJson(route('api.locations.show', $location->id))
->assertForbidden();
}
public function testViewingLocationAssetIndexRequiresPermission()
{
$location = Location::factory()->create();
$this->actingAsForApi(User::factory()->create())
->getJson(route('api.locations.viewassets', $location->id))
->assertForbidden();
}
public function testViewingLocationAssetIndex()
{
$location = Location::factory()->create();
Asset::factory()->count(3)->assignedToLocation($location)->create();
$this->actingAsForApi(User::factory()->superuser()->create())
->getJson(route('api.locations.viewassets', $location->id))
->assertOk()
->assertJsonStructure([
'total',
'rows',
])
->assertJson([
'total' => 3,
]);
}
}