From 158106c758c2c1764d1fe2a8282715bad1c19aa5 Mon Sep 17 00:00:00 2001 From: ricky rx Date: Wed, 12 Jun 2024 11:55:11 +0700 Subject: [PATCH] chore: change code for ik http transaction & timeout --- app/Helper/STS/Indokargo.php | 100 +++++++++------------------ app/Providers/AppServiceProvider.php | 2 +- 2 files changed, 33 insertions(+), 69 deletions(-) diff --git a/app/Helper/STS/Indokargo.php b/app/Helper/STS/Indokargo.php index 6662f3b..445b9de 100644 --- a/app/Helper/STS/Indokargo.php +++ b/app/Helper/STS/Indokargo.php @@ -31,6 +31,31 @@ class Indokargo { throw new \Exception("Indokargo status is not valid (ik status = $status)" ); } } + private static function _HttpTransaction(StsLog $stsLog, $callback) { + try { + return $callback(); + } catch(ConnectionException $e) { + // if error cause by connection error, try again later + $stsLog->error_info = [ + 'message' => $e->getMessage(), + 'errors' => $e->getTrace() + ]; + $stsLog->result = StsLog::STATUS_FAILED; + $stsLog->is_retry = true; + $stsLog->save(); + return JSONResponse::Success(['is_warning' => true, + 'message' => "Failed to sync data with indokargo data, don't worry, we will sync data later"]); + } catch(\Throwable $th) { + DB::rollBack(); + $stsLog->error_info = [ + 'message' => $th->getMessage(), + 'errors' => $th->getTrace() + ]; + $stsLog->result = StsLog::STATUS_FAILED; + $stsLog->save(); + throw $th; + } + } //-- END UTILITIES //------------------------------------------------------------------------ @@ -53,7 +78,7 @@ class Indokargo { $stsLog->request_data = $request->all(); $stsLog->request_time = Carbon::now(); - try { + return self::_HttpTransaction($stsLog, function() use($tvFk, $request, $stsLog) { $request->validate(['code' => 'required|string']); $result = Http::indokargo()->post('tv/address/create', $request->all()); @@ -85,27 +110,7 @@ class Indokargo { $stsLog->save(); DB::commit(); return JSONResponse::Success(['is_warning' => false, 'message' => 'Success To Save Data']); - } catch(ConnectionException $e) { - // if error cause by connection error, try again later - $stsLog->error_info = [ - 'message' => $e->getMessage(), - 'errors' => $e->getTrace() - ]; - $stsLog->result = StsLog::STATUS_FAILED; - $stsLog->is_retry = true; - $stsLog->save(); - return JSONResponse::Success(['is_warning' => true, - 'message' => "Failed to sync data with indokargo data, don't worry, we will sync data later"]); - } catch(\Throwable $th) { - DB::rollBack(); - $stsLog->error_info = [ - 'message' => $th->getMessage(), - 'errors' => $th->getTrace() - ]; - $stsLog->result = StsLog::STATUS_FAILED; - $stsLog->save(); - throw $th; - } + }); } public static function updateTVAddress(Request $request, int $tvFk, String $ikAddressId, int $lastSeq = 0) { @@ -123,7 +128,7 @@ class Indokargo { $stsLog->request_data = $request->all(); $stsLog->request_time = Carbon::now(); - try { + return self::_HttpTransaction($stsLog, function() use($stsLog, $request) { $request->validate(['code' => 'required|string', 'ik_address_id' => 'required|string']); $result = Http::indokargo()->post('tv/address/update-tv', $request->all()); $res = $result->throw()->json(); @@ -135,29 +140,8 @@ class Indokargo { $stsLog->result = StsLog::STATUS_SUCCESS; $stsLog->save(); DB::commit(); - - return JSONResponse::Success(['is_warning' => false, 'message' => 'Success To Save Data']); - } catch(ConnectionException $e) { - // if error cause by connection error, try again later - $stsLog->error_info = [ - 'message' => $e->getMessage(), - 'errors' => $e->getTrace() - ]; - $stsLog->result = StsLog::STATUS_FAILED; - $stsLog->is_retry = true; - $stsLog->save(); - return JSONResponse::Success(['is_warning' => true, - 'message' => "Failed to sync data with indokargo data, don't worry, we will sync data later"]); - } catch(\Throwable $th) { - DB::rollBack(); - $stsLog->error_info = [ - 'message' => $th->getMessage(), - 'errors' => $th->getTrace() - ]; - $stsLog->result = StsLog::STATUS_FAILED; - $stsLog->save(); - throw $th; - } + return JSONResponse::Success(['message' => "Success to save data"]); + }); } public static function changeStatusAddress(Request $request, int $tvFk, String $ikAddressId, int $lastSeq = 0) { @@ -175,7 +159,7 @@ class Indokargo { $stsLog->request_data = $request->all(); $stsLog->request_time = Carbon::now(); - try { + return self::_HttpTransaction($stsLog, function() use($stsLog, $request) { $request->validate(['ik_address_id' => 'required|string', 'is_active'=> 'required|boolean']); $result = Http::indokargo()->post('tv/address/change-status', $request->all()); $res = $result->throw()->json(); @@ -189,27 +173,7 @@ class Indokargo { DB::commit(); return JSONResponse::Success(['is_warning' => false, 'message' => 'Success To Change Status']); - } catch(ConnectionException $e) { - // if error cause by connection error, try again later - $stsLog->error_info = [ - 'message' => $e->getMessage(), - 'errors' => $e->getTrace() - ]; - $stsLog->result = StsLog::STATUS_FAILED; - $stsLog->is_retry = true; - $stsLog->save(); - return JSONResponse::Success(['is_warning' => true, - 'message' => "Failed to sync data with indokargo data, don't worry, we will sync data later"]); - } catch(\Throwable $th) { - DB::rollBack(); - $stsLog->error_info = [ - 'message' => $th->getMessage(), - 'errors' => $th->getTrace() - ]; - $stsLog->result = StsLog::STATUS_FAILED; - $stsLog->save(); - throw $th; - } + }); } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index a8cc324..0f630e2 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -25,7 +25,7 @@ class AppServiceProvider extends ServiceProvider 'session' => env('INDOKARGO_API_SESSION_ID', '') ]; - return Http::withHeaders($headers) + return Http::withHeaders($headers)->timeout(120) ->baseUrl(env('INDOKARGO_API_URL', '').'/v1/'); }); }