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 @@
+ Tindakan ini akan menghapus seluruh catatan riwayat aktivitas admin saat ini. Sangat disarankan dilakukan sebelum sistem resmi di-launching agar riwayat bersih. +
+