Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GoCardless integration for ABNAMRO_ABNANL2A #513

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/app-gocardless/bank-factory.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import AbancaCaglesmm from './banks/abanca-caglesmm.js';
import AbnamroAbnanl2a from './banks/abnamro_abnanl2a.js';
import AmericanExpressAesudef1 from './banks/american-express-aesudef1.js';
import BancsabadellBsabesbb from './banks/bancsabadell-bsabesbbb.js';
import BankinterBkbkesmm from './banks/bankinter-bkbkesmm.js';
Expand Down Expand Up @@ -32,6 +33,7 @@ import VirginNrnbgb22 from './banks/virgin_nrnbgb22.js';

export const banks = [
AbancaCaglesmm,
AbnamroAbnanl2a,
AmericanExpressAesudef1,
BancsabadellBsabesbb,
BankinterBkbkesmm,
Expand Down
79 changes: 79 additions & 0 deletions src/app-gocardless/banks/abnamro_abnanl2a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
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: ['ABNAMRO_ABNANL2A'],

accessValidForDays: 180,

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',
};
},

normalizeTransaction(transaction, _booked) {
// There is no remittanceInformationUnstructured, so we'll make it
let remittanceInformationUnstructured =
transaction.remittanceInformationUnstructuredArray.join(' ');
Comment on lines +27 to +28
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Add error handling for missing 'remittanceInformationUnstructuredArray'

If transaction.remittanceInformationUnstructuredArray is undefined, calling .join(' ') will result in a runtime error. Consider providing a default empty array to prevent this issue.

Apply this diff to add a default value:

 let remittanceInformationUnstructured =
-  transaction.remittanceInformationUnstructuredArray.join(' ');
+  (transaction.remittanceInformationUnstructuredArray || []).join(' ');
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
let remittanceInformationUnstructured =
transaction.remittanceInformationUnstructuredArray.join(' ');
let remittanceInformationUnstructured =
(transaction.remittanceInformationUnstructuredArray || []).join(' ');


// Remove clutter to extract the payee from remittanceInformationUnstructured ...
// ... when not otherwise provided.
const matches =
remittanceInformationUnstructured.match(/Betaalpas(.+),PAS/);
const payeeName = matches
? matches[1].replace(/.+\*/, '').trim()
: undefined;
transaction.debtorName = transaction.debtorName || payeeName;
transaction.creditorName = transaction.creditorName || payeeName;

// There are anumber of superfluous keywords in the remittanceInformation.
// Remove them to aboid clutter in notes.
const keywordsToRemove = ['.EA, Betaalpas', ',PAS\\d{3}', 'NR:.+, '];
const regex = new RegExp(keywordsToRemove.join('|'), 'g');
transaction.remittanceInformationUnstructured =
remittanceInformationUnstructured.replace(regex, '').trim();

return {
...transaction,
payeeName: formatPayeeName(transaction),
date: transaction.valueDateTime.slice(0, 10),
};
},

sortTransactions(transactions = []) {
return transactions.sort(
(a, b) => +new Date(b.valueDateTime) - +new Date(a.valueDateTime),
);
},

calculateStartingBalance(sortedTransactions = [], balances = []) {
if (sortedTransactions.length) {
const oldestTransaction =
sortedTransactions[sortedTransactions.length - 1];
const oldestKnownBalance = amountToInteger(
oldestTransaction.balanceAfterTransaction.balanceAmount.amount,
);
const oldestTransactionAmount = amountToInteger(
oldestTransaction.transactionAmount.amount,
);

return oldestKnownBalance - oldestTransactionAmount;
} else {
return amountToInteger(
balances.find((balance) => 'interimBooked' === balance.balanceType)
.balanceAmount.amount,
);
}
Comment on lines +73 to +77
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Handle cases where 'interimBooked' balance is not found

If no balance with balanceType equal to 'interimBooked' is found, balances.find(...) will return undefined, and accessing .balanceAmount.amount will throw an error. Adding error handling ensures robustness.

Apply this diff to add error handling:

 } else {
+  const interimBalance = balances.find(
+    (balance) => 'interimBooked' === balance.balanceType,
+  );
+  if (!interimBalance) {
+    // Handle the case where no interimBooked balance is found
+    throw new Error('No interimBooked balance found');
+  }
   return amountToInteger(
-    balances.find((balance) => 'interimBooked' === balance.balanceType)
-      .balanceAmount.amount,
+    interimBalance.balanceAmount.amount,
   );
 }

Committable suggestion skipped: line range outside the PR's diff.

},
};
33 changes: 33 additions & 0 deletions src/app-gocardless/banks/tests/abnamro_abnanl2a.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import AbnamroAbnanl2a from '../abnamro_abnanl2a.js';

describe('AbnamroAbnanl2a', () => {
describe('#normalizeTransaction', () => {
it('correctly extracts the payee and when not provided', () => {
const transaction = {
transactionId: '0123456789012345',
bookingDate: '2023-12-11',
valueDateTime: '2023-12-09T15:43:37.950',
transactionAmount: {
amount: '-10.00',
currency: 'EUR',
},
remittanceInformationUnstructuredArray: [
'BEA, Betaalpas',
'My Payee Name,PAS123',
'NR:123A4B, 09.12.23/15:43',
'CITY',
],
};

const normalizedTransaction = AbnamroAbnanl2a.normalizeTransaction(
transaction,
false,
);

expect(normalizedTransaction.remittanceInformationUnstructured).toEqual(
'My Payee Name 09.12.23/15:43 CITY',
);
expect(normalizedTransaction.payeeName).toEqual('My Payee Name');
});
});
});
6 changes: 6 additions & 0 deletions upcoming-release-notes/513.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Enhancements
authors: [nsulzer]
---

Add GoCardless integration for ABNAMRO_ABNANL2A
Loading