added failover transaction to seize the error from transactions.
This commit is contained in:
@@ -14,6 +14,7 @@ use App\InOutMar;
|
||||
use App\KomuditiPurchase;
|
||||
use App\KomuditiTransaksi;
|
||||
use App\Marhun;
|
||||
use App\FailoverTransaction;
|
||||
use App\Moratorium;
|
||||
use App\Support\OnePercent;
|
||||
use App\Tebus;
|
||||
@@ -945,16 +946,19 @@ class Penebusan
|
||||
$array_trans_base = $request->only(['pinjaman','teller']);
|
||||
|
||||
try{
|
||||
DB::transaction(function () use ($kodcaw,$norujukan,$gadai,$cawangan,$array_tebus,$array_trans,$array_tebus_base,$array_trans_base) {
|
||||
$failover_response = $this->handleFailOverTransaction($kodcaw,'create',$norujukan,$request->teller);
|
||||
DB::transaction(function () use ($failover_response,$kodcaw,$norujukan,$gadai,$cawangan,$array_tebus,$array_trans) {
|
||||
$this->saveTebus($kodcaw,$norujukan,$gadai,$cawangan,$array_tebus);
|
||||
$this->createTransaction($cawangan,$kodcaw,$norujukan,$array_trans);
|
||||
|
||||
DB::connection('mysql7')->transaction(function () use ($kodcaw,$norujukan,$gadai,$array_tebus_base,$array_trans_base) {
|
||||
$this->saveTebusBase($kodcaw,$norujukan,$gadai,$array_tebus_base);
|
||||
$this->createTransactionBase($norujukan,$kodcaw,$array_trans_base);
|
||||
});
|
||||
$this->createTransaction($cawangan,$kodcaw,$norujukan,$array_trans,$failover_response['id']);
|
||||
});
|
||||
|
||||
DB::connection('mysql7')->transaction(function () use ($kodcaw,$norujukan,$gadai,$array_tebus_base,$array_trans_base) {
|
||||
$this->saveTebusBase($kodcaw,$norujukan,$gadai,$array_tebus_base);
|
||||
$this->createTransactionBase($norujukan,$kodcaw,$array_trans_base);
|
||||
});
|
||||
|
||||
$this->handleFailOverTransaction($kodcaw,'done',$norujukan,$request->teller,$failover_response['id']);
|
||||
|
||||
$array_response = [
|
||||
'result' => 'success',
|
||||
'data' => 1
|
||||
@@ -962,13 +966,20 @@ class Penebusan
|
||||
return response()->json($array_response);
|
||||
|
||||
}catch(\Exception $e){
|
||||
if (isset($failover_response['id'])) {
|
||||
$this->handleFailOverTransaction($kodcaw,'fail',$norujukan,$request->teller,$failover_response['id']);
|
||||
}
|
||||
|
||||
$array_response = [
|
||||
'result' => 'error',
|
||||
'data' => $e->getMessage()
|
||||
];
|
||||
|
||||
Log::debug('Exception: ' . $e->getMessage());
|
||||
Log::debug('Line: ' . $e->getLine());
|
||||
Log::error('Transaction failed', [
|
||||
'exception' => $e->getMessage(),
|
||||
'line' => $e->getLine(),
|
||||
'file' => $e->getFile(),
|
||||
]);
|
||||
|
||||
return response()->json($array_response);
|
||||
}
|
||||
@@ -1116,7 +1127,7 @@ class Penebusan
|
||||
]);
|
||||
}
|
||||
|
||||
private function createTransaction($cawangan,$kodcaw,$norujukan,$array_trans){
|
||||
private function createTransaction($cawangan,$kodcaw,$norujukan,$array_trans,$fail_id = 0){
|
||||
$current = Carbon::now();
|
||||
|
||||
/* Create Gadai */
|
||||
@@ -1242,6 +1253,9 @@ class Penebusan
|
||||
'historygadaian' => $new_sag_number,
|
||||
'tag_tebus' => $array_trans['jenistebus'],
|
||||
));
|
||||
|
||||
$this->handleFailOverTransaction($kodcaw,'update',$new_sag_number,$array_trans['teller'],$fail_id);
|
||||
|
||||
}
|
||||
|
||||
private function createTransactionBase($norujukan,$kodcaw,$array_trans_base){
|
||||
@@ -1268,5 +1282,43 @@ class Penebusan
|
||||
]);
|
||||
}
|
||||
|
||||
private function handleFailOverTransaction($kodcaw,$status,$norujukan,$teller,$fail_id = 0){
|
||||
$current = Carbon::now();
|
||||
|
||||
if($status == 'create'){
|
||||
return FailoverTransaction::on('mysql7')->create([
|
||||
'kodcaw' => $kodcaw,
|
||||
'teller' => $teller,
|
||||
'old_norujukan' => $norujukan,
|
||||
'new_norujukan' => null,
|
||||
'db_caw' => false,
|
||||
'db_online' => false,
|
||||
'status' => 'pending'
|
||||
]);
|
||||
}else if($status == 'update'){
|
||||
return FailoverTransaction::on('mysql7')->where('id',$fail_id)->update([
|
||||
'new_norujukan' => $norujukan,
|
||||
'db_caw' => true,
|
||||
'db_online' => false,
|
||||
'status' => 'pending'
|
||||
]);
|
||||
|
||||
}else if($status == 'done'){
|
||||
return FailoverTransaction::on('mysql7')->where('id',$fail_id)->update([
|
||||
'new_norujukan' => $norujukan,
|
||||
'db_caw' => true,
|
||||
'db_online' => true,
|
||||
'status' => 'resolved',
|
||||
'resolve_time' => $current
|
||||
]);
|
||||
}else{
|
||||
return FailoverTransaction::on('mysql7')->where('id',$fail_id)->update([
|
||||
'old_norujukan' => $norujukan,
|
||||
'status' => 'fail',
|
||||
]);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user