From a4e835119924c4a9d9a9a10b149660c3cd665ea7 Mon Sep 17 00:00:00 2001 From: paulwer Date: Sat, 16 Dec 2023 14:33:09 +0100 Subject: [PATCH] remove unnecessary code and change sync-behavior to suppress sync error based on wrong from_date --- .../Bank/ProcessBankTransactionsNordigen.php | 58 ++++++------------- 1 file changed, 17 insertions(+), 41 deletions(-) diff --git a/app/Jobs/Bank/ProcessBankTransactionsNordigen.php b/app/Jobs/Bank/ProcessBankTransactionsNordigen.php index acd010886e69..962bcb4cfd85 100644 --- a/app/Jobs/Bank/ProcessBankTransactionsNordigen.php +++ b/app/Jobs/Bank/ProcessBankTransactionsNordigen.php @@ -34,10 +34,6 @@ class ProcessBankTransactionsNordigen implements ShouldQueue private ?string $from_date; - private bool $stop_loop = true; - - private int $skip = 0; - public Company $company; public Nordigen $nordigen; public $nordigen_account; @@ -48,7 +44,7 @@ class ProcessBankTransactionsNordigen implements ShouldQueue public function __construct(BankIntegration $bank_integration) { $this->bank_integration = $bank_integration; - $this->from_date = $bank_integration->from_date; + $this->from_date = $bank_integration->from_date ?: now()->subDays(90); $this->company = $this->bank_integration->company; } @@ -71,9 +67,6 @@ class ProcessBankTransactionsNordigen implements ShouldQueue set_time_limit(0); - //Loop through everything until we are up to date - $this->from_date = $this->from_date ?: '2021-01-01'; - // UPDATE ACCOUNT try { $this->updateAccount(); @@ -94,27 +87,27 @@ class ProcessBankTransactionsNordigen implements ShouldQueue return; // UPDATE TRANSACTIONS - do { + try { + $this->processTransactions(); + } catch (\Exception $e) { + // reset from_date in case this was the error (self-heal) and perform a max sync of data we can fetch (next-time) @todo we should analyze the error for this + $this->bank_integration->from_date = now()->subDays(90); + $this->bank_integration->save(); - try { - $this->processTransactions(); - } catch (\Exception $e) { - nlog("{$this->bank_integration->account->key} - exited abnormally => " . $e->getMessage()); + nlog("{$this->bank_integration->account->key} - exited abnormally => " . $e->getMessage()); - $content = [ - "Processing transactions for account: {$this->bank_integration->account->key} failed", - "Exception Details => ", - $e->getMessage(), - ]; + $content = [ + "Processing transactions for account: {$this->bank_integration->account->key} failed", + "Exception Details => ", + $e->getMessage(), + ]; - $this->bank_integration->company->notification(new GenericNinjaAdminNotification($content))->ninja(); - - throw $e; - } + $this->bank_integration->company->notification(new GenericNinjaAdminNotification($content))->ninja(); + throw $e; } - while ($this->stop_loop); + // Perform Matching BankMatchingService::dispatch($this->company->id, $this->company->db); } @@ -148,18 +141,13 @@ class ProcessBankTransactionsNordigen implements ShouldQueue //Get transaction count object $transactions = $this->nordigen->getTransactions($this->bank_integration->nordigen_account_id, $this->from_date); - Log::Info($transactions); - - //Get int count - $count = sizeof($transactions); - //if no transactions, update the from_date and move on if (count($transactions) == 0) { $this->bank_integration->from_date = now()->subDays(5); $this->bank_integration->disabled_upstream = false; $this->bank_integration->save(); - $this->stop_loop = false; + return; } @@ -175,7 +163,6 @@ class ProcessBankTransactionsNordigen implements ShouldQueue $now = now(); - foreach ($transactions as $transaction) { if (BankTransaction::where('transaction_id', $transaction['transaction_id'])->where('company_id', $this->company->id)->where('bank_integration_id', $this->bank_integration->id)->withTrashed()->exists()) @@ -194,17 +181,6 @@ class ProcessBankTransactionsNordigen implements ShouldQueue } - - // $this->skip = $this->skip + 500; - - // if ($count < 500) { - // $this->stop_loop = false; - // $this->bank_integration->from_date = now()->subDays(5); - // $this->bank_integration->save(); - - // } - - $this->stop_loop = false; $this->bank_integration->from_date = now()->subDays(5); $this->bank_integration->save();