diff --git a/app/Console/Commands/CleanTestLogsCommand.php b/app/Console/Commands/CleanTestLogsCommand.php new file mode 100644 index 0000000..35bb00f --- /dev/null +++ b/app/Console/Commands/CleanTestLogsCommand.php @@ -0,0 +1,128 @@ +info('==============================================================='); + $this->info(' PEMBERSIHAN DATA UJI COBA (PRE-LAUNCH CLEAN SLATE) - RKM-KMH '); + $this->info('==============================================================='); + $this->warn('Perintah ini akan membersihkan data riwayat pengujian:'); + $this->line(' - Riwayat Perjalanan LV / Perlintasan Gerbang (vehicle_activities)'); + $this->line(' - Riwayat Mutasi Lokasi Kendaraan (vehicle_location_histories)'); + $this->line(' - Log Riwayat Aktivitas Admin (admin_activity_logs)'); + + if ($this->option('include-he')) { + $this->line(' - Timesheet Kendaraan Berat / HE (unit_support_timesheets)'); + $this->line(' - Riwayat Mobilisasi / Transfer Unit (unit_transfers)'); + } + + if ($this->option('empty-trash')) { + $this->line(' - Seluruh data di Tong Sampah (Recycle Bin)'); + } + + $this->info("\nMaster Kendaraan, Data Karyawan, SIMPER, Pos Gerbang & Akun Pengguna TETAP 100% UTUH."); + + if (!$this->option('force')) { + if (!$this->confirm('Apakah Anda yakin ingin melanjutkan pembersihan riwayat uji coba ini?', false)) { + $this->warn('Operasi pembersihan dibatalkan.'); + return Command::SUCCESS; + } + } + + $this->output->write('Sedang membersihkan log riwayat uji coba... '); + + Schema::disableForeignKeyConstraints(); + + try { + // 1. Bersihkan Catatan Perjalanan LV & Perlintasan Gerbang + $activityCount = DB::table('vehicle_activities')->count(); + DB::table('vehicle_activities')->truncate(); + + // 2. Bersihkan Riwayat Lokasi Kendaraan + $locationHistoryCount = DB::table('vehicle_location_histories')->count(); + DB::table('vehicle_location_histories')->truncate(); + + // 3. Bersihkan Log Aktivitas Admin + $adminLogCount = DB::table('admin_activity_logs')->count(); + DB::table('admin_activity_logs')->truncate(); + + // 4. Bersihkan Timesheet HE & Transfer jika dipilih + $heCount = 0; + $transferCount = 0; + if ($this->option('include-he')) { + $heCount = DB::table('unit_support_timesheets')->count(); + DB::table('unit_support_timesheets')->truncate(); + + $transferCount = DB::table('unit_transfers')->count(); + DB::table('unit_transfers')->truncate(); + } + + // 5. Reset status site kendaraan kembali ke default ('inside_site') + DB::table('vehicles')->update([ + 'site_status' => 'inside_site', + 'location_updated_at' => null, + 'location_updated_by' => null, + 'location_note' => null, + ]); + + // 6. Kosongkan Tong Sampah jika diminta + $trashCount = 0; + if ($this->option('empty-trash')) { + $trashCount = $trashService->emptyTrash(); + } + + Schema::enableForeignKeyConstraints(); + + $this->info("SELESAI!\n"); + $this->table(['Kategori Log Dibersihkan', 'Jumlah Data Terhapus'], [ + ['Riwayat Perjalanan LV (vehicle_activities)', number_format($activityCount)], + ['Riwayat Mutasi Lokasi (vehicle_location_histories)', number_format($locationHistoryCount)], + ['Log Riwayat Aktivitas Admin (admin_activity_logs)', number_format($adminLogCount)], + ['Timesheet Alat Berat / HE', $this->option('include-he') ? number_format($heCount) : 'Dilewati'], + ['Riwayat Transfer Unit', $this->option('include-he') ? number_format($transferCount) : 'Dilewati'], + ['Tong Sampah (Recycle Bin)', $this->option('empty-trash') ? number_format($trashCount) : 'Dilewati'], + ]); + + $this->info("\nSistem sekarang bersih dan siap digunakan untuk peluncuran resmi (Launching)! 🎉"); + return Command::SUCCESS; + } catch (\Throwable $e) { + Schema::enableForeignKeyConstraints(); + $this->error("\nTerjadi kesalahan: " . $e->getMessage()); + return Command::FAILURE; + } + } +} diff --git a/app/Http/Controllers/Admin/AdminActivityLogController.php b/app/Http/Controllers/Admin/AdminActivityLogController.php index 273fdd8..6a54609 100644 --- a/app/Http/Controllers/Admin/AdminActivityLogController.php +++ b/app/Http/Controllers/Admin/AdminActivityLogController.php @@ -5,7 +5,9 @@ namespace App\Http\Controllers\Admin; use App\Http\Controllers\Controller; use App\Models\AdminActivityLog; use Carbon\Carbon; +use Illuminate\Http\RedirectResponse; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Hash; use Illuminate\View\View; class AdminActivityLogController extends Controller @@ -54,4 +56,31 @@ class AdminActivityLogController extends Controller return view('admin.activity_logs.index', compact('logs', 'stats')); } + + /** + * Kosongkan seluruh riwayat log aktivitas admin (Hanya Super Admin). + */ + public function clear(Request $request): RedirectResponse + { + $user = $request->user(); + if (!$user || (!$user->hasRole('super_admin') && $user->role !== 'super_admin')) { + abort(403, 'Akses ditolak. Hanya Super Admin yang berhak mengosongkan log aktivitas sistem.'); + } + + $confirmed = false; + if ($request->filled('confirmation') && trim($request->input('confirmation')) === 'KOSONGKAN') { + $confirmed = true; + } elseif ($request->filled('password') && Hash::check($request->input('password'), $user->password)) { + $confirmed = true; + } + + if (!$confirmed) { + return back()->with('error', 'Konfirmasi keamanan tidak sesuai. Silakan ketik kata "KOSONGKAN" atau masukkan password Super Admin Anda.'); + } + + $count = AdminActivityLog::count(); + AdminActivityLog::truncate(); + + return back()->with('success', "Seluruh rekaman log aktivitas admin berhasil dikosongkan ({$count} catatan log dibersihkan). Sistem siap untuk fase operasional baru."); + } } diff --git a/resources/views/admin/activity_logs/index.blade.php b/resources/views/admin/activity_logs/index.blade.php index 73c18cf..b8ad449 100644 --- a/resources/views/admin/activity_logs/index.blade.php +++ b/resources/views/admin/activity_logs/index.blade.php @@ -16,7 +16,14 @@

- + @if(auth()->user()?->hasRole('super_admin') || auth()->user()?->role === 'super_admin') + + @endif + Rekaman Keamanan Sistem @@ -518,10 +525,19 @@ document.getElementById('logDetailModal').classList.add('hidden'); } + function openClearLogsModal() { + document.getElementById('clearLogsModal')?.classList.remove('hidden'); + } + + function closeClearLogsModal() { + document.getElementById('clearLogsModal')?.classList.add('hidden'); + } + // Tutup saat klik di luar modal / Escape key document.addEventListener('keydown', function(e) { if (e.key === 'Escape') { closeLogDetailModal(); + closeClearLogsModal(); } }); @@ -530,5 +546,50 @@ closeLogDetailModal(); } }); + + document.getElementById('clearLogsModal')?.addEventListener('click', function(e) { + if (e.target === this) { + closeClearLogsModal(); + } + }); + +@if(auth()->user()?->hasRole('super_admin') || auth()->user()?->role === 'super_admin') + + +@endif @endsection diff --git a/routes/web.php b/routes/web.php index 5c4af44..3c65057 100644 --- a/routes/web.php +++ b/routes/web.php @@ -214,6 +214,7 @@ Route::middleware('auth')->group(function () { // Log Riwayat Aktivitas Super Admin Route::get('/activity-logs', [AdminActivityLogController::class, 'index'])->name('activity_logs.index'); + Route::post('/activity-logs/clear', [AdminActivityLogController::class, 'clear'])->name('activity_logs.clear'); // Backup & Restore Sistem (Database & Media) Route::get('/backups', [BackupController::class, 'index'])->name('backups.index'); diff --git a/tests/Feature/CleanTestLogsCommandTest.php b/tests/Feature/CleanTestLogsCommandTest.php new file mode 100644 index 0000000..b456e3e --- /dev/null +++ b/tests/Feature/CleanTestLogsCommandTest.php @@ -0,0 +1,107 @@ +seed(); + $this->seedSampleData(); + + $this->superAdmin = User::where('role', 'super_admin')->first(); + $this->regularUser = User::where('role', 'employee')->first(); + } + + public function test_artisan_clean_test_logs_command_cleans_activities_and_admin_logs(): void + { + // Pastikan ada data aktivitas dan log admin + AdminActivityLog::record( + actionType: 'create', + module: 'Test Module', + description: 'Uji coba aktivitas log admin' + ); + + $vehicle = Vehicle::first(); + $employee = Employee::first(); + $gate = Gate::first(); + + VehicleActivity::create([ + 'transaction_no' => 'TRX-TEST-001', + 'vehicle_id' => $vehicle->id, + 'driver_id' => $employee->id, + 'gate_id' => $gate->id, + 'direction' => 'in', + 'previous_km' => 95, + 'current_km' => 100, + 'distance' => 5, + 'purpose' => 'Uji Coba Lapangan', + 'recorded_at' => now(), + 'validation_status' => 'approved', + ]); + + $this->assertGreaterThan(0, VehicleActivity::count()); + $this->assertGreaterThan(0, AdminActivityLog::count()); + + // Jalankan perintah artisan pembersihan + $this->artisan('kmh:clean-test-logs --force') + ->assertSuccessful(); + + // Verifikasi log bersih + $this->assertEquals(0, VehicleActivity::count()); + $this->assertEquals(0, VehicleLocationHistory::count()); + $this->assertEquals(0, AdminActivityLog::count()); + + // Verifikasi master data tetap utuh + $this->assertGreaterThan(0, Vehicle::count()); + $this->assertGreaterThan(0, Employee::count()); + $this->assertGreaterThan(0, User::count()); + } + + public function test_super_admin_can_clear_activity_logs_via_web(): void + { + AdminActivityLog::record( + actionType: 'create', + module: 'Test Module', + description: 'Uji coba aktivitas log admin untuk web' + ); + + $this->assertGreaterThan(0, AdminActivityLog::count()); + + $response = $this->actingAs($this->superAdmin) + ->post(route('admin.activity_logs.clear'), [ + 'confirmation' => 'KOSONGKAN', + ]); + + $response->assertRedirect(); + $response->assertSessionHas('success'); + + $this->assertEquals(0, AdminActivityLog::count()); + } + + public function test_non_super_admin_cannot_clear_activity_logs_via_web(): void + { + $response = $this->actingAs($this->regularUser) + ->post(route('admin.activity_logs.clear'), [ + 'confirmation' => 'KOSONGKAN', + ]); + + $response->assertStatus(403); + } +}