A local-first personal finance app
Fix pending transaction amount sign and payee name for National Bank of Greece import (#448)
mezger6f78383be29b4
diff --git a/src/app-gocardless/bank-factory.js b/src/app-gocardless/bank-factory.jsindex 3b36193c08..0f6e7fa085 100644--- a/src/app-gocardless/bank-factory.js+++ b/src/app-gocardless/bank-factory.js@@ -23,6 +23,7 @@ import SparNordSpNoDK22 from './banks/sparnord-spnodk22.js';import SpkKarlsruhekarsde66 from './banks/spk-karlsruhe-karsde66.js';import SpkMarburgBiedenkopfHeladef1mar from './banks/spk-marburg-biedenkopf-heladef1mar.js';import VirginNrnbgb22 from './banks/virgin_nrnbgb22.js';+import NbgEthngraaxxx from './banks/nbg_ethngraaxxx.js';export const banks = [AbancaCaglesmm,@@ -49,6 +50,7 @@ export const banks = [SpkKarlsruhekarsde66,SpkMarburgBiedenkopfHeladef1mar,VirginNrnbgb22,+ NbgEthngraaxxx,];export default (institutionId) =>diff --git a/src/app-gocardless/banks/nbg_ethngraaxxx.js b/src/app-gocardless/banks/nbg_ethngraaxxx.jsnew file mode 100644index 0000000000..a03c1663e9--- /dev/null+++ b/src/app-gocardless/banks/nbg_ethngraaxxx.js@@ -0,0 +1,71 @@+import Fallback from './integration-bank.js';++import { printIban, amountToInteger } from '../utils.js';+import { formatPayeeName } from '../../util/payee-name.js';++/** @type {import('./bank.interface.js').IBank} */+export default {+ ...Fallback,++ institutionIds: ['NBG_ETHNGRAAXXX'],++ accessValidForDays: 90,++ normalizeAccount(account) {+ return {+ account_id: account.id,+ institution: account.institution,+ mask: account.iban.slice(-4),+ iban: account.iban,+ name: [account.name, printIban(account)].join(' '),+ official_name: account.product,+ type: 'checking',+ };+ },++ /**+ * Fixes for the pending transactions:+ * - Corrects amount to negative (nbg erroneously omits the minus sign in pending transactions)+ * - Removes prefix 'ΑΓΟΡΑ' from remittance information to align with the booked transaction (necessary for fuzzy matching to work)+ */+ normalizeTransaction(transaction, _booked) {+ if (+ !transaction.transactionId &&+ transaction.remittanceInformationUnstructured.startsWith('ΑΓΟΡΑ ')+ ) {+ transaction = {+ ...transaction,+ transactionAmount: {+ amount: '-' + transaction.transactionAmount.amount,+ currency: transaction.transactionAmount.currency,+ },+ remittanceInformationUnstructured:+ transaction.remittanceInformationUnstructured.substring(6),+ };+ }++ return {+ ...transaction,+ payeeName: formatPayeeName(transaction),+ date: transaction.bookingDate || transaction.valueDate,+ };+ },++ /**+ * For NBG_ETHNGRAAXXX we don't know what balance was+ * after each transaction so we have to calculate it by getting+ * current balance from the account and subtract all the transactions+ *+ * As a current balance we use `interimBooked` balance type because+ * it includes transaction placed during current day+ */+ calculateStartingBalance(sortedTransactions = [], balances = []) {+ const currentBalance = balances.find(+ (balance) => 'interimAvailable' === balance.balanceType,+ );++ return sortedTransactions.reduce((total, trans) => {+ return total - amountToInteger(trans.transactionAmount.amount);+ }, amountToInteger(currentBalance.balanceAmount.amount));+ },+};diff --git a/src/app-gocardless/banks/tests/nbg_ethngraaxxx.spec.js b/src/app-gocardless/banks/tests/nbg_ethngraaxxx.spec.jsnew file mode 100644index 0000000000..990c6cd5e2--- /dev/null+++ b/src/app-gocardless/banks/tests/nbg_ethngraaxxx.spec.js@@ -0,0 +1,48 @@+import NbgEthngraaxxx from '../nbg_ethngraaxxx.js';++describe('NbgEthngraaxxx', () => {+ describe('#normalizeTransaction', () => {+ it('provides correct amount in pending transaction and removes payee prefix', () => {+ const transaction = {+ bookingDate: '2024-09-03',+ date: '2024-09-03',+ remittanceInformationUnstructured: 'ΑΓΟΡΑ testingson',+ transactionAmount: {+ amount: '100.00',+ currency: 'EUR',+ },+ valueDate: '2024-09-03',+ };++ const normalizedTransaction = NbgEthngraaxxx.normalizeTransaction(+ transaction,+ false,+ );++ expect(normalizedTransaction.transactionAmount.amount).toEqual('-100.00');+ expect(normalizedTransaction.payeeName).toEqual('Testingson');+ });+ });++ it('provides correct amount and payee in booked transaction', () => {+ const transaction = {+ transactionId: 'O244015L68IK',+ bookingDate: '2024-09-03',+ date: '2024-09-03',+ remittanceInformationUnstructured: 'testingson',+ transactionAmount: {+ amount: '-100.00',+ currency: 'EUR',+ },+ valueDate: '2024-09-03',+ };++ const normalizedTransaction = NbgEthngraaxxx.normalizeTransaction(+ transaction,+ true,+ );++ expect(normalizedTransaction.transactionAmount.amount).toEqual('-100.00');+ expect(normalizedTransaction.payeeName).toEqual('Testingson');+ });+});diff --git a/upcoming-release-notes/448.md b/upcoming-release-notes/448.mdnew file mode 100644index 0000000000..8df7eb58e7--- /dev/null+++ b/upcoming-release-notes/448.md@@ -0,0 +1,6 @@+---+category: Bugfix+authors: [mezger6]+---++Fix pending purchases amount sign and payee name for National Bank of Greece import\ No newline at end of file