From 62c6a8775c20ab6a9cd6d0b59f6c3a92e58b89b3 Mon Sep 17 00:00:00 2001 From: Matt Fiddaman Date: Sun, 16 Jun 2024 14:38:49 +0100 Subject: [PATCH] Add bank handler for `VIRGIN_NRNBGB22` (Virgin Money) (#360) --- src/app-gocardless/bank-factory.js | 2 + .../banks/tests/virgin_nrnbgb22.spec.js | 65 +++++++++++++++++++ src/app-gocardless/banks/virgin_nrnbgb22.js | 39 +++++++++++ upcoming-release-notes/360.md | 6 ++ 4 files changed, 112 insertions(+) create mode 100644 src/app-gocardless/banks/tests/virgin_nrnbgb22.spec.js create mode 100644 src/app-gocardless/banks/virgin_nrnbgb22.js create mode 100644 upcoming-release-notes/360.md diff --git a/src/app-gocardless/bank-factory.js b/src/app-gocardless/bank-factory.js index c1665d64a..1d5d33c70 100644 --- a/src/app-gocardless/bank-factory.js +++ b/src/app-gocardless/bank-factory.js @@ -15,6 +15,7 @@ import SandboxfinanceSfin0000 from './banks/sandboxfinance-sfin0000.js'; import SparNordSpNoDK22 from './banks/sparnord-spnodk22.js'; import SpkMarburgBiedenkopfHeladef1mar from './banks/spk-marburg-biedenkopf-heladef1mar.js'; import SpkKarlsruhekarsde66 from './banks/spk-karlsruhe-karsde66.js'; +import VirginNrnbgb22 from './banks/virgin_nrnbgb22.js'; export const banks = [ AbancaCaglesmm, @@ -33,6 +34,7 @@ export const banks = [ SparNordSpNoDK22, SpkMarburgBiedenkopfHeladef1mar, SpkKarlsruhekarsde66, + VirginNrnbgb22, ]; export default (institutionId) => diff --git a/src/app-gocardless/banks/tests/virgin_nrnbgb22.spec.js b/src/app-gocardless/banks/tests/virgin_nrnbgb22.spec.js new file mode 100644 index 000000000..c6cb6b26c --- /dev/null +++ b/src/app-gocardless/banks/tests/virgin_nrnbgb22.spec.js @@ -0,0 +1,65 @@ +import Virgin from '../virgin_nrnbgb22.js'; +import { mockTransactionAmount } from '../../services/tests/fixtures.js'; + +describe('Virgin', () => { + describe('#normalizeTransaction', () => { + it('does not alter simple payee information', () => { + const transaction = { + bookingDate: '2024-01-01T00:00:00Z', + remittanceInformationUnstructured: 'DIRECT DEBIT PAYMENT', + transactionAmount: mockTransactionAmount, + }; + + const normalizedTransaction = Virgin.normalizeTransaction( + transaction, + true, + ); + + expect(normalizedTransaction.creditorName).toEqual( + 'DIRECT DEBIT PAYMENT', + ); + expect(normalizedTransaction.debtorName).toEqual('DIRECT DEBIT PAYMENT'); + expect(normalizedTransaction.remittanceInformationUnstructured).toEqual( + 'DIRECT DEBIT PAYMENT', + ); + }); + + it('formats bank transfer payee and references', () => { + const transaction = { + bookingDate: '2024-01-01T00:00:00Z', + remittanceInformationUnstructured: 'FPS, Joe Bloggs, Food', + transactionAmount: mockTransactionAmount, + }; + + const normalizedTransaction = Virgin.normalizeTransaction( + transaction, + true, + ); + + expect(normalizedTransaction.creditorName).toEqual('Joe Bloggs'); + expect(normalizedTransaction.debtorName).toEqual('Joe Bloggs'); + expect(normalizedTransaction.remittanceInformationUnstructured).toEqual( + 'Food', + ); + }); + + it('removes method information from payee name', () => { + const transaction = { + bookingDate: '2024-01-01T00:00:00Z', + remittanceInformationUnstructured: 'Card 99, Tesco Express', + transactionAmount: mockTransactionAmount, + }; + + const normalizedTransaction = Virgin.normalizeTransaction( + transaction, + true, + ); + + expect(normalizedTransaction.creditorName).toEqual('Tesco Express'); + expect(normalizedTransaction.debtorName).toEqual('Tesco Express'); + expect(normalizedTransaction.remittanceInformationUnstructured).toEqual( + 'Card 99, Tesco Express', + ); + }); + }); +}); diff --git a/src/app-gocardless/banks/virgin_nrnbgb22.js b/src/app-gocardless/banks/virgin_nrnbgb22.js new file mode 100644 index 000000000..7340cdeb1 --- /dev/null +++ b/src/app-gocardless/banks/virgin_nrnbgb22.js @@ -0,0 +1,39 @@ +import Fallback from './integration-bank.js'; + +/** @type {import('./bank.interface.js').IBank} */ +export default { + ...Fallback, + + institutionIds: ['VIRGIN_NRNBGB22'], + + accessValidForDays: 90, + + normalizeTransaction(transaction, booked) { + const transferPrefixes = ['MOB', 'FPS']; + const methodRegex = /^(Card|WLT)\s\d+/; + + const parts = transaction.remittanceInformationUnstructured.split(', '); + + if (transferPrefixes.includes(parts[0])) { + // Transfer remittance information begins with either "MOB" or "FPS" + // the second field contains the payee and the third contains the + // reference + + transaction.creditorName = parts[1]; + transaction.debtorName = parts[1]; + transaction.remittanceInformationUnstructured = parts[2]; + } else if (parts[0].match(methodRegex)) { + // The payee is prefixed with the payment method, eg "Card 11, {payee}" + + transaction.creditorName = parts[1]; + transaction.debtorName = parts[1]; + } else { + // Simple payee name + + transaction.creditorName = transaction.remittanceInformationUnstructured; + transaction.debtorName = transaction.remittanceInformationUnstructured; + } + + return Fallback.normalizeTransaction(transaction, booked); + }, +}; diff --git a/upcoming-release-notes/360.md b/upcoming-release-notes/360.md new file mode 100644 index 000000000..1a4c2429c --- /dev/null +++ b/upcoming-release-notes/360.md @@ -0,0 +1,6 @@ +--- +category: Enhancements +authors: [matt-fidd] +--- + +Add bank handler for VIRGIN_NRNBGB22 (Virgin Money) for more accurate payees