From 41b12151f6ef7db9c378a6c7e7109c0f95ab56ce Mon Sep 17 00:00:00 2001 From: David Martos Date: Sun, 8 Sep 2024 20:45:17 +0200 Subject: [PATCH] Fix BancSabadell payee name based on the transaction amount (#445) * Fix BancSabadell creditor and debtor names * release notes --- src/app-gocardless/bank-factory.js | 2 + .../banks/bancsabadell-bsabesbbb.js | 36 +++++++++++++++++ .../tests/bancsabadell-bsabesbbb.spec.js | 39 +++++++++++++++++++ upcoming-release-notes/445.md | 6 +++ 4 files changed, 83 insertions(+) create mode 100644 src/app-gocardless/banks/bancsabadell-bsabesbbb.js create mode 100644 src/app-gocardless/banks/tests/bancsabadell-bsabesbbb.spec.js create mode 100644 upcoming-release-notes/445.md diff --git a/src/app-gocardless/bank-factory.js b/src/app-gocardless/bank-factory.js index 0c4fcc855..4339e6d2e 100644 --- a/src/app-gocardless/bank-factory.js +++ b/src/app-gocardless/bank-factory.js @@ -1,5 +1,6 @@ import AbancaCaglesmm from './banks/abanca-caglesmm.js'; import AmericanExpressAesudef1 from './banks/american-express-aesudef1.js'; +import BancsabadellBsabesbb from './banks/bancsabadell-bsabesbbb.js'; import BankinterBkbkesmm from './banks/bankinter-bkbkesmm.js'; import Belfius from './banks/belfius_gkccbebb.js'; import Berliner_Sparkasse_beladebexxx from './banks/berliner_sparkasse_beladebexxx.js'; @@ -26,6 +27,7 @@ import VirginNrnbgb22 from './banks/virgin_nrnbgb22.js'; export const banks = [ AbancaCaglesmm, AmericanExpressAesudef1, + BancsabadellBsabesbb, BankinterBkbkesmm, Belfius, Berliner_Sparkasse_beladebexxx, diff --git a/src/app-gocardless/banks/bancsabadell-bsabesbbb.js b/src/app-gocardless/banks/bancsabadell-bsabesbbb.js new file mode 100644 index 000000000..52802ebb4 --- /dev/null +++ b/src/app-gocardless/banks/bancsabadell-bsabesbbb.js @@ -0,0 +1,36 @@ +import Fallback from './integration-bank.js'; + +import { formatPayeeName } from '../../util/payee-name.js'; + +/** @type {import('./bank.interface.js').IBank} */ +export default { + ...Fallback, + + institutionIds: ['BANCSABADELL_BSABESBB'], + + accessValidForDays: 180, + + // Sabadell transactions don't get the creditorName/debtorName properly + normalizeTransaction(transaction, _booked) { + const amount = transaction.transactionAmount.amount; + + // The amount is negative for outgoing transactions, positive for incoming transactions. + const isCreditorPayee = Number.parseFloat(amount) < 0; + + const payeeName = transaction.remittanceInformationUnstructuredArray + .join(' ') + .trim(); + + // The payee name is the creditor name for outgoing transactions and the debtor name for incoming transactions. + const creditorName = isCreditorPayee ? payeeName : null; + const debtorName = isCreditorPayee ? null : payeeName; + + transaction.creditorName = creditorName; + transaction.debtorName = debtorName; + + return { + ...transaction, + payeeName: formatPayeeName(transaction), + }; + }, +}; diff --git a/src/app-gocardless/banks/tests/bancsabadell-bsabesbbb.spec.js b/src/app-gocardless/banks/tests/bancsabadell-bsabesbbb.spec.js new file mode 100644 index 000000000..e33be8524 --- /dev/null +++ b/src/app-gocardless/banks/tests/bancsabadell-bsabesbbb.spec.js @@ -0,0 +1,39 @@ +import Sabadell from '../bancsabadell-bsabesbbb.js'; + +describe('BancSabadell', () => { + describe('#normalizeTransaction', () => { + describe('returns the creditorName and debtorName from remittanceInformationUnstructuredArray', () => { + it('debtor role - amount < 0', () => { + const transaction = { + transactionAmount: { amount: '-100', currency: 'EUR' }, + remittanceInformationUnstructuredArray: ['some-creditor-name'], + internalTransactionId: 'd7dca139cf31d9', + transactionId: '04704109322', + }; + const normalizedTransaction = Sabadell.normalizeTransaction( + transaction, + true, + ); + expect(normalizedTransaction.creditorName).toEqual( + 'some-creditor-name', + ); + expect(normalizedTransaction.debtorName).toEqual(null); + }); + + it('creditor role - amount > 0', () => { + const transaction = { + transactionAmount: { amount: '100', currency: 'EUR' }, + remittanceInformationUnstructuredArray: ['some-debtor-name'], + internalTransactionId: 'd7dca139cf31d9', + transactionId: '04704109322', + }; + const normalizedTransaction = Sabadell.normalizeTransaction( + transaction, + true, + ); + expect(normalizedTransaction.debtorName).toEqual('some-debtor-name'); + expect(normalizedTransaction.creditorName).toEqual(null); + }); + }); + }); +}); diff --git a/upcoming-release-notes/445.md b/upcoming-release-notes/445.md new file mode 100644 index 000000000..2cab07784 --- /dev/null +++ b/upcoming-release-notes/445.md @@ -0,0 +1,6 @@ +--- +category: Bugfix +authors: [davidmartos96] +--- + +Fix BancSabadell payee name based on the transaction amount