Browse Source

Add safety catch to both Nordigen and Spectre

pull/424/head
James Cole 2 years ago
parent
commit
1c8f3b8de9
No known key found for this signature in database GPG Key ID: B49A324B7EAD6D80
  1. 10
      app/Services/Nordigen/Conversion/Routine/GenerateTransactions.php
  2. 50
      app/Services/Spectre/Conversion/Routine/GenerateTransactions.php
  3. 116
      app/Support/Internal/DuplicateSafetyCatch.php

10
app/Services/Nordigen/Conversion/Routine/GenerateTransactions.php

@ -35,6 +35,7 @@ use App\Services\Shared\Authentication\SecretManager;
use App\Services\Shared\Configuration\Configuration;
use App\Services\Shared\Conversion\ProgressInformation;
use App\Support\Http\CollectsAccounts;
use App\Support\Internal\DuplicateSafetyCatch;
use Cache;
use GrumpyDictator\FFIIIApiSupport\Exceptions\ApiHttpException;
use GrumpyDictator\FFIIIApiSupport\Model\Account;
@ -50,6 +51,7 @@ class GenerateTransactions
{
use ProgressInformation;
use CollectsAccounts;
use DuplicateSafetyCatch;
private array $accounts;
private Configuration $configuration;
@ -306,6 +308,10 @@ class GenerateTransactions
}
}
$transaction = $this->negativeTransactionSafetyCatch($transaction, (string) $entry->getDestinationName(), (string) $entry->getDestinationIban());
app('log')->debug(sprintf('source_id = %d, destination_id = "%s", destination_name = "%s", destination_iban = "%s"', $transaction['source_id'], $transaction['destination_id'] ?? '', $transaction['destination_name'] ?? '', $transaction['destination_iban'] ?? ''));
return $transaction;
}
@ -359,6 +365,10 @@ class GenerateTransactions
}
}
$transaction = $this->positiveTransactionSafetyCatch($transaction, (string) $entry->getSourceName(), (string) $entry->getSourceIban());
app('log')->debug(sprintf('destination_id = %d, source_name = "%s", source_iban = "%s", source_id = "%s"', $transaction['destination_id'] ?? '', $transaction['source_name'] ?? '', $transaction['source_iban'] ?? '', $transaction['source_id'] ?? ''));
return $transaction;
}

50
app/Services/Spectre/Conversion/Routine/GenerateTransactions.php

