Skip to content

Commit

Permalink
handle transactions that contain both creditor and debtor names, pref…
Browse files Browse the repository at this point in the history
…erring the correct field first
  • Loading branch information
matt-fidd committed May 15, 2024
1 parent 819f7ce commit 87d6b89
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/app-gocardless/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,35 @@ export const amountToInteger = (n) => Math.round(n * 100);
export const formatPayeeName = (trans) => {
const nameParts = [];

const name =
// get the correct name and account fields for the transaction amount
let name;
let account;
if (trans.amount > 0 || Object.is(Number(trans.amount), 0)) {
name = trans.debtorName;
account = trans.debtorAccount;
} else {
name = trans.creditorName;
account = trans.creditorAccount;
}

// use the correct fields for the amount if it was found
// if not, use whatever we can find
name =
name ||
trans.debtorName ||
trans.creditorName ||
trans.remittanceInformationUnstructured ||
(trans.remittanceInformationUnstructuredArray || []).join(', ') ||
trans.additionalInformation;

account = account || trans.debtorAccount || trans.creditorAccount;

if (name) {
nameParts.push(title(name));
}

if (trans.debtorAccount && trans.debtorAccount.iban) {
nameParts.push(formatPayeeIban(trans.debtorAccount.iban));
} else if (trans.creditorAccount && trans.creditorAccount.iban) {
nameParts.push(formatPayeeIban(trans.creditorAccount.iban));
if (account && account.iban) {
nameParts.push(formatPayeeIban(account.iban));
}

return nameParts.join(' ');
Expand Down

0 comments on commit 87d6b89

Please sign in to comment.