Menambahkan parameter --period pada command ProcessDailyMigration untuk fleksibilitas pemrosesan data harian. Perubahan yang dilakukan: - Menambahkan parameter --period dengan default '-1 day' pada command ProcessDailyMigration - Memungkinkan input period dalam berbagai format: - Format Ymd (contoh: 20250120) - Format relative date (contoh: '-2 days', '-1 week') - Default fallback ke '-1 day' jika parameter kosong atau format tidak valid - Update method index di MigrasiController untuk menerima dan memproses parameter period - Menambahkan method determinePeriod untuk konversi dan validasi parameter period - Menggunakan Carbon untuk parsing dan konversi tanggal - Menambahkan logging detail untuk tracking parameter input dan hasil konversi period - Menambahkan validasi dan error handling jika format periode tidak sesuai - Mempertahankan backward compatibility agar command lama tetap berjalan seperti sebelumnya - Update deskripsi command dan signature agar dokumentasi CLI lebih jelas Tujuan perubahan: - Memberikan fleksibilitas bagi tim operasional untuk menjalankan migrasi data dengan periode yang spesifik - Memudahkan eksekusi ulang data harian atau data backdate tanpa modifikasi kode - Memastikan proses migrasi lebih aman, transparan, dan dapat dipantau melalui logging
200 lines
7.6 KiB
PHP
200 lines
7.6 KiB
PHP
<?php
|
|
|
|
namespace Modules\Webstatement\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use BadMethodCallException;
|
|
use Exception;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Modules\Webstatement\Jobs\{ProcessAccountDataJob,
|
|
ProcessArrangementDataJob,
|
|
ProcessAtmTransactionJob,
|
|
ProcessBillDetailDataJob,
|
|
ProcessCategoryDataJob,
|
|
ProcessCompanyDataJob,
|
|
ProcessCustomerDataJob,
|
|
ProcessDataCaptureDataJob,
|
|
ProcessFtTxnTypeConditionJob,
|
|
ProcessFundsTransferDataJob,
|
|
ProcessStmtEntryDataJob,
|
|
ProcessStmtNarrFormatDataJob,
|
|
ProcessStmtNarrParamDataJob,
|
|
ProcessTellerDataJob,
|
|
ProcessTransactionDataJob,
|
|
ProcessSectorDataJob,
|
|
ProcessProvinceDataJob,
|
|
ProcessStmtEntryDetailDataJob};
|
|
|
|
class MigrasiController extends Controller
|
|
{
|
|
private const PROCESS_TYPES = [
|
|
'transaction' => ProcessTransactionDataJob::class,
|
|
'stmtNarrParam' => ProcessStmtNarrParamDataJob::class,
|
|
'stmtNarrFormat' => ProcessStmtNarrFormatDataJob::class,
|
|
'ftTxnTypeCondition' => ProcessFtTxnTypeConditionJob::class,
|
|
'category' => ProcessCategoryDataJob::class,
|
|
'company' => ProcessCompanyDataJob::class,
|
|
'customer' => ProcessCustomerDataJob::class,
|
|
'account' => ProcessAccountDataJob::class,
|
|
'stmtEntry' => ProcessStmtEntryDataJob::class,
|
|
'stmtEntryDetail' => ProcessStmtEntryDetailDataJob::class, // Tambahan baru
|
|
'dataCapture' => ProcessDataCaptureDataJob::class,
|
|
'fundsTransfer' => ProcessFundsTransferDataJob::class,
|
|
'teller' => ProcessTellerDataJob::class,
|
|
'atmTransaction' => ProcessAtmTransactionJob::class,
|
|
'arrangement' => ProcessArrangementDataJob::class,
|
|
'billDetail' => ProcessBillDetailDataJob::class,
|
|
'sector' => ProcessSectorDataJob::class,
|
|
'province' => ProcessProvinceDataJob::class
|
|
];
|
|
|
|
private const PARAMETER_PROCESSES = [
|
|
'transaction',
|
|
'stmtNarrParam',
|
|
'stmtNarrFormat',
|
|
'ftTxnTypeCondition',
|
|
'sector',
|
|
'province'
|
|
];
|
|
|
|
private const DATA_PROCESSES = [
|
|
'category',
|
|
'company',
|
|
'customer',
|
|
'account',
|
|
'stmtEntry',
|
|
'stmtEntryDetail', // Tambahan baru
|
|
'dataCapture',
|
|
'fundsTransfer',
|
|
'teller',
|
|
'atmTransaction',
|
|
'arrangement',
|
|
'billDetail'
|
|
];
|
|
|
|
public function __call($method, $parameters)
|
|
{
|
|
if (strpos($method, 'process') === 0) {
|
|
$type = lcfirst(substr($method, 7));
|
|
if (isset(self::PROCESS_TYPES[$type])) {
|
|
return $this->processData($type, $parameters[0] ?? '');
|
|
}
|
|
}
|
|
throw new BadMethodCallException("Method {$method} does not exist.");
|
|
}
|
|
|
|
private function processData(string $type, string $period)
|
|
: JsonResponse
|
|
{
|
|
try {
|
|
$jobClass = self::PROCESS_TYPES[$type];
|
|
$jobClass::dispatch($period);
|
|
|
|
$message = sprintf('%s data processing job has been queued successfully', ucfirst($type));
|
|
Log::info($message);
|
|
|
|
return response()->json(['message' => $message]);
|
|
} catch (Exception $e) {
|
|
Log::error(sprintf('Error in %s processing: %s', $type, $e->getMessage()));
|
|
return response()->json(['error' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
/**
|
|
* Proses migrasi data dengan parameter dan periode yang dapat dikustomisasi
|
|
*
|
|
* @param bool|string $processParameter Flag untuk memproses parameter
|
|
* @param string|null $period Periode yang akan diproses (default: -1 day)
|
|
* @return JsonResponse
|
|
*/
|
|
public function index($processParameter = false, $period = null)
|
|
{
|
|
try {
|
|
Log::info('Starting migration process', [
|
|
'process_parameter' => $processParameter,
|
|
'period' => $period
|
|
]);
|
|
|
|
$disk = Storage::disk('sftpStatement');
|
|
|
|
if ($processParameter) {
|
|
Log::info('Processing parameter data');
|
|
|
|
foreach (self::PARAMETER_PROCESSES as $process) {
|
|
$this->processData($process, '_parameter');
|
|
}
|
|
|
|
Log::info('Parameter processes completed successfully');
|
|
return response()->json(['message' => 'Parameter processes completed successfully']);
|
|
}
|
|
|
|
// Tentukan periode yang akan diproses
|
|
$targetPeriod = $this->determinePeriod($period);
|
|
|
|
Log::info('Processing data for period', ['period' => $targetPeriod]);
|
|
|
|
if (!$disk->exists($targetPeriod)) {
|
|
$errorMessage = "Period {$targetPeriod} folder not found in SFTP storage";
|
|
Log::warning($errorMessage);
|
|
|
|
return response()->json([
|
|
"message" => $errorMessage
|
|
], 404);
|
|
}
|
|
|
|
foreach (self::DATA_PROCESSES as $process) {
|
|
$this->processData($process, $targetPeriod);
|
|
}
|
|
|
|
$successMessage = "Data processing for period {$targetPeriod} has been queued successfully";
|
|
Log::info($successMessage);
|
|
|
|
return response()->json([
|
|
'message' => $successMessage
|
|
]);
|
|
} catch (Exception $e) {
|
|
Log::error('Error in migration index method: ' . $e->getMessage());
|
|
return response()->json(['error' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Tentukan periode berdasarkan input atau gunakan default
|
|
*
|
|
* @param string|null $period Input periode
|
|
* @return string Periode dalam format Ymd
|
|
*/
|
|
private function determinePeriod($period = null): string
|
|
{
|
|
if ($period === null) {
|
|
// Default: -1 day
|
|
$calculatedPeriod = date('Ymd', strtotime('-1 day'));
|
|
Log::info('Using default period', ['period' => $calculatedPeriod]);
|
|
return $calculatedPeriod;
|
|
}
|
|
|
|
// Jika periode sudah dalam format Ymd (8 digit)
|
|
if (preg_match('/^\d{8}$/', $period)) {
|
|
Log::info('Using provided period in Ymd format', ['period' => $period]);
|
|
return $period;
|
|
}
|
|
|
|
// Jika periode dalam format relative date (contoh: -2 days, -1 week, etc.)
|
|
try {
|
|
$calculatedPeriod = date('Ymd', strtotime($period));
|
|
Log::info('Calculated period from relative date', [
|
|
'input' => $period,
|
|
'calculated' => $calculatedPeriod
|
|
]);
|
|
return $calculatedPeriod;
|
|
} catch (Exception $e) {
|
|
Log::warning('Invalid period format, using default', [
|
|
'input' => $period,
|
|
'error' => $e->getMessage()
|
|
]);
|
|
return date('Ymd', strtotime('-1 day'));
|
|
}
|
|
}
|
|
}
|