🧹 feat(CleanupSingleInspeksiCommand): Tambahkan command untuk cleanup inspeksi per permohonan
Membuat artisan command untuk cleanup data inspeksi secara spesifik untuk 1 permohonan dan user tertentu dengan opsi sync/async - Command signature: `lpj:cleanup-single-inspeksi` dengan required arguments permohonan-id dan created-by - Opsi --sync untuk eksekusi synchronous dan --force untuk skip konfirmasi - Validasi input permohonanId dan createdBy harus angka positif - Integrasi dengan InspeksiCleanupService untuk cleanupSync() dan cleanupAsync() - Konfirmasi interaktif sebelum proses (dapat di-skip dengan --force) - Comprehensive logging dengan context permohonan_id dan created_by - Error handling dengan try-catch dan return Command::SUCCESS/FAILURE - Informasi mode eksekusi (Synchronous vs Queue) ditampilkan ke user - Pesan sukses yang jelas untuk monitoring progress di log
This commit is contained in:
99
app/Console/Commands/CleanupSingleInspeksiCommand.php
Normal file
99
app/Console/Commands/CleanupSingleInspeksiCommand.php
Normal file
@@ -0,0 +1,99 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Lpj\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Modules\Lpj\Jobs\CleanupInspeksiDataJob;
|
||||
use Modules\Lpj\Services\InspeksiCleanupService;
|
||||
|
||||
class CleanupSingleInspeksiCommand extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'lpj:cleanup-single-inspeksi
|
||||
{permohonan-id : ID permohonan yang akan di-cleanup}
|
||||
{created-by : ID user yang membuat data}
|
||||
{--sync : Jalankan secara synchronous}
|
||||
{--force : Jalankan tanpa konfirmasi}';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'Cleanup data inspeksi untuk 1 permohonan dan user tertentu';
|
||||
|
||||
/**
|
||||
* Execute the console command.
|
||||
*/
|
||||
public function handle(): int
|
||||
{
|
||||
Log::info('CleanupSingleInspeksiCommand: Memulai proses cleanup', [
|
||||
'permohonan_id' => $this->argument('permohonan-id'),
|
||||
'created_by' => $this->argument('created-by'),
|
||||
'sync' => $this->option('sync')
|
||||
]);
|
||||
|
||||
try {
|
||||
$permohonanId = (int) $this->argument('permohonan-id');
|
||||
$createdBy = (int) $this->argument('created-by');
|
||||
$sync = $this->option('sync');
|
||||
$force = $this->option('force');
|
||||
|
||||
// Validasi input
|
||||
if ($permohonanId <= 0) {
|
||||
$this->error('Permohonan ID harus angka positif.');
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
if ($createdBy <= 0) {
|
||||
$this->error('Created By harus angka positif.');
|
||||
return Command::FAILURE;
|
||||
}
|
||||
|
||||
// Tampilkan info
|
||||
$this->info('=== Cleanup Single Inspeksi ===');
|
||||
$this->info("Permohonan ID: {$permohonanId}");
|
||||
$this->info("Created By: {$createdBy}");
|
||||
$this->info("Mode: " . ($sync ? 'Synchronous' : 'Queue'));
|
||||
$this->newLine();
|
||||
|
||||
// Konfirmasi jika tidak force
|
||||
if (!$force && !$this->confirm('Lanjutkan dengan cleanup?')) {
|
||||
$this->info('Cleanup dibatalkan.');
|
||||
return Command::SUCCESS;
|
||||
}
|
||||
|
||||
// Jalankan cleanup
|
||||
$cleanupService = new InspeksiCleanupService();
|
||||
|
||||
if ($sync) {
|
||||
$this->info('Menjalankan cleanup secara synchronous...');
|
||||
$cleanupService->cleanupSync($permohonanId, $createdBy);
|
||||
$this->info('Cleanup selesai.');
|
||||
} else {
|
||||
$this->info('Mengirim job ke queue...');
|
||||
$cleanupService->cleanupAsync($permohonanId, $createdBy);
|
||||
$this->info('Job telah di-dispatch ke queue.');
|
||||
$this->info('Monitor progress di log.');
|
||||
}
|
||||
|
||||
return Command::SUCCESS;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error('CleanupSingleInspeksiCommand: Terjadi error saat proses cleanup', [
|
||||
'permohonan_id' => $this->argument('permohonan-id'),
|
||||
'created_by' => $this->argument('created-by'),
|
||||
'error' => $e->getMessage(),
|
||||
'trace' => $e->getTraceAsString()
|
||||
]);
|
||||
|
||||
$this->error('Terjadi kesalahan: ' . $e->getMessage());
|
||||
return Command::FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user