Skip to content

Commit

Permalink
Extract creditor name from remittance information for SWEDBANK_HABALV22
Browse files Browse the repository at this point in the history
Pending transactions are missing the creditor name this fills it out
form the remittanceInformationUnstructured field
  • Loading branch information
dmednis committed Nov 8, 2024
1 parent d83994a commit 5766c36
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 15 deletions.
39 changes: 29 additions & 10 deletions src/app-gocardless/banks/swedbank-habalv22.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,36 @@ export default {
* The actual transaction date for card transactions is only available in the remittanceInformationUnstructured field when the transaction is booked.
*/
normalizeTransaction(transaction, booked) {
const dateMatch = transaction.remittanceInformationUnstructured?.match(
/PIRKUMS [\d*]+ (\d{2}\.\d{2}\.\d{4})/,
);

if (dateMatch) {
const extractedDate = d.parse(dateMatch[1], 'dd.MM.yyyy', new Date());

return Fallback.normalizeTransaction(
{ ...transaction, bookingDate: d.format(extractedDate, 'yyyy-MM-dd') },
booked,
const isCardTransaction =
transaction.remittanceInformationUnstructured?.startsWith('PIRKUMS');

if (isCardTransaction) {
if (!booked && !transaction.creditorName) {
const creditorNameMatch =
transaction.remittanceInformationUnstructured?.match(
/PIRKUMS [\d*]+ \d{2}\.\d{2}\.\d{2} \d{2}:\d{2} [\d.]+ \w{3} \(\d+\) (.+)/,
);

if (creditorNameMatch) {
transaction = {
...transaction,
creditorName: creditorNameMatch[1],
};
}
}

const dateMatch = transaction.remittanceInformationUnstructured?.match(
/PIRKUMS [\d*]+ (\d{2}\.\d{2}\.\d{4})/,
);

if (dateMatch) {
const extractedDate = d.parse(dateMatch[1], 'dd.MM.yyyy', new Date());

transaction = {
...transaction,
bookingDate: d.format(extractedDate, 'yyyy-MM-dd'),
};
}
}

return Fallback.normalizeTransaction(transaction, booked);
Expand Down
29 changes: 24 additions & 5 deletions src/app-gocardless/banks/tests/swedbank-habalv22.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import SwedbankHabaLV22 from '../swedbank-habalv22.js';

describe('#normalizeTransaction', () => {
const cardTransaction = {
const bookedCardTransaction = {
transactionId: '2024102900000000-1',
bookingDate: '2024-10-29',
valueDate: '2024-10-29',
Expand All @@ -15,14 +15,15 @@ describe('#normalizeTransaction', () => {
bankTransactionCode: 'PMNT-CCRD-POSD',
internalTransactionId: 'fa000f86afb2cc7678bcff0000000000',
};

it('extracts card transaction date', () => {
expect(
SwedbankHabaLV22.normalizeTransaction(cardTransaction, true).bookingDate,
SwedbankHabaLV22.normalizeTransaction(bookedCardTransaction, true)
.bookingDate,
).toEqual('2024-10-28');

expect(
SwedbankHabaLV22.normalizeTransaction(cardTransaction, true).date,
SwedbankHabaLV22.normalizeTransaction(bookedCardTransaction, true).date,
).toEqual('2024-10-28');
});

Expand All @@ -32,12 +33,30 @@ describe('#normalizeTransaction', () => {
['null value', null],
])('normalizes non-card transaction with %s', (_, remittanceInfo) => {
const transaction = {
...cardTransaction,
...bookedCardTransaction,
remittanceInformationUnstructured: remittanceInfo,
};
const normalized = SwedbankHabaLV22.normalizeTransaction(transaction, true);

expect(normalized.bookingDate).toEqual('2024-10-29');
expect(normalized.date).toEqual('2024-10-29');
});

const pendingCardTransaction = {
transactionId: '2024102900000000-1',
valueDate: '2024-10-29',
transactionAmount: {
amount: '-22.99',
currency: 'EUR',
},
remittanceInformationUnstructured:
'PIRKUMS 424242******4242 28.10.24 13:37 22.99 EUR (111111) SOME CREDITOR NAME',
};

it('extracts pending card transaction creditor name', () => {
expect(
SwedbankHabaLV22.normalizeTransaction(pendingCardTransaction, false)
.creditorName,
).toEqual('SOME CREDITOR NAME');
});
});

0 comments on commit 5766c36

Please sign in to comment.