A local-first personal finance app
Improve Boursobank Gocardless transaction parsing (#5202)
Guillaume Taquet Gasperinid73ead135e71
diff --git a/packages/sync-server/src/app-gocardless/banks/boursobank_bousfrppxxx.js b/packages/sync-server/src/app-gocardless/banks/boursobank_bousfrppxxx.jsindex 3b508d5f19..9d2eb9aa2d 100644--- a/packages/sync-server/src/app-gocardless/banks/boursobank_bousfrppxxx.js+++ b/packages/sync-server/src/app-gocardless/banks/boursobank_bousfrppxxx.js@@ -3,13 +3,15 @@ import { title } from '../../util/title/index.js';import Fallback from './integration-bank.js';const regexCard =- /^CARTE \d{2}\/\d{2}\/\d{2} (?<payeeName>.+?)( CB\*)?( ?\d{4,})?$/;+ /^CARTE (?<date>\d{2}\/\d{2}\/\d{2}) (?<payeeName>.+?)( \d+)?( CB\*\d{4})?$/;const regexAtmWithdrawal =- /^RETRAIT DAB \d{2}\/\d{2}\/\d{2} (?<locationName>.+?) CB\*\d{4,}/;+ /^RETRAIT DAB (?<date>\d{2}\/\d{2}\/\d{2}) (?<locationName>.+?) CB\*\d{4,}/;const regexTransfer = /^VIR /;const regexInstantTransfer = /^VIR INST /;const regexSepa = /^(PRLV|VIR) SEPA /;const regexLoan = /^ECH PRET:/;+const regexCreditNote =+ /^AVOIR (?<date>\d{2}\/\d{2}\/\d{2}) (?<payeeName>.+?) CB\*\d{4,}/;/** @type {import('./bank.interface.js').IBank} */export default {@@ -21,58 +23,67 @@ export default {const editedTrans = { ...transaction };editedTrans.remittanceInformationUnstructuredArray =- // Remove the backslashes that are sometimes present+ // Remove the localisation with backslashes that are sometimes presenttransaction.remittanceInformationUnstructuredArray- .map(line => line.replace(/\\ ?/g, ' '))+ .map(line => line.replace(/\\.+/g, ''))// Remove an unwanted line that pollutes the remittance information.filter(line => line.startsWith('Réf : ') === false);const infoArray = editedTrans.remittanceInformationUnstructuredArray;const firstLine = infoArray[0];- if (firstLine.match(regexCard)) {+ let match;++ /*+ * Some transactions always have their identifier in the first line (e.g. card),+ * while others have it **randomly** in any of the lines (e.g. transfers).+ */++ // Check the first line for specific patterns+ if ((match = firstLine.match(regexCard))) {// Card transaction- const payeeName = firstLine.replace(regexCard, '$1');+ const payeeName = match.groups.payeeName;editedTrans.payeeName = title(payeeName);- } else if (firstLine.match(regexInstantTransfer)) {- // Instant transfer- editedTrans.payeeName = title(- firstLine.replace(regexInstantTransfer, ''),- );- } else if (firstLine.match(regexSepa)) {- // SEPA transfer- editedTrans.payeeName = title(firstLine.replace(regexSepa, ''));- } else if (firstLine.match(regexTransfer) && infoArray.length > 1) {- // Other transfer- // Must be evaluated after the other transfers as they're more specific (here VIR only)- editedTrans.payeeName = title(infoArray[1]);- editedTrans.notes = firstLine.replace(regexTransfer, '');- } else if (firstLine.match(regexLoan)) {+ editedTrans.notes = `Carte ${match.groups.date}`;+ if (infoArray.length > 1) {+ editedTrans.notes += ' ' + infoArray.slice(1).join(' ');+ }+ } else if ((match = firstLine.match(regexLoan))) {// LoaneditedTrans.payeeName = 'Prêt bancaire';editedTrans.notes = firstLine;- } else if (firstLine.match(regexAtmWithdrawal)) {+ } else if ((match = firstLine.match(regexAtmWithdrawal))) {// ATM withdrawaleditedTrans.payeeName = 'Retrait DAB';- editedTrans.notes =- firstLine.match(regexAtmWithdrawal).groups.locationName;+ editedTrans.notes = `Retrait ${match.groups.date} ${match.groups.locationName}`;if (infoArray.length > 1) {- editedTrans.notes += ' ' + infoArray[1];+ editedTrans.notes += ' ' + infoArray.slice(1).join(' ');}+ } else if ((match = firstLine.match(regexCreditNote))) {+ // Credit note (refund)+ editedTrans.payeeName = title(match.groups.payeeName);+ editedTrans.notes = `Avoir ${match.groups.date}`;} else {- editedTrans.payeeName = title(firstLine);- }-- if (editedTrans.notes === undefined) {- // We managed to extract the payee name, but nothing specific in the notes-- if (infoArray.length === 2) {- // If there are only two lines, the second one is the notes- editedTrans.notes = infoArray[1];+ // For the next patterns, we need to check all lines as the identifier can be anywhere+ if ((match = infoArray.find(line => regexInstantTransfer.test(line)))) {+ // Instant transfer+ editedTrans.payeeName = title(match.replace(regexInstantTransfer, ''));+ editedTrans.notes = infoArray.filter(l => l !== match).join(' ');+ } else if ((match = infoArray.find(line => regexSepa.test(line)))) {+ // SEPA transfer+ editedTrans.payeeName = title(match.replace(regexSepa, ''));+ editedTrans.notes = infoArray.filter(l => l !== match).join(' ');+ } else if ((match = infoArray.find(line => regexTransfer.test(line)))) {+ // Other transfer+ // Must be evaluated after the other transfers as they're more specific+ // (here VIR only)+ const infoArrayWithoutLine = infoArray.filter(l => l !== match);+ editedTrans.payeeName = title(infoArrayWithoutLine.join(' '));+ editedTrans.notes = match.replace(regexTransfer, '');} else {- // Unfortunately, the order of the lines is not always the same, which causes issues on multiple syncs- // Thus, do not set the notes if we have more than two lines- editedTrans.notes = '';+ // Unknown transaction type+ editedTrans.payeeName = title(firstLine.replace(/ \d+$/, ''));+ editedTrans.notes = infoArray.slice(1).join(' ');}}diff --git a/packages/sync-server/src/app-gocardless/banks/tests/boursobank_bousfrppxxx.spec.js b/packages/sync-server/src/app-gocardless/banks/tests/boursobank_bousfrppxxx.spec.jsindex e6024ed73a..88a03d08c3 100644--- a/packages/sync-server/src/app-gocardless/banks/tests/boursobank_bousfrppxxx.spec.js+++ b/packages/sync-server/src/app-gocardless/banks/tests/boursobank_bousfrppxxx.spec.js@@ -3,21 +3,26 @@ import BoursoBank from '../boursobank_bousfrppxxx.js';describe('BoursoBank', () => {describe('#normalizeTransaction', () => {it.each([- [['CARTE 01/03/25 PAYEE NAME CB*4567'], 'Payee Name', ''],- [['CARTE 01/03/25 PAYEE NAME'], 'Payee Name', ''],- [['CARTE 01/03/25 PAYEE NAME 7428347'], 'Payee Name', ''],+ [['CARTE 01/03/25 PAYEE NAME CB*4567'], 'Payee Name', 'Carte 01/03/25'],+ [+ ['CARTE 01/03/25 PAYEE NAME 713621 CB*4567'],+ 'Payee Name',+ 'Carte 01/03/25',+ ],+ [['CARTE 01/03/25 PAYEE NAME'], 'Payee Name', 'Carte 01/03/25'],+ [['CARTE 01/03/25 PAYEE NAME 7428347'], 'Payee Name', 'Carte 01/03/25'],[['CARTE 03/02/25 PAYEE NAME CB*1234','2,80 NZD / 1 euro = 1,818181818',],'Payee Name',- '2,80 NZD / 1 euro = 1,818181818',+ 'Carte 03/02/25 2,80 NZD / 1 euro = 1,818181818',],[['RETRAIT DAB 01/03/25 My location CB*9876'],'Retrait DAB',- 'My location',+ 'Retrait 01/03/25 My location',],[[@@ -25,13 +30,18 @@ describe('BoursoBank', () => {'2,80 NZD / 1 euro = 1,818181818',],'Retrait DAB',- 'My location 2,80 NZD / 1 euro = 1,818181818',+ 'Retrait 01/03/25 My location 2,80 NZD / 1 euro = 1,818181818',],[['VIR Text put by the sender', 'PAYEE NAME'],'Payee Name','Text put by the sender',],+ [+ ['PAYEE NAME', 'VIR Text put by the sender'],+ 'Payee Name',+ 'Text put by the sender',+ ],[['VIR Text put by the sender',@@ -50,11 +60,31 @@ describe('BoursoBank', () => {'Payee Name','Text put by the sender',],+ [+ [+ 'Réf : SOME TEXT PUT BY THE BANK',+ 'VIR Text put by the sender',+ 'PAYEE NAME',+ ],+ 'Payee Name',+ 'Text put by the sender',+ ],[['VIR INST PAYEE NAME', 'Text put by the sender'],'Payee Name','Text put by the sender',],+ [+ ['Text put by the sender', 'VIR INST PAYEE NAME'],+ 'Payee Name',+ 'Text put by the sender',+ ],+ [['VIR INST PAYEE NAME'], 'Payee Name', ''],+ [+ ['PAYEE NAME', 'VIR Text put by the sender'],+ 'Payee Name',+ 'Text put by the sender',+ ],[['VIR SEPA PAYEE NAME',@@ -63,7 +93,17 @@ describe('BoursoBank', () => {'YET ANOTHER TEXT',],'Payee Name',- '',+ 'SOME TEXT ANOTHER TEXT YET ANOTHER TEXT',+ ],+ [+ [+ 'SOME TEXT',+ 'ANOTHER TEXT',+ 'VIR SEPA PAYEE NAME',+ 'YET ANOTHER TEXT',+ ],+ 'Payee Name',+ 'SOME TEXT ANOTHER TEXT YET ANOTHER TEXT',],[[@@ -74,15 +114,28 @@ describe('BoursoBank', () => {'PRELEVEMENT FOO BAR BAZ du',],'Payee Name',- '',+ 'HERE IS SOMETHING SOME OTHER TEXT 30/04/2025 PRELEVEMENT FOO BAR BAZ du',+ ],+ [+ [+ '30/05/2025',+ 'SOME.TEXT.123.456',+ 'PRELEVEMENT FOO BAR BAZ du',+ 'PRLV SEPA Payee Name',+ 'ABC 1934821371',+ ],+ 'Payee Name',+ '30/05/2025 SOME.TEXT.123.456 PRELEVEMENT FOO BAR BAZ du ABC 1934821371',],[['ECH PRET:1823918329832913'],'Prêt bancaire','ECH PRET:1823918329832913',],- [['PAYEE NAME'], 'Payee Name', ''],- [['PAYEE\\NAME\\ BIS'], 'Payee Name Bis', ''],+ [['PAYEE NAME 411'], 'Payee Name', ''],+ [['PAYEE NAME\\PARIS\\ FR'], 'Payee Name', ''],+ [['PAYEE NAME 1\\PARIS\\ FR'], 'Payee Name', ''],+ [['AVOIR 17/06/25 PAYEE NAME CB*1234'], 'Payee Name', 'Avoir 17/06/25'],])('normalizes transaction with %s',(diff --git a/upcoming-release-notes/5202.md b/upcoming-release-notes/5202.mdnew file mode 100644index 0000000000..422c81d9f8--- /dev/null+++ b/upcoming-release-notes/5202.md@@ -0,0 +1,6 @@+---+category: Enhancements+authors: [Aerion]+---++Improve Boursobank GoCardless transaction parsing