refactor(webstatement): optimalkan proses sinkronisasi data kartu ATM
- Pisahkan logika sinkronisasi ke dalam metode-metode terpisah untuk meningkatkan keterbacaan kode. - Tambahkan konstanta untuk API dan database guna mengurangi duplikasi kode. - Ganti metode sinkronisasi kartu menjadi batch processing untuk efisiensi. - Implementasikan logging pada berbagai titik untuk mempermudah debugging. - Tambahkan penanganan error untuk proses batch sinkronisasi dan update data kartu. Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
This commit is contained in:
@@ -2,9 +2,11 @@
|
|||||||
|
|
||||||
namespace Modules\Webstatement\Jobs;
|
namespace Modules\Webstatement\Jobs;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
use Illuminate\Bus\Queueable;
|
use Illuminate\Bus\Queueable;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Http;
|
use Illuminate\Support\Facades\Http;
|
||||||
|
use Illuminate\Support\Facades\Log;
|
||||||
use Illuminate\Queue\SerializesModels;
|
use Illuminate\Queue\SerializesModels;
|
||||||
use Illuminate\Queue\InteractsWithQueue;
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
use Modules\Webstatement\Models\Atmcard;
|
use Modules\Webstatement\Models\Atmcard;
|
||||||
@@ -16,84 +18,136 @@ class BiayaKartu implements ShouldQueue
|
|||||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Create a new job instance.
|
* API dan database constants
|
||||||
*/
|
*/
|
||||||
public function __construct()
|
private const API_BASE_PATH = '/restgateway/services/IGATEToCoreBankingServices';
|
||||||
{
|
private const API_INQUIRY_PATH = '/InquiryBalanceService';
|
||||||
//
|
private const DB_TABLE = 'IST77.VW_CMS_VCARD';
|
||||||
}
|
private const BATCH_SIZE = 100;
|
||||||
|
private const MAX_EXECUTION_TIME = 86400; // 24 jam dalam detik
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Execute the job.
|
* Execute the job.
|
||||||
*/
|
*/
|
||||||
public function handle(): void
|
public function handle(): void
|
||||||
{
|
{
|
||||||
set_time_limit(24 * 60 * 60);
|
set_time_limit(self::MAX_EXECUTION_TIME);
|
||||||
$this->syncAtmCards();
|
$this->syncAtmCards();
|
||||||
}
|
}
|
||||||
|
|
||||||
function getAccountInfo($accountNumber){
|
/**
|
||||||
$url = $_ENV['FIORANO_URL'].'/restgateway/services/IGATEToCoreBankingServices';
|
* Get account information from the API
|
||||||
$path = '/InquiryBalanceService';
|
*
|
||||||
|
* @param string $accountNumber
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private function getAccountInfo(string $accountNumber): array
|
||||||
|
{
|
||||||
|
$url = env('FIORANO_URL') . self::API_BASE_PATH;
|
||||||
|
$path = self::API_INQUIRY_PATH;
|
||||||
$data = [
|
$data = [
|
||||||
'accountNo' => $accountNumber
|
'accountNo' => $accountNumber
|
||||||
];
|
];
|
||||||
|
|
||||||
$response = Http::post($url.$path, $data);
|
$response = Http::post($url . $path, $data);
|
||||||
|
|
||||||
return $response->json();
|
return $response->json();
|
||||||
}
|
}
|
||||||
|
|
||||||
function syncAtmCards()
|
/**
|
||||||
{
|
* Synchronize ATM cards data from Oracle database
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function syncAtmCards(): void
|
||||||
|
{
|
||||||
|
try {
|
||||||
|
$offset = 0;
|
||||||
|
$hasMoreRecords = true;
|
||||||
|
|
||||||
|
while ($hasMoreRecords) {
|
||||||
|
$cards = $this->fetchCardBatch($offset);
|
||||||
|
$this->processCardBatch($cards);
|
||||||
|
|
||||||
|
$hasMoreRecords = count($cards) === self::BATCH_SIZE;
|
||||||
|
$offset += self::BATCH_SIZE;
|
||||||
|
}
|
||||||
|
|
||||||
|
Log::info('Sinkronisasi kartu ATM berhasil diselesaikan');
|
||||||
|
} catch (Exception $e) {
|
||||||
|
Log::error('BiayaKartu: syncAtmCards: ' . $e->getMessage(), [
|
||||||
|
'file' => $e->getFile(),
|
||||||
|
'line' => $e->getLine()
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fetch a batch of ATM cards from the database
|
||||||
|
*
|
||||||
|
* @param int $offset
|
||||||
|
* @return \Illuminate\Support\Collection
|
||||||
|
*/
|
||||||
|
private function fetchCardBatch(int $offset)
|
||||||
|
{
|
||||||
|
return DB::connection('oracle')
|
||||||
|
->table(self::DB_TABLE)
|
||||||
|
->where('crsts', 1)
|
||||||
|
->whereNotNull('ACCFLAG')
|
||||||
|
->where('ACCFLAG', '>', 0)
|
||||||
|
->skip($offset)
|
||||||
|
->take(self::BATCH_SIZE)
|
||||||
|
->get();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Process a batch of ATM cards
|
||||||
|
*
|
||||||
|
* @param \Illuminate\Support\Collection $cards
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function processCardBatch($cards): void
|
||||||
|
{
|
||||||
|
foreach ($cards as $card) {
|
||||||
try {
|
try {
|
||||||
$start = 0;
|
$accountInfo = $this->getAccountInfo($card->accflag);
|
||||||
$limit = 100;
|
$this->updateOrCreateAtmCard($card, $accountInfo);
|
||||||
$loop = 1;
|
|
||||||
|
|
||||||
while ($loop) {
|
|
||||||
$cards = DB::connection('oracle')
|
|
||||||
->table('IST77.VW_CMS_VCARD')
|
|
||||||
->where('crsts', 1)
|
|
||||||
->where('ACCFLAG','!=',null)
|
|
||||||
->where('ACCFLAG', '>', 0)
|
|
||||||
->skip($start)
|
|
||||||
->take($limit)
|
|
||||||
->get();
|
|
||||||
|
|
||||||
foreach ($cards as $card) {
|
|
||||||
$accountInfo = $this->getAccountInfo($card->accflag);
|
|
||||||
Atmcard::updateOrCreate([
|
|
||||||
'crdno' => $card->crdno
|
|
||||||
], [
|
|
||||||
'accflag' => $card->accflag,
|
|
||||||
'cracc1' => $card->cracc1,
|
|
||||||
'cracc2' => $card->cracc2,
|
|
||||||
'cracc3' => $card->cracc3,
|
|
||||||
'cracc4' => $card->cracc4,
|
|
||||||
'cracc5' => $card->cracc5,
|
|
||||||
'craccnam1' => $card->craccnam1,
|
|
||||||
'craccnam2' => $card->craccnam2,
|
|
||||||
'craccnam3' => $card->craccnam3,
|
|
||||||
'craccnam4' => $card->craccnam4,
|
|
||||||
'craccnam5' => $card->craccnam5,
|
|
||||||
'crsts' => $card->crsts,
|
|
||||||
'cttype' => $card->cttype,
|
|
||||||
'ctdesc' => $card->ctdesc,
|
|
||||||
'last_update' => $card->lastupdate,
|
|
||||||
'branch' => $accountInfo->acctCompany,
|
|
||||||
'currency' => $accountInfo->acctCurrency,
|
|
||||||
]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (count($cards) < $limit) {
|
|
||||||
$loop = 0;
|
|
||||||
}
|
|
||||||
$start += $limit;
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (Exception $e) {
|
} catch (Exception $e) {
|
||||||
Log::error('SyncDwh: syncCurrency: ' . $e->getMessage());
|
Log::warning("Gagal memproses kartu {$card->crdno}: " . $e->getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update or create ATM card record
|
||||||
|
*
|
||||||
|
* @param object $card
|
||||||
|
* @param array $accountInfo
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
private function updateOrCreateAtmCard(object $card, array $accountInfo): void
|
||||||
|
{
|
||||||
|
Atmcard::updateOrCreate(
|
||||||
|
['crdno' => $card->crdno],
|
||||||
|
[
|
||||||
|
'accflag' => $card->accflag,
|
||||||
|
'cracc1' => $card->cracc1,
|
||||||
|
'cracc2' => $card->cracc2,
|
||||||
|
'cracc3' => $card->cracc3,
|
||||||
|
'cracc4' => $card->cracc4,
|
||||||
|
'cracc5' => $card->cracc5,
|
||||||
|
'craccnam1' => $card->craccnam1,
|
||||||
|
'craccnam2' => $card->craccnam2,
|
||||||
|
'craccnam3' => $card->craccnam3,
|
||||||
|
'craccnam4' => $card->craccnam4,
|
||||||
|
'craccnam5' => $card->craccnam5,
|
||||||
|
'crsts' => $card->crsts,
|
||||||
|
'cttype' => $card->cttype,
|
||||||
|
'ctdesc' => $card->ctdesc,
|
||||||
|
'last_update' => $card->lastupdate,
|
||||||
|
'branch' => $accountInfo['acctCompany'],
|
||||||
|
'currency' => $accountInfo['acctCurrency'],
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user