A free open source IT asset/license management system
Status Labels: Added bulk delete on index
snipec97786684e15
diff --git a/app/Actions/StatusLabels/DestroyStatuslabelAction.php b/app/Actions/StatusLabels/DestroyStatuslabelAction.phpnew file mode 100644index 0000000000..ba544c0d71--- /dev/null+++ b/app/Actions/StatusLabels/DestroyStatuslabelAction.php@@ -0,0 +1,25 @@+<?php++namespace App\Actions\StatusLabels;++use App\Exceptions\ItemStillHasAssets;+use App\Models\Statuslabel;++class DestroyStatuslabelAction+{+ /**+ * @throws ItemStillHasAssets+ */+ public static function run(Statuslabel $statuslabel): bool+ {+ $statuslabel->loadCount(['assets as assets_count']);++ if ($statuslabel->assets_count > 0) {+ throw new ItemStillHasAssets($statuslabel);+ }++ $statuslabel->delete();++ return true;+ }+}diff --git a/app/Http/Controllers/Api/StatuslabelsController.php b/app/Http/Controllers/Api/StatuslabelsController.phpindex b6649e6b84..d8556dbeb8 100644--- a/app/Http/Controllers/Api/StatuslabelsController.php+++ b/app/Http/Controllers/Api/StatuslabelsController.php@@ -132,7 +132,10 @@ class StatuslabelsController extends Controllerpublic function show($id): array{$this->authorize('view', Statuslabel::class);- $statuslabel = Statuslabel::findOrFail($id);+ // Match index()'s eager load so isDeletable() (via available_actions.delete+ // and bulk_selectable.delete) reads the preloaded count instead of firing+ // a fallback assets()->count() query.+ $statuslabel = Statuslabel::withCount('assets as assets_count')->findOrFail($id);return (new StatuslabelsTransformer)->transformStatuslabel($statuslabel);}diff --git a/app/Http/Controllers/BulkStatuslabelsController.php b/app/Http/Controllers/BulkStatuslabelsController.phpnew file mode 100644index 0000000000..52652962fa--- /dev/null+++ b/app/Http/Controllers/BulkStatuslabelsController.php@@ -0,0 +1,50 @@+<?php++namespace App\Http\Controllers;++use App\Actions\StatusLabels\DestroyStatuslabelAction;+use App\Exceptions\ItemStillHasAssets;+use App\Models\Statuslabel;+use Illuminate\Http\Request;++class BulkStatuslabelsController extends Controller+{+ public function destroy(Request $request)+ {+ $this->authorize('delete', Statuslabel::class);++ $errors = [];+ $success_count = 0;++ foreach ($request->ids as $id) {+ $statuslabel = Statuslabel::find($id);+ if (is_null($statuslabel)) {+ $errors[] = trans('admin/statuslabels/message.does_not_exist');++ continue;+ }+ try {+ DestroyStatuslabelAction::run(statuslabel: $statuslabel);+ $success_count++;+ } catch (ItemStillHasAssets $e) {+ $errors[] = trans('general.bulk_delete_associations.assoc_assets_no_count', ['item_name' => $statuslabel->name, 'item' => trans('admin/statuslabels/table.status_label')]);+ } catch (\Exception $e) {+ report($e);+ $errors[] = trans('general.something_went_wrong');+ }+ }++ if (count($errors) > 0) {+ if ($success_count > 0) {+ return redirect()->route('statuslabels.index')+ ->with('success', trans_choice('admin/statuslabels/message.delete.partial_success', $success_count, ['count' => $success_count]))+ ->with('multi_error_messages', $errors);+ }++ return redirect()->route('statuslabels.index')->with('multi_error_messages', $errors);+ }++ return redirect()->route('statuslabels.index')+ ->with('success', trans('admin/statuslabels/message.delete.bulk_success'));+ }+}diff --git a/app/Http/Transformers/StatuslabelsTransformer.php b/app/Http/Transformers/StatuslabelsTransformer.phpindex f394518612..2805c49b79 100644--- a/app/Http/Transformers/StatuslabelsTransformer.php+++ b/app/Http/Transformers/StatuslabelsTransformer.php@@ -41,6 +41,9 @@ class StatuslabelsTransformer$permissions_array['available_actions'] = ['update' => Gate::allows('update', Statuslabel::class) ? true : false,'delete' => (Gate::allows('delete', Statuslabel::class) && ($statuslabel->isDeletable())) ? true : false,+ 'bulk_selectable' => [+ 'delete' => $statuslabel->isDeletable(),+ ],];$array += $permissions_array;diff --git a/app/Presenters/StatusLabelPresenter.php b/app/Presenters/StatusLabelPresenter.phpindex 141037c167..eb800b0e28 100644--- a/app/Presenters/StatusLabelPresenter.php+++ b/app/Presenters/StatusLabelPresenter.php@@ -16,6 +16,13 @@ class StatusLabelPresenter extends Presenter{$layout = [[+ 'field' => 'checkbox',+ 'checkbox' => true,+ 'formatter' => 'checkboxEnabledFormatter',+ 'titleTooltip' => trans('general.select_all_none'),+ 'printIgnore' => true,+ 'class' => 'hidden-print',+ ], ['field' => 'id','searchable' => false,'sortable' => true,diff --git a/resources/lang/en-US/admin/statuslabels/message.php b/resources/lang/en-US/admin/statuslabels/message.phpindex 2fbb424a7d..d566b662e0 100644--- a/resources/lang/en-US/admin/statuslabels/message.php+++ b/resources/lang/en-US/admin/statuslabels/message.php@@ -20,6 +20,8 @@ return ['confirm' => 'Are you sure you wish to delete this Status Label?','error' => 'There was an issue deleting the Status Label. Please try again.','success' => 'The Status Label was deleted successfully.',+ 'bulk_success' => 'Status Labels were deleted successfully.',+ 'partial_success' => 'Status Label deleted successfully. See additional information below. | :count status labels were deleted successfully. See additional information below.',],'help' => [diff --git a/resources/lang/en-US/admin/statuslabels/table.php b/resources/lang/en-US/admin/statuslabels/table.phpindex 46c9e66eba..2f01c1d8c4 100644--- a/resources/lang/en-US/admin/statuslabels/table.php+++ b/resources/lang/en-US/admin/statuslabels/table.php@@ -13,6 +13,7 @@ return ['pending' => 'Pending','status_type' => 'Status Type','show_in_nav' => 'Show in side nav',+ 'status_label' => 'Status Label','title' => 'Status Labels','undeployable' => 'Undeployable','update' => 'Update Status Label',diff --git a/resources/views/statuslabels/index.blade.php b/resources/views/statuslabels/index.blade.phpindex 2ab5cf6de8..ad98f1e7c0 100755--- a/resources/views/statuslabels/index.blade.php+++ b/resources/views/statuslabels/index.blade.php@@ -13,6 +13,18 @@<x-page-column class="col-md-9"><x-box>+ <x-slot:bulkactions>+ <x-table.bulk-actions+ name='statuslabel'+ action_route="{{ route('statuslabels.bulk.delete') }}"+ model_name="statuslabel"+ >+ @can('delete', App\Models\Statuslabel::class)+ <option>{{ trans('general.delete') }}</option>+ @endcan+ </x-table.bulk-actions>+ </x-slot:bulkactions>+<x-tablename="statuslabel"buttons="statuslabelButtons"diff --git a/routes/web.php b/routes/web.phpindex 9484ef1c67..06f271bde4 100644--- a/routes/web.php+++ b/routes/web.php@@ -9,6 +9,7 @@ use App\Http\Controllers\Auth\ResetPasswordController;use App\Http\Controllers\BulkCategoriesController;use App\Http\Controllers\BulkCompaniesController;use App\Http\Controllers\BulkManufacturersController;+use App\Http\Controllers\BulkStatuslabelsController;use App\Http\Controllers\BulkSuppliersController;use App\Http\Controllers\CategoriesController;use App\Http\Controllers\CompaniesController;@@ -108,6 +109,8 @@ Route::group(['middleware' => 'auth'], function () {*/Route::resource('statuslabels', StatuslabelsController::class);+ Route::post('statuslabels/bulk/delete', [BulkStatuslabelsController::class, 'destroy'])->name('statuslabels.bulk.delete');+/** Departments*/diff --git a/tests/Feature/StatusLabels/Api/BulkSelectableTest.php b/tests/Feature/StatusLabels/Api/BulkSelectableTest.phpnew file mode 100644index 0000000000..c1381ec51c--- /dev/null+++ b/tests/Feature/StatusLabels/Api/BulkSelectableTest.php@@ -0,0 +1,40 @@+<?php++namespace Tests\Feature\StatusLabels\Api;++use App\Models\Asset;+use App\Models\Statuslabel;+use App\Models\User;+use Tests\TestCase;++/**+ * Verifies the JS-visible flag that drives the bulk-delete checkbox on the+ * status-labels index. The bootstrap-table `checkboxEnabledFormatter` reads+ * `available_actions.bulk_selectable.delete` and disables the row's checkbox+ * when every entry there is false. A status label with any associated asset+ * must therefore report `bulk_selectable.delete === false`; a clean label+ * must report `true`.+ */+class BulkSelectableTest extends TestCase+{+ public function test_clean_status_label_is_bulk_selectable()+ {+ $statusLabel = Statuslabel::factory()->create();++ $this->actingAsForApi(User::factory()->superuser()->create())+ ->getJson(route('api.statuslabels.show', $statusLabel))+ ->assertOk()+ ->assertJsonPath('available_actions.bulk_selectable.delete', true);+ }++ public function test_status_label_with_assets_is_not_bulk_selectable()+ {+ $statusLabel = Statuslabel::factory()->create();+ Asset::factory()->create(['status_id' => $statusLabel->id]);++ $this->actingAsForApi(User::factory()->superuser()->create())+ ->getJson(route('api.statuslabels.show', $statusLabel))+ ->assertOk()+ ->assertJsonPath('available_actions.bulk_selectable.delete', false);+ }+}diff --git a/tests/Feature/StatusLabels/Ui/BulkDeleteStatusLabelsTest.php b/tests/Feature/StatusLabels/Ui/BulkDeleteStatusLabelsTest.phpnew file mode 100644index 0000000000..3e69e908f5--- /dev/null+++ b/tests/Feature/StatusLabels/Ui/BulkDeleteStatusLabelsTest.php@@ -0,0 +1,85 @@+<?php++namespace Tests\Feature\StatusLabels\Ui;++use App\Models\Asset;+use App\Models\Statuslabel;+use App\Models\User;+use Tests\TestCase;++class BulkDeleteStatusLabelsTest extends TestCase+{+ public function test_requires_permission()+ {+ $this->actingAs(User::factory()->create())+ ->post(route('statuslabels.bulk.delete'), [+ 'ids' => [1, 2, 3],+ ])+ ->assertForbidden();+ }++ public function test_status_label_with_assets_is_not_bulk_deleted()+ {+ $statusLabel = Statuslabel::factory()->create();+ Asset::factory()->create(['status_id' => $statusLabel->id]);++ $this->actingAs(User::factory()->deleteStatusLabels()->create())+ ->post(route('statuslabels.bulk.delete'), [+ 'ids' => [$statusLabel->id],+ ]);++ $this->assertModelExists($statusLabel);+ $this->assertNotSoftDeleted($statusLabel);+ }++ public function test_deletable_status_labels_are_bulk_deleted()+ {+ $label1 = Statuslabel::factory()->create();+ $label2 = Statuslabel::factory()->create();+ $label3 = Statuslabel::factory()->create();++ $this->actingAs(User::factory()->deleteStatusLabels()->create())+ ->post(route('statuslabels.bulk.delete'), [+ 'ids' => [$label1->id, $label2->id, $label3->id],+ ])+ ->assertRedirect(route('statuslabels.index'));++ $this->assertSoftDeleted($label1);+ $this->assertSoftDeleted($label2);+ $this->assertSoftDeleted($label3);+ }++ public function test_partial_success_deletes_the_clean_ones_and_reports_the_rest()+ {+ // One clean, one blocked by an asset. The clean one should be soft-deleted+ // and the blocked one should surface in the multi-error flash bag.+ $deletable = Statuslabel::factory()->create();+ $blocked = Statuslabel::factory()->create();+ Asset::factory()->create(['status_id' => $blocked->id]);++ $this->actingAs(User::factory()->deleteStatusLabels()->create())+ ->post(route('statuslabels.bulk.delete'), [+ 'ids' => [$deletable->id, $blocked->id],+ ])+ ->assertRedirect(route('statuslabels.index'))+ ->assertSessionHas('success')+ ->assertSessionHas('multi_error_messages');++ $this->assertSoftDeleted($deletable);+ $this->assertNotSoftDeleted($blocked);+ }++ public function test_nonexistent_ids_are_reported_and_do_not_break_the_batch()+ {+ $deletable = Statuslabel::factory()->create();++ $this->actingAs(User::factory()->deleteStatusLabels()->create())+ ->post(route('statuslabels.bulk.delete'), [+ 'ids' => [$deletable->id, 999999],+ ])+ ->assertRedirect(route('statuslabels.index'))+ ->assertSessionHas('multi_error_messages');++ $this->assertSoftDeleted($deletable);+ }+}