A free open source IT asset/license management system
Added assets endpoint for locations
snipec3a2cdeee968
diff --git a/app/Http/Controllers/Api/LocationsController.php b/app/Http/Controllers/Api/LocationsController.phpindex 2ceeb8374e..73227e0825 100644--- a/app/Http/Controllers/Api/LocationsController.php+++ b/app/Http/Controllers/Api/LocationsController.php@@ -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 Controllerreturn 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.*diff --git a/routes/api.php b/routes/api.phpindex b5311aa982..9fffd75669 100644--- a/routes/api.php+++ b/routes/api.php@@ -712,7 +712,7 @@ Route::group(['prefix' => 'v1', 'middleware' => ['api', 'throttle:api']], functiRoute::get('{location}/assets',[Api\LocationsController::class,- 'getDataViewAssets'+ 'assets'])->name('api.locations.viewassets');diff --git a/tests/Feature/Locations/Api/LocationsViewTest.php b/tests/Feature/Locations/Api/LocationsViewTest.phpnew file mode 100644index 0000000000..b5bcff8e4d--- /dev/null+++ b/tests/Feature/Locations/Api/LocationsViewTest.php@@ -0,0 +1,36 @@+<?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();+ 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,+ ]);+ }+}