A free open source IT asset/license management system
Fixed RB, added withTrashed()
snipe18ef770a8529
diff --git a/app/Models/Traits/Loggable.php b/app/Models/Traits/Loggable.phpindex e9334b192d..b506d308d1 100644--- a/app/Models/Traits/Loggable.php+++ b/app/Models/Traits/Loggable.php@@ -67,33 +67,35 @@ trait Loggable'action_date',];- $history = $this->with('item', 'user', 'adminuser', 'target', 'location', 'history');+ // Start with the polymorphic history relation so all subsequent filters,+ // search terms, and sorting are applied to the same query instance.+ $history = $this->history();if ($request->filled('search')) {- $history = $this->history()->TextSearch(e($request->input('search')));+ $history = $history->TextSearch(e($request->input('search')));}if ($request->filled('action_type')) {- $history = $this->history()->where('action_type', '=', $request->input('action_type'));+ $history = $history->where('action_type', '=', $request->input('action_type'));}if ($request->filled('created_by')) {- $history = $this->history()->where('created_by', '=', $request->input('created_by'));+ $history = $history->where('created_by', '=', $request->input('created_by'));}if ($request->filled('action_source')) {- $history = $this->history()->where('action_source', '=', $request->input('action_source'));+ $history = $history->where('action_source', '=', $request->input('action_source'));}if ($request->filled('remote_ip')) {- $history = $this->history()->where('remote_ip', '=', $request->input('remote_ip'));+ $history = $history->where('remote_ip', '=', $request->input('remote_ip'));}if ($request->filled('uploads')) {- $history = $this->history()->whereNotNull('filename');+ $history = $history->whereNotNull('filename');}- $total = $this->history()->count();+ $total = $history->count();// Make sure the offset and limit are actually integers and do not exceed system limits$offset = ($request->input('offset') > $total) ? $total : app('api_offset_value');$limit = app('api_limit_value');@@ -102,11 +104,11 @@ trait Loggableswitch ($request->input('sort')) {case 'created_by':- $this->history()->OrderByCreatedBy($order);+ $history = $history->OrderByCreatedBy($order);break;default:$sort = in_array($request->input('sort'), $allowed_columns) ? e($request->input('sort')) : 'action_logs.created_at';- $history = $this->history()->orderBy($sort, $order);+ $history = $history->orderBy($sort, $order);break;}diff --git a/routes/api.php b/routes/api.phpindex 58e2177b1a..95a1774b74 100644--- a/routes/api.php+++ b/routes/api.php@@ -87,7 +87,7 @@ Route::group(['prefix' => 'v1', 'middleware' => ['api', 'api-throttle:api']], fuApi\AccessoriesController::class,'history',]- )->name('api.accessories.history');+ )->name('api.accessories.history')->withTrashed();Route::get('{accessory}/checkedout',[@@ -227,7 +227,7 @@ Route::group(['prefix' => 'v1', 'middleware' => ['api', 'api-throttle:api']], fuApi\ComponentsController::class,'history',]- )->name('api.components.history');+ )->name('api.components.history')->withTrashed();Route::get('selectlist',[@@ -282,7 +282,7 @@ Route::group(['prefix' => 'v1', 'middleware' => ['api', 'api-throttle:api']], fuApi\ConsumablesController::class,'history',]- )->name('api.consumables.history');+ )->name('api.consumables.history')->withTrashed();Route::get('selectlist',[@@ -462,7 +462,7 @@ Route::group(['prefix' => 'v1', 'middleware' => ['api', 'api-throttle:api']], fuApi\AssetsController::class,'history',]- )->name('api.assets.history');+ )->name('api.assets.history')->withTrashed();Route::get('bytag/{tag}',[@@ -605,7 +605,7 @@ Route::group(['prefix' => 'v1', 'middleware' => ['api', 'api-throttle:api']], fuApi\MaintenancesController::class,'history',]- )->name('api.maintenances.history');+ )->name('api.maintenances.history')->withTrashed();Route::resource('maintenances',Api\MaintenancesController::class,@@ -677,7 +677,7 @@ Route::group(['prefix' => 'v1', 'middleware' => ['api', 'api-throttle:api']], fuApi\LicensesController::class,'history',]- )->name('api.licenses.history');+ )->name('api.licenses.history')->withTrashed();});@@ -756,7 +756,7 @@ Route::group(['prefix' => 'v1', 'middleware' => ['api', 'api-throttle:api']], fuApi\LocationsController::class,'history',]- )->name('api.locations.history');+ )->name('api.locations.history')->withTrashed();/** End assigned routes */});@@ -820,7 +820,7 @@ Route::group(['prefix' => 'v1', 'middleware' => ['api', 'api-throttle:api']], fuApi\AssetModelsController::class,'history',]- )->name('api.models.history');+ )->name('api.models.history')->withTrashed();Route::get('selectlist',[@@ -1064,7 +1064,7 @@ Route::group(['prefix' => 'v1', 'middleware' => ['api', 'api-throttle:api']], fuApi\UsersController::class,'history',]- )->name('api.users.history');+ )->name('api.users.history')->withTrashed();Route::get('selectlist',[diff --git a/tests/Feature/History/Api/HistoryTest.php b/tests/Feature/History/Api/HistoryTest.phpindex 3c55d31868..10089b0a69 100644--- a/tests/Feature/History/Api/HistoryTest.php+++ b/tests/Feature/History/Api/HistoryTest.php@@ -3,6 +3,7 @@namespace Tests\Feature\History\Api;use App\Models\Accessory;+use App\Models\Actionlog;use App\Models\Asset;use App\Models\Component;use App\Models\Consumable;@@ -189,4 +190,152 @@ class IndexHistoryTest extends TestCase->getJson(route('api.maintenances.history', Maintenance::factory()->create()))->assertOk();}++ /** Deleted Models */+ public function test_viewing_user_history_for_deleted_user_still_returns_logs()+ {+ $deletedUser = User::factory()->create();+ $actor = User::factory()->viewUserHistory()->create();+ $uniqueNote = 'history-for-deleted-user-'.uniqid();++ $log = Actionlog::factory()->create([+ 'item_id' => $deletedUser->id,+ 'item_type' => User::class,+ 'created_by' => $actor->id,+ 'action_type' => 'update',+ 'note' => $uniqueNote,+ 'created_at' => '2026-01-01 00:00:00',+ 'action_date' => '2026-01-01 00:00:00',+ ]);++ $deletedUser->delete();++ $this->actingAsForApi($actor)+ ->getJson(route('api.users.history', [+ 'user' => $deletedUser,+ 'search' => $uniqueNote,+ ]))+ ->assertOk()+ ->assertJsonPath('total', 1)+ ->assertJsonPath('rows.0.id', $log->id)+ ->assertJsonPath('rows.0.item.id', $deletedUser->id);+ }++ public function test_viewing_user_history_can_order_by_created_at()+ {+ $subject = User::factory()->create();+ $actor = User::factory()->viewUserHistory()->create();++ $older = Actionlog::factory()->create([+ 'item_id' => $subject->id,+ 'item_type' => User::class,+ 'created_by' => $actor->id,+ 'action_type' => 'update',+ 'created_at' => '2026-01-01 00:00:00',+ 'action_date' => '2026-01-01 00:00:00',+ ]);++ $newer = Actionlog::factory()->create([+ 'item_id' => $subject->id,+ 'item_type' => User::class,+ 'created_by' => $actor->id,+ 'action_type' => 'update',+ 'created_at' => '2026-01-02 00:00:00',+ 'action_date' => '2026-01-02 00:00:00',+ ]);++ $this->actingAsForApi($actor)+ ->getJson(route('api.users.history', [+ 'user' => $subject,+ 'sort' => 'created_at',+ 'order' => 'asc',+ ]))+ ->assertOk()+ ->assertJsonPath('rows.0.id', $older->id)+ ->assertJsonPath('rows.1.id', $newer->id);+ }++ public function test_viewing_user_history_can_order_by_action_date()+ {+ $subject = User::factory()->create();+ $actor = User::factory()->viewUserHistory()->create();++ $olderActionDate = Actionlog::factory()->create([+ 'item_id' => $subject->id,+ 'item_type' => User::class,+ 'created_by' => $actor->id,+ 'action_type' => 'update',+ 'created_at' => '2026-01-02 00:00:00',+ 'action_date' => '2026-01-01 00:00:00',+ ]);++ $newerActionDate = Actionlog::factory()->create([+ 'item_id' => $subject->id,+ 'item_type' => User::class,+ 'created_by' => $actor->id,+ 'action_type' => 'update',+ 'created_at' => '2026-01-01 00:00:00',+ 'action_date' => '2026-01-02 00:00:00',+ ]);++ $this->actingAsForApi($actor)+ ->getJson(route('api.users.history', [+ 'user' => $subject,+ 'sort' => 'action_date',+ 'order' => 'asc',+ ]))+ ->assertOk()+ ->assertJsonPath('rows.0.id', $olderActionDate->id)+ ->assertJsonPath('rows.1.id', $newerActionDate->id);+ }++ public function test_viewing_user_history_can_order_by_created_by()+ {+ $subject = User::factory()->create();+ $requestUser = User::factory()->viewUserHistory()->create();+ $uniqueNote = 'history-created-by-sort-'.uniqid();++ $alphaCreator = User::factory()->create([+ 'first_name' => 'Aaron',+ 'last_name' => 'Alpha',+ 'username' => 'aaron-alpha-'.uniqid(),+ ]);++ $omegaCreator = User::factory()->create([+ 'first_name' => 'Zelda',+ 'last_name' => 'Omega',+ 'username' => 'zelda-omega-'.uniqid(),+ ]);++ Actionlog::factory()->create([+ 'item_id' => $subject->id,+ 'item_type' => User::class,+ 'created_by' => $omegaCreator->id,+ 'action_type' => 'update',+ 'note' => $uniqueNote,+ 'created_at' => '2026-01-01 00:00:00',+ 'action_date' => '2026-01-01 00:00:00',+ ]);++ Actionlog::factory()->create([+ 'item_id' => $subject->id,+ 'item_type' => User::class,+ 'created_by' => $alphaCreator->id,+ 'action_type' => 'update',+ 'note' => $uniqueNote,+ 'created_at' => '2026-01-01 00:00:00',+ 'action_date' => '2026-01-01 00:00:00',+ ]);++ $this->actingAsForApi($requestUser)+ ->getJson(route('api.users.history', [+ 'user' => $subject,+ 'search' => $uniqueNote,+ 'sort' => 'created_by',+ 'order' => 'asc',+ ]))+ ->assertOk()+ ->assertJsonPath('rows.0.created_by.id', $alphaCreator->id)+ ->assertJsonPath('rows.1.created_by.id', $omegaCreator->id);+ }}