feat(admin): add clear activity logs button and artisan clean-test-logs command
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Models\AdminActivityLog;
|
||||
use App\Models\UnitSupportTimesheet;
|
||||
use App\Models\UnitTransfer;
|
||||
use App\Models\Vehicle;
|
||||
use App\Models\VehicleActivity;
|
||||
use App\Models\VehicleLocationHistory;
|
||||
use App\Services\TrashService;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CleanTestLogsCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'kmh:clean-test-logs
|
||||
{--force : Jalankan pembersihan tanpa konfirmasi interaktif}
|
||||
{--include-he : Sertakan juga pembersihan timesheet alat berat & transfer unit}
|
||||
{--empty-trash : Kosongkan seluruh item di Tong Sampah}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Bersihkan seluruh riwayat transaksi uji coba (Timesheet LV, Riwayat Pos Gerbang, Log Aktivitas Admin) menjelang launching.';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle(TrashService $trashService): int
|
||||
{
|
||||
$this->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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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.");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user