refactor(webstatement): perbaikan struktur dan penyempurnaan kode pada GenerateBiayaKartuCsvJob

- Mengubah konversi konstanta MAX_EXECUTION_TIME menjadi properti.
- Memindahkan dan merapikan fungsi-fungsi `updateCsvLogStart`, `updateCsvLogSuccess`, dan `updateCsvLogFailed` untuk meningkatkan keterbacaan.
- Mengubah format deklarasi fungsi untuk konsistensi dalam penggunaan tipe data return.
- Memperbaiki logika pengambilan file info pada proses pembuatan dan pengunggahan CSV.
- Menambahkan logging yang lebih terstruktur untuk setiap proses (mulai, sukses, gagal).

Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
This commit is contained in:
Daeng Deni Mardaeni
2025-05-11 17:14:08 +07:00
parent 7df50b5141
commit 012d1629c9

View File

@@ -19,26 +19,22 @@
class GenerateBiayaKartuCsvJob implements ShouldQueue class GenerateBiayaKartuCsvJob implements ShouldQueue
{ {
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
// Changed from const to property // Changed from const to property
private const MAX_EXECUTION_TIME = 3600; // 1 jam dalam detik private const MAX_EXECUTION_TIME = 3600; // 1 jam dalam detik
private $csvFilename;
/** /**
* Periode yang akan diproses (YYYY-MM) * Periode yang akan diproses (YYYY-MM)
*/ */
protected $periode; protected $periode;
/** /**
* ID log sinkronisasi * ID log sinkronisasi
*/ */
protected $syncLogId; protected $syncLogId;
/** /**
* Model log sinkronisasi * Model log sinkronisasi
*/ */
protected $syncLog; protected $syncLog;
private $csvFilename;
/** /**
* Create a new job instance. * Create a new job instance.
@@ -46,21 +42,10 @@
public function __construct() public function __construct()
{ {
$this->csvFilename = env('BIAYA_KARTU_CSV_FILENAME', 'biaya_kartu_atm.csv'); $this->csvFilename = env('BIAYA_KARTU_CSV_FILENAME', 'biaya_kartu_atm.csv');
$this->periode = $periode ?? Carbon::now()->format('Y-m'); $this->periode = $periode ?? Carbon::now()->format('Y-m');
$this->syncLogId = KartuSyncLog::where('periode', $this->periode)->first(); $this->syncLogId = KartuSyncLog::where('periode', $this->periode)->first();
} }
/**
* Get default fees from JenisKartu table
*
* @return array
*/
private function getDefaultFees(): array
{
return JenisKartu::getDefaultFees();
}
/** /**
* Execute the job. * Execute the job.
*/ */
@@ -70,7 +55,7 @@
set_time_limit(self::MAX_EXECUTION_TIME); set_time_limit(self::MAX_EXECUTION_TIME);
// Load log sinkronisasi // Load log sinkronisasi
$this->syncLog = KartuSyncLog::findOrFail($this->syncLogId); $this->syncLog = KartuSyncLog::findOrFail($this->syncLogId->id);
try { try {
@@ -97,14 +82,29 @@
$this->updateCsvLogFailed($e->getMessage()); $this->updateCsvLogFailed($e->getMessage());
Log::error('Gagal membuat atau mengunggah file CSV biaya kartu ATM: ' . $e->getMessage(), [ Log::error('Gagal membuat atau mengunggah file CSV biaya kartu ATM: ' . $e->getMessage(), [
'file' => $e->getFile(), 'file' => $e->getFile(),
'line' => $e->getLine(), 'line' => $e->getLine(),
'periode' => $this->periode 'periode' => $this->periode
]); ]);
throw $e; throw $e;
} }
} }
/**
* Update log saat pembuatan CSV dimulai
*/
private function updateCsvLogStart()
: void
{
$this->syncLog->update([
'csv_notes' => 'Pembuatan file CSV untuk periode ' . $this->periode . ' dimulai',
'is_csv' => false,
'csv_at' => null,
]);
Log::info('Memulai pembuatan file CSV untuk periode ' . $this->periode);
}
/** /**
* Generate CSV file with ATM card data and upload to SFTP * Generate CSV file with ATM card data and upload to SFTP
* *
@@ -113,11 +113,11 @@
private function generateAtmCardCsv() private function generateAtmCardCsv()
: array : array
{ {
$cards = $this->getEligibleAtmCards(); $cards = $this->getEligibleAtmCards();
//$filename = storage_path('app/' . $this->csvFilename); //$filename = storage_path('app/' . $this->csvFilename);
// Add date-time to filename // Add date-time to filename
$dateTime = now()->format('Ymd_Hi'); $dateTime = now()->format('Ymd_Hi');
$filenameWithDate = pathinfo($this->csvFilename, PATHINFO_FILENAME) . $filenameWithDate = pathinfo($this->csvFilename, PATHINFO_FILENAME) .
'_' . $dateTime . '.' . '_' . $dateTime . '.' .
pathinfo($this->csvFilename, PATHINFO_EXTENSION); pathinfo($this->csvFilename, PATHINFO_EXTENSION);
@@ -172,6 +172,17 @@
->get(); ->get();
} }
/**
* Get default fees from JenisKartu table
*
* @return array
*/
private function getDefaultFees()
: array
{
return JenisKartu::getDefaultFees();
}
/** /**
* Determine fee for a card based on its type * Determine fee for a card based on its type
* *
@@ -297,71 +308,24 @@
$this->updateSftpLogFailed($e->getMessage()); $this->updateSftpLogFailed($e->getMessage());
Log::error("Error saat mengunggah file ke SFTP: " . $e->getMessage(), [ Log::error("Error saat mengunggah file ke SFTP: " . $e->getMessage(), [
'file' => $e->getFile(), 'file' => $e->getFile(),
'line' => $e->getLine(), 'line' => $e->getLine(),
'periode' => $this->periode 'periode' => $this->periode
]); ]);
return false; return false;
} }
} }
/**
* Update log saat pembuatan CSV dimulai
*/
private function updateCsvLogStart(): void
{
$this->syncLog->update([
'csv_notes' => 'Pembuatan file CSV untuk periode ' . $this->periode . ' dimulai',
'is_csv' => false,
'csv_at' => null,
]);
Log::info('Memulai pembuatan file CSV untuk periode ' . $this->periode);
}
/**
* Update log saat pembuatan CSV berhasil
*/
private function updateCsvLogSuccess($result): void
{
// Get file info
$fileInfo = pathinfo($result['localFilePath']);
$fileName = $fileInfo['basename'];
$fileSize = Storage::size($result['localFilePath']);
$this->syncLog->update([
'is_csv' => true,
'csv_at' => Carbon::now(),
'csv_notes' => 'File CSV berhasil dibuat: ' . $fileName . ' (' . $this->formatSize($fileSize) . ')',
'file_path' => storage_path('app/' . $result['localFilePath']),
'file_name' => $fileName,
]);
Log::info('File CSV berhasil dibuat: ' . $result['localFilePath']);
}
/**
* Update log saat pembuatan CSV gagal
*/
private function updateCsvLogFailed($errorMessage): void
{
$this->syncLog->update([
'is_csv' => false,
'csv_notes' => 'Pembuatan file CSV gagal: ' . $errorMessage
]);
Log::error('Pembuatan file CSV gagal: ' . $errorMessage);
}
/** /**
* Update log saat upload SFTP dimulai * Update log saat upload SFTP dimulai
*/ */
private function updateSftpLogStart(): void private function updateSftpLogStart()
: void
{ {
$this->syncLog->update([ $this->syncLog->update([
'ftp_notes' => 'Upload ke SFTP untuk file periode ' . $this->periode . ' dimulai', 'ftp_notes' => 'Upload ke SFTP untuk file periode ' . $this->periode . ' dimulai',
'is_ftp' => false, 'is_ftp' => false,
'ftp_at' => null, 'ftp_at' => null,
]); ]);
Log::info('Memulai upload ke SFTP untuk periode ' . $this->periode); Log::info('Memulai upload ke SFTP untuk periode ' . $this->periode);
@@ -370,12 +334,13 @@
/** /**
* Update log saat upload SFTP berhasil * Update log saat upload SFTP berhasil
*/ */
private function updateSftpLogSuccess(): void private function updateSftpLogSuccess()
: void
{ {
$this->syncLog->update([ $this->syncLog->update([
'is_ftp' => true, 'is_ftp' => true,
'ftp_at' => Carbon::now(), 'ftp_at' => Carbon::now(),
'ftp_notes' => 'File berhasil diupload ke SFTP', 'ftp_notes' => 'File berhasil diupload ke SFTP',
'ftp_destination' => env('SFTP_KARTU_HOST', '/'), 'ftp_destination' => env('SFTP_KARTU_HOST', '/'),
]); ]);
@@ -385,15 +350,49 @@
/** /**
* Update log saat upload SFTP gagal * Update log saat upload SFTP gagal
*/ */
private function updateSftpLogFailed($errorMessage): void private function updateSftpLogFailed($errorMessage)
: void
{ {
$this->syncLog->update([ $this->syncLog->update([
'is_ftp' => false, 'is_ftp' => false,
'ftp_notes' => 'Upload ke SFTP gagal: ' . $errorMessage 'ftp_notes' => 'Upload ke SFTP gagal: ' . $errorMessage
]); ]);
Log::error('Upload ke SFTP gagal: ' . $errorMessage); Log::error('Upload ke SFTP gagal: ' . $errorMessage);
} }
/**
* Update log saat pembuatan CSV berhasil
*/
private function updateCsvLogSuccess($result)
: void
{
// Get file info
$fileInfo = pathinfo($result['localFilePath']);
$fileName = $fileInfo['basename'];
$this->syncLog->update([
'is_csv' => true,
'csv_at' => Carbon::now(),
'csv_notes' => 'File CSV berhasil dibuat: ' . $fileName,
'file_path' => $result['localFilePath'],
'file_name' => $fileName,
]);
Log::info('File CSV berhasil dibuat: ' . $result['localFilePath']);
}
/**
* Update log saat pembuatan CSV gagal
*/
private function updateCsvLogFailed($errorMessage)
: void
{
$this->syncLog->update([
'is_csv' => false,
'csv_notes' => 'Pembuatan file CSV gagal: ' . $errorMessage
]);
Log::error('Pembuatan file CSV gagal: ' . $errorMessage);
}
} }