account = $account; $this->bank_integration = $bank_integration; $this->from_date = $bank_integration->from_date; $this->company = $this->bank_integration->company; if ($this->bank_integration->integration_type != BankIntegration::INTEGRATION_TYPE_NORDIGEN) throw new \Exception("Invalid BankIntegration Type"); $this->nordigen = new Nordigen($this->account->bank_integration_nordigen_secret_id, $this->account->bank_integration_nordigen_secret_key); } /** * Execute the job. * * * @return void */ public function handle() { 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(); } catch (\Exception $e) { nlog("{$this->account->bank_integration_nordigen_secret_id} - exited abnormally => " . $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(); return; } if (!$this->nordigen_account) return; // UPDATE TRANSACTIONS do { try { $this->processTransactions(); } catch (\Exception $e) { nlog("{$this->account->bank_integration_nordigen_secret_id} - exited abnormally => " . $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(); return; } } while ($this->stop_loop); BankMatchingService::dispatch($this->company->id, $this->company->db); } private function updateAccount() { $bank_account_id = explode(',', $this->bank_integration->nordigen_meta)[0]; // maybe replace it later with bank_account_id if (!$this->nordigen->isAccountActive($bank_account_id)) { $this->bank_integration->disabled_upstream = true; $this->bank_integration->save(); $this->stop_loop = false; // @turbo124 @todo send email for expired account return; } $this->nordigen_account = $this->nordigen->getAccount($bank_account_id); $this->bank_integration->bank_account_status = $this->nordigen_account['account_status']; $this->bank_integration->balance = $this->nordigen_account['current_balance']; $this->bank_integration->currency = $this->nordigen_account['account_currency']; $this->bank_integration->save(); } private function processTransactions() { //Get transaction count object $transactions = $this->nordigen->getTransactions($this->nordigen_account["id"], $this->from_date); //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; } //Harvest the company MultiDB::setDb($this->company->db); /*Get the user */ $user_id = $this->company->owner()->id; /* Unguard the model to perform batch inserts */ BankTransaction::unguard(); $now = now(); foreach ($transactions as $transaction) { if (BankTransaction::where('transaction_id', $transaction['transaction_id'])->where('company_id', $this->company->id)->withTrashed()->exists()) continue; //this should be much faster to insert than using ::create() \DB::table('bank_transactions')->insert( array_merge($transaction, [ 'company_id' => $this->company->id, 'user_id' => $user_id, 'bank_integration_id' => $this->bank_integration->id, 'created_at' => $now, 'updated_at' => $now, ]) ); } // $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(); } }