Skip to content

A free open source IT asset/license management system

Added ability to support aliased count/sum fields in search

snipebdd44061f308
diff --git a/app/Models/AssetModel.php b/app/Models/AssetModel.php
index e002cbb5b9..63997cc5ef 100755
--- a/app/Models/AssetModel.php
+++ b/app/Models/AssetModel.php
@@ -85,10 +85,11 @@ class AssetModel extends SnipeModel
* @var array
*/
protected $searchableAttributes = [
- 'name',
+ 'eol',
+ 'min_amt',
'model_number',
+ 'name',
'notes',
- 'eol',
];
/**
@@ -104,6 +105,18 @@ class AssetModel extends SnipeModel
'adminuser' => ['first_name', 'last_name', 'display_name'],
];
+ /**
+ * Computed aliases (withCount/withSum) that can be searched via TextSearch filters.
+ *
+ * @var array
+ */
+ protected $searchableCounts = [
+ 'assets_count',
+ 'remaining',
+ 'assets_assigned_count',
+ 'assets_archived_count',
+ ];
+
protected static function booted(): void
{
static::forceDeleted(function (AssetModel $assetModel) {
diff --git a/app/Models/Traits/Searchable.php b/app/Models/Traits/Searchable.php
index 291fd97e92..e9948b4300 100644
--- a/app/Models/Traits/Searchable.php
+++ b/app/Models/Traits/Searchable.php
@@ -165,6 +165,7 @@ trait Searchable
private function applySearchFilters(Builder $query, array $filters): Builder
{
$searchableAttributes = $this->getSearchableAttributes();
+ $searchableCounts = $this->getSearchableCounts();
$searchableRelations = $this->getSearchableRelations();
$table = $this->getTable();
@@ -175,6 +176,12 @@ trait Searchable
continue;
}
+ if (in_array($filterKey, $searchableCounts, true)) {
+ $query = $this->applyCountAliasFilter($query, $filterKey, $filterValue);
+
+ continue;
+ }
+
if (! array_key_exists($filterKey, $searchableRelations)) {
continue;
}
@@ -214,6 +221,18 @@ trait Searchable
return $query;
}
+ /**
+ * Apply filtering on computed count aliases (for example withCount aliases).
+ */
+ private function applyCountAliasFilter(Builder $query, string $countAlias, string $filterValue): Builder
+ {
+ if (is_numeric($filterValue)) {
+ return $query->having($countAlias, '=', (int) $filterValue);
+ }
+
+ return $query->having($countAlias, 'LIKE', '%'.$filterValue.'%');
+ }
+
/**
* Searches the models attributes for the search terms
*
@@ -376,6 +395,14 @@ trait Searchable
return $this->searchableRelations ?? [];
}
+ /**
+ * Get searchable computed count aliases, if defined.
+ */
+ private function getSearchableCounts(): array
+ {
+ return $this->searchableCounts ?? [];
+ }
+
/**
* Get the table name of a relation.
*
diff --git a/app/Presenters/AssetModelPresenter.php b/app/Presenters/AssetModelPresenter.php
index d835859123..d21d37b762 100644
--- a/app/Presenters/AssetModelPresenter.php
+++ b/app/Presenters/AssetModelPresenter.php
@@ -72,7 +72,7 @@ class AssetModelPresenter extends Presenter
],
[
'field' => 'min_amt',
- 'searchable' => false,
+ 'searchable' => true,
'sortable' => true,
'switchable' => true,
'title' => trans('mail.min_QTY'),
@@ -83,7 +83,7 @@ class AssetModelPresenter extends Presenter
[
'field' => 'assets_count',
- 'searchable' => false,
+ 'searchable' => true,
'sortable' => true,
'switchable' => true,
'title' => trans('admin/models/table.numassets'),
@@ -93,7 +93,7 @@ class AssetModelPresenter extends Presenter
],
[
'field' => 'assets_assigned_count',
- 'searchable' => false,
+ 'searchable' => true,
'sortable' => true,
'switchable' => true,
'title' => trans('general.assigned'),
@@ -103,7 +103,7 @@ class AssetModelPresenter extends Presenter
],
[
'field' => 'remaining',
- 'searchable' => false,
+ 'searchable' => true,
'sortable' => true,
'switchable' => true,
'title' => trans('general.remaining'),
@@ -122,7 +122,7 @@ class AssetModelPresenter extends Presenter
],
[
'field' => 'assets_archived_count',
- 'searchable' => false,
+ 'searchable' => true,
'sortable' => true,
'switchable' => true,
'title' => trans('general.archived'),
diff --git a/tests/Feature/AssetModels/Api/IndexAssetModelsTest.php b/tests/Feature/AssetModels/Api/IndexAssetModelsTest.php
index c55ec57b39..9e09c73e9c 100644
--- a/tests/Feature/AssetModels/Api/IndexAssetModelsTest.php
+++ b/tests/Feature/AssetModels/Api/IndexAssetModelsTest.php
@@ -2,6 +2,7 @@
namespace Tests\Feature\AssetModels\Api;
+use App\Models\Asset;
use App\Models\AssetModel;
use App\Models\User;
use Illuminate\Testing\Fluent\AssertableJson;
@@ -62,4 +63,29 @@ class IndexAssetModelsTest extends TestCase
])
->assertJson(fn (AssertableJson $json) => $json->has('rows', 1)->etc());
}
+
+ public function test_asset_model_index_filter_can_search_computed_count_aliases()
+ {
+ $targetModel = AssetModel::factory()->create(['name' => 'Two Assets Model']);
+ $otherModel = AssetModel::factory()->create(['name' => 'One Asset Model']);
+
+ Asset::factory()->count(2)->create(['model_id' => $targetModel->id]);
+ Asset::factory()->create(['model_id' => $otherModel->id]);
+
+ $this->actingAsForApi(User::factory()->superuser()->create())
+ ->getJson(route('api.models.index', [
+ 'filter' => '{"assets_count":"2"}',
+ 'sort' => 'id',
+ 'order' => 'asc',
+ 'offset' => '0',
+ 'limit' => '20',
+ ]))
+ ->assertOk()
+ ->assertJsonStructure([
+ 'total',
+ 'rows',
+ ])
+ ->assertJson(fn (AssertableJson $json) => $json->has('rows', 1)->etc())
+ ->assertJsonFragment(['name' => 'Two Assets Model']);
+ }
}