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\Http\Controllers\Controller;
|
||||||
use App\Models\AdminActivityLog;
|
use App\Models\AdminActivityLog;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
|
use Illuminate\Http\RedirectResponse;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Support\Facades\Hash;
|
||||||
use Illuminate\View\View;
|
use Illuminate\View\View;
|
||||||
|
|
||||||
class AdminActivityLogController extends Controller
|
class AdminActivityLogController extends Controller
|
||||||
@@ -54,4 +56,31 @@ class AdminActivityLogController extends Controller
|
|||||||
|
|
||||||
return view('admin.activity_logs.index', compact('logs', 'stats'));
|
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.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,14 @@
|
|||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="flex items-center gap-2">
|
<div class="flex items-center gap-2">
|
||||||
<span class="inline-flex items-center gap-1.5 px-3 py-1.5 rounded-xl text-xs font-bold bg-indigo-50 text-indigo-700 border border-indigo-200">
|
@if(auth()->user()?->hasRole('super_admin') || auth()->user()?->role === 'super_admin')
|
||||||
|
<button type="button" onclick="openClearLogsModal()"
|
||||||
|
class="inline-flex items-center gap-1.5 px-3.5 py-2 rounded-xl text-xs font-bold text-rose-700 bg-rose-50 hover:bg-rose-100 border border-rose-200 transition cursor-pointer shadow-2xs">
|
||||||
|
<i class="fa-regular fa-trash-can text-sm"></i>
|
||||||
|
<span>Kosongkan Log</span>
|
||||||
|
</button>
|
||||||
|
@endif
|
||||||
|
<span class="inline-flex items-center gap-1.5 px-3 py-2 rounded-xl text-xs font-bold bg-indigo-50 text-indigo-700 border border-indigo-200">
|
||||||
<i class="fa-solid fa-shield-halved"></i>
|
<i class="fa-solid fa-shield-halved"></i>
|
||||||
<span>Rekaman Keamanan Sistem</span>
|
<span>Rekaman Keamanan Sistem</span>
|
||||||
</span>
|
</span>
|
||||||
@@ -518,10 +525,19 @@
|
|||||||
document.getElementById('logDetailModal').classList.add('hidden');
|
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
|
// Tutup saat klik di luar modal / Escape key
|
||||||
document.addEventListener('keydown', function(e) {
|
document.addEventListener('keydown', function(e) {
|
||||||
if (e.key === 'Escape') {
|
if (e.key === 'Escape') {
|
||||||
closeLogDetailModal();
|
closeLogDetailModal();
|
||||||
|
closeClearLogsModal();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -530,5 +546,50 @@
|
|||||||
closeLogDetailModal();
|
closeLogDetailModal();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
document.getElementById('clearLogsModal')?.addEventListener('click', function(e) {
|
||||||
|
if (e.target === this) {
|
||||||
|
closeClearLogsModal();
|
||||||
|
}
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
@if(auth()->user()?->hasRole('super_admin') || auth()->user()?->role === 'super_admin')
|
||||||
|
<!-- Modal Konfirmasi Kosongkan Log Aktivitas -->
|
||||||
|
<div id="clearLogsModal" class="hidden fixed inset-0 z-50 overflow-y-auto bg-slate-900/60 backdrop-blur-xs flex items-center justify-center p-4">
|
||||||
|
<div class="relative w-full max-w-md bg-white rounded-3xl shadow-2xl border border-slate-100 overflow-hidden transform transition-all animate-in fade-in zoom-in-95 duration-150">
|
||||||
|
<div class="p-6 text-center">
|
||||||
|
<div class="w-14 h-14 rounded-2xl bg-rose-50 text-rose-600 flex items-center justify-center mx-auto mb-4 border border-rose-100 text-2xl shadow-inner">
|
||||||
|
<i class="fa-solid fa-triangle-exclamation"></i>
|
||||||
|
</div>
|
||||||
|
<h3 class="text-lg font-black text-slate-900 tracking-tight">Kosongkan Riwayat Log Admin?</h3>
|
||||||
|
<p class="text-xs text-slate-500 mt-2 leading-relaxed">
|
||||||
|
Tindakan ini akan menghapus <strong>seluruh catatan riwayat aktivitas admin</strong> saat ini. Sangat disarankan dilakukan sebelum sistem resmi di-launching agar riwayat bersih.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<form method="POST" action="{{ route('admin.activity_logs.clear') }}" class="px-6 pb-6 space-y-4">
|
||||||
|
@csrf
|
||||||
|
<div>
|
||||||
|
<label class="block text-xs font-bold text-slate-700 mb-1.5 text-left">
|
||||||
|
Konfirmasi Keamanan (Ketik <span class="text-rose-600 font-mono">KOSONGKAN</span> atau Password Super Admin):
|
||||||
|
</label>
|
||||||
|
<input type="text" name="confirmation" placeholder="Ketik KOSONGKAN"
|
||||||
|
class="w-full text-xs px-3.5 py-2.5 rounded-xl border border-slate-200 focus:outline-none focus:border-rose-500 focus:ring-2 focus:ring-rose-500/20 font-mono transition">
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="flex items-center justify-end space-x-2 pt-2">
|
||||||
|
<button type="button" onclick="closeClearLogsModal()"
|
||||||
|
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="px-4 py-2 rounded-xl text-xs font-bold text-white bg-rose-600 hover:bg-rose-700 transition cursor-pointer shadow-sm">
|
||||||
|
<i class="fa-regular fa-trash-can mr-1.5"></i> Ya, Kosongkan Semua Log
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
@endif
|
||||||
@endsection
|
@endsection
|
||||||
|
|||||||
@@ -214,6 +214,7 @@ Route::middleware('auth')->group(function () {
|
|||||||
|
|
||||||
// Log Riwayat Aktivitas Super Admin
|
// Log Riwayat Aktivitas Super Admin
|
||||||
Route::get('/activity-logs', [AdminActivityLogController::class, 'index'])->name('activity_logs.index');
|
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)
|
// Backup & Restore Sistem (Database & Media)
|
||||||
Route::get('/backups', [BackupController::class, 'index'])->name('backups.index');
|
Route::get('/backups', [BackupController::class, 'index'])->name('backups.index');
|
||||||
|
|||||||
@@ -0,0 +1,107 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests\Feature;
|
||||||
|
|
||||||
|
use App\Models\AdminActivityLog;
|
||||||
|
use App\Models\Employee;
|
||||||
|
use App\Models\Gate;
|
||||||
|
use App\Models\User;
|
||||||
|
use App\Models\Vehicle;
|
||||||
|
use App\Models\VehicleActivity;
|
||||||
|
use App\Models\VehicleLocationHistory;
|
||||||
|
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||||
|
use Tests\TestCase;
|
||||||
|
|
||||||
|
class CleanTestLogsCommandTest extends TestCase
|
||||||
|
{
|
||||||
|
use RefreshDatabase;
|
||||||
|
|
||||||
|
protected User $superAdmin;
|
||||||
|
protected User $regularUser;
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
$this->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);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user