added failover transaction to seize the error from transactions.
This commit is contained in:
@@ -0,0 +1,43 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
|
||||||
|
class FailoverTransaction extends Model
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* The table associated with the model.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $table = 'failover_transaction';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that are mass assignable.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $fillable = [
|
||||||
|
'kodcaw',
|
||||||
|
'teller',
|
||||||
|
'old_norujukan',
|
||||||
|
'new_norujukan',
|
||||||
|
'db_caw',
|
||||||
|
'db_online',
|
||||||
|
'status',
|
||||||
|
'resolve_time',
|
||||||
|
];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The attributes that should be cast.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
protected $casts = [
|
||||||
|
'db_caw' => 'boolean',
|
||||||
|
'db_online' => 'boolean',
|
||||||
|
'resolve_time' => 'datetime',
|
||||||
|
];
|
||||||
|
}
|
||||||
@@ -14,6 +14,7 @@ use App\InOutMar;
|
|||||||
use App\KomuditiPurchase;
|
use App\KomuditiPurchase;
|
||||||
use App\KomuditiTransaksi;
|
use App\KomuditiTransaksi;
|
||||||
use App\Marhun;
|
use App\Marhun;
|
||||||
|
use App\FailoverTransaction;
|
||||||
use App\Moratorium;
|
use App\Moratorium;
|
||||||
use App\Support\OnePercent;
|
use App\Support\OnePercent;
|
||||||
use App\Tebus;
|
use App\Tebus;
|
||||||
@@ -945,16 +946,19 @@ class Penebusan
|
|||||||
$array_trans_base = $request->only(['pinjaman','teller']);
|
$array_trans_base = $request->only(['pinjaman','teller']);
|
||||||
|
|
||||||
try{
|
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->saveTebus($kodcaw,$norujukan,$gadai,$cawangan,$array_tebus);
|
||||||
$this->createTransaction($cawangan,$kodcaw,$norujukan,$array_trans);
|
$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);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
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 = [
|
$array_response = [
|
||||||
'result' => 'success',
|
'result' => 'success',
|
||||||
'data' => 1
|
'data' => 1
|
||||||
@@ -962,13 +966,20 @@ class Penebusan
|
|||||||
return response()->json($array_response);
|
return response()->json($array_response);
|
||||||
|
|
||||||
}catch(\Exception $e){
|
}catch(\Exception $e){
|
||||||
|
if (isset($failover_response['id'])) {
|
||||||
|
$this->handleFailOverTransaction($kodcaw,'fail',$norujukan,$request->teller,$failover_response['id']);
|
||||||
|
}
|
||||||
|
|
||||||
$array_response = [
|
$array_response = [
|
||||||
'result' => 'error',
|
'result' => 'error',
|
||||||
'data' => $e->getMessage()
|
'data' => $e->getMessage()
|
||||||
];
|
];
|
||||||
|
|
||||||
Log::debug('Exception: ' . $e->getMessage());
|
Log::error('Transaction failed', [
|
||||||
Log::debug('Line: ' . $e->getLine());
|
'exception' => $e->getMessage(),
|
||||||
|
'line' => $e->getLine(),
|
||||||
|
'file' => $e->getFile(),
|
||||||
|
]);
|
||||||
|
|
||||||
return response()->json($array_response);
|
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();
|
$current = Carbon::now();
|
||||||
|
|
||||||
/* Create Gadai */
|
/* Create Gadai */
|
||||||
@@ -1242,6 +1253,9 @@ class Penebusan
|
|||||||
'historygadaian' => $new_sag_number,
|
'historygadaian' => $new_sag_number,
|
||||||
'tag_tebus' => $array_trans['jenistebus'],
|
'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){
|
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',
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
class CreateFailoverTransactionTable extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function up()
|
||||||
|
{
|
||||||
|
Schema::connection('mysql7')->create('failover_transaction', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('kodcaw',10);
|
||||||
|
$table->string('teller',255);
|
||||||
|
$table->string('old_norujukan',50);
|
||||||
|
$table->string('new_norujukan',50)->nullable();
|
||||||
|
$table->boolean('db_caw')->default(false); // Boolean for db_caw, default to false
|
||||||
|
$table->boolean('db_online')->default(false);
|
||||||
|
$table->enum('status', ['pending', 'resolved', 'failed'])->default('pending'); // Use ENUM for statuses
|
||||||
|
$table->timestamp('resolve_time')->nullable(); // Timestamp for resolve time, nullable
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function down()
|
||||||
|
{
|
||||||
|
Schema::connection('mysql7')->dropIfExists('failover_transaction');
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user