fixes for sync transactions

This commit is contained in:
paulwer 2023-12-13 16:18:18 +01:00
parent 1d0506a832
commit e12fb1f765
4 changed files with 33 additions and 14 deletions

View File

@ -21,6 +21,7 @@ namespace App\Helpers\Bank\Nordigen;
use App\Helpers\Bank\Nordigen\Transformer\AccountTransformer;
use App\Helpers\Bank\Nordigen\Transformer\TransactionTransformer;
use Log;
class Nordigen
{
@ -111,6 +112,8 @@ class Nordigen
{
$transactionResponse = $this->client->account($accountId)->getAccountTransactions($dateFrom);
Log::info($transactionResponse);
$it = new TransactionTransformer();
return $it->transform($transactionResponse);
}

View File

@ -86,17 +86,28 @@ class TransactionTransformer implements BankRevenueInterface
if (!array_key_exists('transactionId', $transaction) || !array_key_exists('transactionAmount', $transaction))
throw new \Exception('invalid dataset');
// description could be in varios places
$description = '';
if (array_key_exists('bank_remittanceInformationStructured', $transaction))
$description = $transaction["bank_remittanceInformationStructured"];
else if (array_key_exists('bank_remittanceInformationStructuredArray', $transaction))
$description = implode($transaction["bank_remittanceInformationStructured"], '\r\n');
else if (array_key_exists('remittanceInformationUnstructured', $transaction))
$description = $transaction["remittanceInformationUnstructured"];
else
Log::warning("Missing description for the following transaction: " . json_encode($transaction));
return [
'transaction_id' => 'nordigen:' . $transaction["transactionId"],
'transaction_id' => $transaction["transactionId"],
'amount' => abs((int) $transaction["transactionAmount"]["amount"]),
'currency_id' => $this->convertCurrency($transaction["transactionAmount"]["currency"]),
'category_id' => 0, // TODO: institution specific keys like: GUTSCHRIFT, ABSCHLUSS, MONATSABSCHLUSS etc
'category_type' => $transaction["additionalInformation"], // TODO: institution specific keys like: GUTSCHRIFT, ABSCHLUSS, MONATSABSCHLUSS etc
'category_type' => array_key_exists('additionalInformation', $transaction) ? $transaction["additionalInformation"] : '', // TODO: institution specific keys like: GUTSCHRIFT, ABSCHLUSS, MONATSABSCHLUSS etc
'date' => $transaction["bookingDate"],
'description' => array_key_exists('bank_remittanceInformationStructured', $transaction) ? $transaction["bank_remittanceInformationStructured"] : (array_key_exists('bank_remittanceInformationStructuredArray', $transaction) ? implode($transaction["bank_remittanceInformationStructured"], '\r\n') : ''),
'description' => $description,
// 'description' => `IBAN: ${elem . json["bank_debtorAccount"] && elem . json["bank_debtorAccount"]["iban"] ? elem . json["bank_debtorAccount"]["iban"] : ' -'}\nVerwendungszweck: ${elem . json["bank_remittanceInformationStructured"] || ' -'}\nName: ${elem . json["bank_debtorName"] || ' -'}`, // 2 fields to get data from (structured and structuredArray (have to be joined))
// TODO: debitor name & iban & bic
'base_type' => (int) $transaction["transactionAmount"]["amount"] > 0 ? 'DEBIT' : 'CREDIT',
'base_type' => (int) $transaction["transactionAmount"]["amount"] <= 0 ? 'DEBIT' : 'CREDIT',
];
}

View File

@ -35,6 +35,7 @@ use App\Utils\Traits\MakesHash;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Cache;
use Log;
class BankIntegrationController extends BaseController
{
@ -199,13 +200,13 @@ class BankIntegrationController extends BaseController
$user_account = $user->account;
// if (Cache::get("throttle_polling:{$user_account->key}")) // @todo uncomment for PR
// return response()->json(BankIntegration::query()->company(), 200);
$this->refreshAccountsYodlee($user);
$this->refreshAccountsNordigen($user);
if (Cache::get("throttle_polling:{$user_account->key}"))
return response()->json(BankIntegration::query()->company(), 200);
// Processing transactions for each bank account
if (Ninja::isHosted() && $user->account->bank_integration_yodlee_account_id)
$user_account->bank_integrations->where("integration_type", BankIntegration::INTEGRATION_TYPE_YODLEE)->where('auto_sync', true)->each(function ($bank_integration) use ($user_account) {
@ -346,6 +347,8 @@ class BankIntegrationController extends BaseController
if (config("ninja.nordigen.secret_id") && config("ninja.nordigen.secret_key") && (Ninja::isSelfHost() || (Ninja::isHosted() && $account->isPaid() && $account->plan == 'enterprise'))) {
$account->bank_integrations()->where('integration_type', BankIntegration::INTEGRATION_TYPE_NORDIGEN)->where('auto_sync', true)->cursor()->each(function ($bank_integration) {
Log::info($bank_integration);
(new ProcessBankTransactionsNordigen($bank_integration))->handle();
});
}

View File

@ -31,8 +31,6 @@ class ProcessBankTransactionsNordigen implements ShouldQueue
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private BankIntegration $bank_integration;
private string $secret_id;
private string $secret_key;
private ?string $from_date;
@ -80,7 +78,7 @@ class ProcessBankTransactionsNordigen implements ShouldQueue
try {
$this->updateAccount();
} catch (\Exception $e) {
nlog("{$this->secret_id} - 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",
@ -89,7 +87,8 @@ class ProcessBankTransactionsNordigen implements ShouldQueue
];
$this->bank_integration->company->notification(new GenericNinjaAdminNotification($content))->ninja();
return;
throw $e;
}
if (!$this->nordigen_account)
return;
@ -100,7 +99,7 @@ class ProcessBankTransactionsNordigen implements ShouldQueue
try {
$this->processTransactions();
} catch (\Exception $e) {
nlog("{$this->secret_id} - 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",
@ -109,7 +108,8 @@ class ProcessBankTransactionsNordigen implements ShouldQueue
];
$this->bank_integration->company->notification(new GenericNinjaAdminNotification($content))->ninja();
return;
throw $e;
}
}
@ -122,10 +122,12 @@ class ProcessBankTransactionsNordigen implements ShouldQueue
private function updateAccount()
{
Log::info("try to execute updateAccount");
if (!$this->nordigen->isAccountActive($this->bank_integration->nordigen_account_id)) {
$this->bank_integration->disabled_upstream = true;
$this->bank_integration->save();
$this->stop_loop = false;
Log::info("account inactive");
// @turbo124 @todo send email for expired account
return;
}
@ -176,7 +178,7 @@ class ProcessBankTransactionsNordigen implements ShouldQueue
foreach ($transactions as $transaction) {
if (BankTransaction::where('transaction_id', $transaction['transaction_id'])->where('company_id', $this->company->id)->withTrashed()->exists())
if (BankTransaction::where('transaction_id', $transaction['transaction_id'])->where('company_id', $this->company->id)->where('bank_integration_id', $this->bank_integration->id)->withTrashed()->exists())
continue;
//this should be much faster to insert than using ::create()