feat(hse): tampilkan tombol delete jenis SIMPER dan aktifkan integrasi soft delete ke tong sampah

This commit is contained in:
Ray1111-aa
2026-10-28 09:59:08 +07:00
parent 539cdbce86
commit 5536480f52
4 changed files with 134 additions and 15 deletions
@@ -8,6 +8,7 @@ use App\Models\Vehicle;
use Carbon\Carbon;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\Rule;
use Illuminate\View\View;
@@ -145,19 +146,18 @@ class SimperTypeController extends Controller
}
/**
* Hapus jenis SIMPER.
* Hapus jenis SIMPER ke Tong Sampah.
*/
public function destroy(int $id): RedirectResponse
{
$simperType = SimperType::withCount('employeeSimpers')->findOrFail($id);
if ($simperType->employee_simpers_count > 0) {
return back()->with('error', "Jenis SIMPER '{$simperType->name}' tidak dapat dihapus karena masih digunakan oleh {$simperType->employee_simpers_count} personil. Anda dapat menonaktifkan statusnya.");
}
$name = $simperType->name;
$simperType->delete();
return back()->with('success', "Jenis SIMPER '{$name}' berhasil dihapus.");
DB::transaction(function () use ($simperType) {
$simperType->employeeSimpers()->delete();
$simperType->delete();
});
return back()->with('success', "Jenis SIMPER '{$name}' berhasil dihapus dan dipindahkan ke Tong Sampah.");
}
}
+12
View File
@@ -378,6 +378,12 @@ class TrashService
->where('employee_id', $item->id)
->restore();
}
if ($item instanceof SimperType) {
EmployeeSimper::onlyTrashed()
->where('simper_type_id', $item->id)
->restore();
}
});
return true;
@@ -420,6 +426,12 @@ class TrashService
->forceDelete();
}
if ($item instanceof SimperType) {
EmployeeSimper::withTrashed()
->where('simper_type_id', $item->id)
->forceDelete();
}
$item->forceDelete();
});
@@ -180,9 +180,9 @@
<i class="fa-regular fa-pen-to-square text-xs"></i>
</button>
@endif
@if((auth()->user()->can('hse.simper_types.delete') || auth()->user()->can('hse.employees.edit')) && $st->total_holders_count == 0)
@if(auth()->user()->hasRole('super_admin') || auth()->user()->can('hse.simper_types.delete') || auth()->user()->can('hse.employees.edit'))
<button type="button"
onclick="openDeleteSimperTypeModal({{ $st->id }}, '{{ $st->name }}')"
onclick="openDeleteSimperTypeModal({{ $st->id }}, '{{ addslashes($st->name) }}', {{ $st->total_holders_count }})"
class="inline-flex items-center justify-center w-8 h-8 rounded-lg text-rose-700 bg-rose-50 hover:bg-rose-100 transition border border-rose-200 cursor-pointer"
title="Hapus Jenis SIMPER">
<i class="fa-regular fa-trash-can text-xs"></i>
@@ -504,22 +504,26 @@
<!-- MODAL: HAPUS JENIS SIMPER -->
<!-- ========================================================================= -->
<div id="deleteSimperTypeModal" data-backdrop="static" class="hidden fixed inset-0 z-50 bg-slate-900/50 backdrop-blur-sm flex items-center justify-center p-4">
<div class="bg-white rounded-3xl shadow-2xl max-w-sm w-full p-6 border border-slate-100 text-center animate-scale-in">
<div class="bg-white rounded-3xl shadow-2xl max-w-sm sm:max-w-md w-full p-6 border border-slate-100 text-center animate-scale-in">
<div class="w-12 h-12 rounded-full bg-rose-100 text-rose-600 flex items-center justify-center text-xl mx-auto mb-3">
<i class="fa-solid fa-triangle-exclamation"></i>
</div>
<h3 class="text-sm font-bold text-slate-900 mb-1">Hapus Jenis SIMPER?</h3>
<p class="text-xs text-slate-500 mb-4">
<p class="text-xs text-slate-500 mb-3">
Apakah Anda yakin ingin menghapus jenis SIMPER <strong id="deleteSimperTypeName" class="text-slate-800"></strong>?
</p>
<div id="deleteSimperTypeWarning" class="hidden mb-4 p-3 bg-amber-50 border border-amber-200 rounded-xl text-left text-xs text-amber-800">
<span class="font-bold">Perhatian:</span> Jenis SIMPER ini saat ini tercatat pada <span id="deleteSimperHoldersCount" class="font-bold"></span> personil. Data jenis SIMPER beserta izin terkait akan dipindahkan ke Tong Sampah.
</div>
<form id="deleteSimperTypeForm" action="" method="POST" class="flex items-center justify-center gap-2">
@csrf
@method('DELETE')
<button type="button" onclick="closeDeleteSimperTypeModal()" class="px-4 py-2 rounded-xl text-xs font-bold text-slate-600 hover:bg-slate-100 transition">
<button type="button" onclick="closeDeleteSimperTypeModal()" class="px-4 py-2 rounded-xl text-xs font-bold text-slate-600 hover:bg-slate-100 transition cursor-pointer">
Batal
</button>
<button type="submit" class="bg-rose-600 hover:bg-rose-700 text-white px-5 py-2 rounded-xl text-xs font-bold transition shadow-xs">
<button type="submit" class="bg-rose-600 hover:bg-rose-700 text-white px-5 py-2 rounded-xl text-xs font-bold transition shadow-xs cursor-pointer">
Ya, Hapus
</button>
</form>
@@ -670,8 +674,16 @@
if (modal) modal.classList.add('hidden');
}
function openDeleteSimperTypeModal(id, name) {
function openDeleteSimperTypeModal(id, name, holdersCount = 0) {
document.getElementById('deleteSimperTypeName').textContent = name;
const warning = document.getElementById('deleteSimperTypeWarning');
const holdersEl = document.getElementById('deleteSimperHoldersCount');
if (holdersCount > 0) {
holdersEl.textContent = `${holdersCount}`;
warning.classList.remove('hidden');
} else {
warning.classList.add('hidden');
}
const form = document.getElementById('deleteSimperTypeForm');
form.action = `/hse/simper-types/${id}`;
+95
View File
@@ -330,4 +330,99 @@ class HseSimperTypeTest extends TestCase
$this->assertTrue($employee->hasValidSimperForVehicle($vehicleLoader));
$this->assertFalse($employee->hasValidSimperForVehicle($vehicleForklift));
}
/**
* Super Admin dan Admin HSE dapat melihat tombol delete dan menghapus jenis SIMPER.
*/
public function test_authorized_user_and_super_admin_can_delete_simper_type(): void
{
$superAdmin = User::factory()->create([
'role' => 'super_admin',
]);
$superAdmin->assignRole(Role::firstOrCreate(['name' => 'super_admin']));
$simperType = SimperType::create([
'code' => 'SIMPER-CRANE-DEL',
'name' => 'SIMPER Mobile Crane Test',
'category_scope' => 'unit_support_heavy',
'is_active' => true,
]);
// Super Admin melihat tombol hapus pada halaman index
$resIndex = $this->actingAs($superAdmin)->get(route('hse.simper_types.index'));
$resIndex->assertOk();
$resIndex->assertSee('SIMPER-CRANE-DEL');
$resIndex->assertSee('openDeleteSimperTypeModal', false);
// Eksekusi hapus oleh Super Admin
$resDelete = $this->actingAs($superAdmin)->delete(route('hse.simper_types.destroy', $simperType->id));
$resDelete->assertRedirect();
$resDelete->assertSessionHas('success');
$this->assertSoftDeleted('simper_types', [
'id' => $simperType->id,
]);
}
/**
* User tanpa hak akses tidak dapat menghapus jenis SIMPER (403).
*/
public function test_unauthorized_user_cannot_delete_simper_type(): void
{
$simperType = SimperType::create([
'code' => 'SIMPER-NO-DEL',
'name' => 'SIMPER Terlindungi',
'category_scope' => 'all',
'is_active' => true,
]);
$response = $this->actingAs($this->regularUser)->delete(route('hse.simper_types.destroy', $simperType->id));
$response->assertForbidden();
$this->assertDatabaseHas('simper_types', [
'id' => $simperType->id,
'deleted_at' => null,
]);
}
/**
* Menghapus jenis SIMPER meng-cascade soft delete ke tabel employee_simpers terkait.
*/
public function test_deleting_simper_type_cascades_soft_deletes_to_employee_simpers(): void
{
$superAdmin = User::factory()->create([
'role' => 'super_admin',
]);
$superAdmin->assignRole(Role::firstOrCreate(['name' => 'super_admin']));
$simperType = SimperType::create([
'code' => 'SIMPER-CASCADE-01',
'name' => 'SIMPER Dozer & Grader',
'category_scope' => 'all',
'is_active' => true,
]);
$employee = Employee::create([
'nik' => 'EMP-SIMPER-DEL',
'name' => 'Karyawan Pemegang SIMPER',
'is_operator' => true,
]);
$empSimper = EmployeeSimper::create([
'employee_id' => $employee->id,
'simper_type_id' => $simperType->id,
'simper_number' => 'SMP-CASCADE-99',
'expired_at' => Carbon::now()->addYear(),
'status' => 'active',
]);
// Hapus jenis SIMPER
$response = $this->actingAs($superAdmin)->delete(route('hse.simper_types.destroy', $simperType->id));
$response->assertRedirect();
$response->assertSessionHas('success');
// Keduanya masuk ke tong sampah
$this->assertSoftDeleted('simper_types', ['id' => $simperType->id]);
$this->assertSoftDeleted('employee_simpers', ['id' => $empSimper->id]);
}
}