Files
webstatement/app/Http/Controllers/MigrasiController.php
daengdeni 1e1120d29b refactor(migrasi): simplify and centralize data processing logic
- Menghapus metode proses data individual untuk setiap tipe data dengan menggantinya menjadi metode `processData`.
- Mengintegrasikan semua tipe proses data ke dalam konstanta `PROCESS_TYPES` untuk mempermudah pengelolaan dan memperluas tipe proses.
- Menambahkan konstanta `PARAMETER_PROCESSES` dan `DATA_PROCESSES` untuk memisahkan proses data parameter dan data utama.
- Mengimplementasikan metode `__call` untuk mendukung pemanggilan metode dinamis berdasarkan tipe proses data.
- Memperbaiki metode `index` untuk mendukung pemrosesan otomatis data parameter dan data utama dalam urutan yang ditentukan.
- Menambahkan logging untuk setiap proses data agar memudahkan debugging dan monitoring.
- Memastikan pengembalian respons JSON seragam untuk keberhasilan dan kegagalan setiap proses data.
2025-05-26 13:40:37 +07:00

122 lines
4.7 KiB
PHP

<?php
namespace Modules\Webstatement\Http\Controllers;
use App\Http\Controllers\Controller;
use Exception;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Storage;
use Log;
use Modules\Webstatement\Jobs\ProcessAccountDataJob;
use Modules\Webstatement\Jobs\ProcessArrangementDataJob;
use Modules\Webstatement\Jobs\ProcessAtmTransactionJob;
use Modules\Webstatement\Jobs\ProcessBillDetailDataJob;
use Modules\Webstatement\Jobs\ProcessCategoryDataJob;
use Modules\Webstatement\Jobs\ProcessCompanyDataJob;
use Modules\Webstatement\Jobs\ProcessCustomerDataJob;
use Modules\Webstatement\Jobs\ProcessDataCaptureDataJob;
use Modules\Webstatement\Jobs\ProcessFtTxnTypeConditionJob;
use Modules\Webstatement\Jobs\ProcessFundsTransferDataJob;
use Modules\Webstatement\Jobs\ProcessStmtEntryDataJob;
use Modules\Webstatement\Jobs\ProcessStmtNarrFormatDataJob;
use Modules\Webstatement\Jobs\ProcessStmtNarrParamDataJob;
use Modules\Webstatement\Jobs\ProcessTellerDataJob;
use Modules\Webstatement\Jobs\ProcessTransactionDataJob;
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,
'dataCapture' => ProcessDataCaptureDataJob::class,
'fundsTransfer' => ProcessFundsTransferDataJob::class,
'teller' => ProcessTellerDataJob::class,
'atmTransaction' => ProcessAtmTransactionJob::class,
'arrangement' => ProcessArrangementDataJob::class,
'billDetail' => ProcessBillDetailDataJob::class
];
private const PARAMETER_PROCESSES = [
'transaction',
'stmtNarrParam',
'stmtNarrFormat',
'ftTxnTypeCondition'
];
private const DATA_PROCESSES = [
'category',
'company',
'customer',
'account',
'stmtEntry',
'dataCapture',
'fundsTransfer',
'teller',
'atmTransaction',
'arrangement',
'billDetail'
];
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);
}
}
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.");
}
public function index()
{
$disk = Storage::disk('sftpStatement');
if (request('process_parameter')) {
foreach (self::PARAMETER_PROCESSES as $process) {
$this->processData($process, '_parameter');
}
return response()->json(['message' => 'Parameter processes completed successfully']);
}
$period = date('Ymd', strtotime('-1 day'));
if (!$disk->exists($period)) {
return response()->json([
"message" => "Period {$period} folder not found in SFTP storage"
], 404);
}
foreach (self::DATA_PROCESSES as $process) {
$this->processData($process, $period);
}
return response()->json([
'message' => "Data processing for period {$period} has been queued successfully"
]);
}
}