@ -28,6 +28,7 @@ use App\Services\Shared\Configuration\Configuration;
use App\Services\Shared\Conversion\ProgressInformation;
use App\Services\Spectre\Model\Transaction;
use App\Support\Http\CollectsAccounts;
use App\Support\Internal\DuplicateSafetyCatch;
use GrumpyDictator\FFIIIApiSupport\Exceptions\ApiHttpException;
/**
@ -37,6 +38,7 @@ class GenerateTransactions
{
use ProgressInformation;
use CollectsAccounts;
use DuplicateSafetyCatch;
private array $accounts;
private Configuration $configuration;
@ -195,30 +197,7 @@ class GenerateTransactions
$transaction['source_id'] = (int)$accountId;
unset($transaction['source_name'], $transaction['source_iban']);
}
// safety catch: if the transaction is a transfer, BUT the source and destination are the same, Firefly III will break.
// The data importer will try to correct this.
if ('transfer' === $transaction['type'] &&
array_key_exists('source_id', $transaction) &&
array_key_exists('destination_id', $transaction) &&
0 !== $transaction['source_id'] &&
0 !== $transaction['destination_id'] &&
$transaction['destination_id'] === $transaction['source_id']) {
app('log')->warning('Transaction is a "transfer", but source and destination are the same. Correcting.');
$transaction['type'] = 'deposit';
// add error message to transaction:
$transaction['notes'] = $transaction['notes'] ?? '';
$transaction['notes'] .= sprintf(" \nThe data importer has ignored the following values in the Salt Edge Spectre transaction data:\n");
$transaction['notes'] .= sprintf("- Original source account name: '%s'\n", $entry->getPayer());
$transaction['notes'] .= sprintf("- Original source account IBAN: '%s'\n", $entry->getPayerIban());
$transaction['notes'] .= "\nTo learn more, please visit: https://bit.ly/FF3-ignored-values";
$transaction['notes'] = trim($transaction['notes']);
unset($transaction['source_id']);
$transaction['source_name'] = '(unknown source account)';
}
$transaction = $this->positiveTransactionSafetyCatch($transaction, $entry->getPayer(), $entry->getPayerIban());
app('log')->debug(sprintf('destination_id = %d, source_name = "%s", source_iban = "%s", source_id = "%s"', $transaction['destination_id'] ?? '', $transaction['source_name'] ?? '', $transaction['source_iban'] ?? '', $transaction['source_id'] ?? ''));
@ -265,28 +244,7 @@ class GenerateTransactions
unset($transaction['destination_name'], $transaction['destination_iban']);
}
// safety catch: if the transaction is a transfer, BUT the source and destination are the same, Firefly III will break.
// The data importer will try to correct this.
if ('transfer' === $transaction['type'] &&
array_key_exists('source_id', $transaction) &&
array_key_exists('destination_id', $transaction) &&
0 !== $transaction['source_id'] &&
0 !== $transaction['destination_id'] &&
$transaction['destination_id'] === $transaction['source_id']) {
app('log')->warning('Transaction is a "transfer", but source and destination are the same. Correcting.');
$transaction['type'] = 'withdrawal';
// add error message to transaction:
$transaction['notes'] = $transaction['notes'] ?? '';
$transaction['notes'] .= sprintf(" \nThe data importer has ignored the following values in the Salt Edge Spectre transaction data:\n");
$transaction['notes'] .= sprintf("- Original destination account name: '%s'\n", $entry->getPayee());
$transaction['notes'] .= sprintf("- Original destination account IBAN: '%s'\n", $entry->getPayeeIban());
$transaction['notes'] .= "\nTo learn more, please visit: https://bit.ly/FF3-ignored-values";
$transaction['notes'] = trim($transaction['notes']);
unset($transaction['destination_id']);
$transaction['destination_name'] = '(unknown destination account)';
}
$transaction = $this->negativeTransactionSafetyCatch($transaction, $entry->getPayee(), $entry->getPayeeIban());
app('log')->debug(sprintf('source_id = %d, destination_id = "%s", destination_name = "%s", destination_iban = "%s"', $transaction['source_id'], $transaction['destination_id'] ?? '', $transaction['destination_name'] ?? '', $transaction['destination_iban'] ?? ''));

116
app/Support/Internal/DuplicateSafetyCatch.php

@ -0,0 +1,116 @@
<?php
/*
* DuplicateSafetyCatch.php
* Copyright (c) 2023 james@firefly-iii.org
*
* This file is part of the Firefly III Data Importer
* (https://github.com/firefly-iii/data-importer).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
namespace App\Support\Internal;
/**
* Trait DuplicateSafetyCatch
*/
trait DuplicateSafetyCatch
{
/**
* @param array $transaction
* @param string $originalName
* @param string $originalIban
*
* @return array
*/
public function positiveTransactionSafetyCatch(array $transaction, string $originalName, string $originalIban): array
{
app('log')->debug('Now in positiveTransactionSafetyCatch');
// safety catch: if the transaction is a transfer, BUT the source and destination are the same, Firefly III will break.
// The data importer will try to correct this.
// check for columns:
if (!array_key_exists('source_id', $transaction) ||
!array_key_exists('destination_id', $transaction) ||
!array_key_exists('type', $transaction)) {
app('log')->debug('positiveTransactionSafetyCatch: missing columns, cannot continue.');
return $transaction;
}
if ('transfer' === $transaction['type'] &&
0 !== $transaction['destination_id'] &&
$transaction['destination_id'] === $transaction['source_id']) {
app('log')->warning('Transaction is a "transfer", but source and destination are the same. Correcting.');
$transaction['type'] = 'deposit';
// add error message to transaction:
$transaction['notes'] = $transaction['notes'] ?? '';
$transaction['notes'] .= " \nThe data importer has ignored the following values in the transaction data:\n";
$transaction['notes'] .= sprintf("- Original source account name: '%s'\n", $originalName);
$transaction['notes'] .= sprintf("- Original source account IBAN: '%s'\n", $originalIban);
$transaction['notes'] .= "\nTo learn more, please visit: https://bit.ly/FF3-ignored-values";
$transaction['notes'] = trim($transaction['notes']);
unset($transaction['source_id']);
$transaction['source_name'] = '(unknown source account)';
return $transaction;
}
app('log')->debug('positiveTransactionSafetyCatch: did not detect a duplicate account.');
return $transaction;
}
/**
* @param array $transaction
* @param string $originalName
* @param string $originalIban
*
* @return array
*/
public function negativeTransactionSafetyCatch(array $transaction, string $originalName, string $originalIban): array
{
app('log')->debug('Now in negativeTransactionSafetyCatch');
// check for columns:
if (!array_key_exists('source_id', $transaction) ||
!array_key_exists('destination_id', $transaction) ||
!array_key_exists('type', $transaction)) {
app('log')->debug('negativeTransactionSafetyCatch: missing columns, cannot continue.');
return $transaction;
}
// safety catch: if the transaction is a transfer, BUT the source and destination are the same, Firefly III will break.
// The data importer will try to correct this.
if ('transfer' === $transaction['type'] &&
0 !== $transaction['destination_id'] &&
$transaction['destination_id'] === $transaction['source_id']) {
app('log')->warning('Transaction is a "transfer", but source and destination are the same. Correcting.');
$transaction['type'] = 'withdrawal';
// add error message to transaction:
$transaction['notes'] = $transaction['notes'] ?? '';
$transaction['notes'] .= " \nThe data importer has ignored the following values in the transaction data:\n";
$transaction['notes'] .= sprintf("- Original destination account name: '%s'\n", $originalName);
$transaction['notes'] .= sprintf("- Original destination account IBAN: '%s'\n", $originalIban);
$transaction['notes'] .= "\nTo learn more, please visit: https://bit.ly/FF3-ignored-values";
$transaction['notes'] = trim($transaction['notes']);
unset($transaction['destination_id']);
$transaction['destination_name'] = '(unknown destination account)';
}
app('log')->debug('negativeTransactionSafetyCatch: did not detect a duplicate account.');
return $transaction;
}
}
Loading…
Cancel
Save