Skip to content

Commit

Permalink
feat(nordigen): payee from AdditionalInformation
Browse files Browse the repository at this point in the history
Support getting the payee from AdditionalInformation if its present in
the Nordigen transaction and no creditor/debtor nor remittance
information is available. Which is the case for SEB_KORT_AB_NO_SKHSFI21
for example.

Fixes #10
  • Loading branch information
martinohansen committed Feb 26, 2023
1 parent ea43b55 commit f5676a5
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 13 deletions.
14 changes: 7 additions & 7 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ type Nordigen struct {
// Use named datafile(relative path in datadir, absolute if starts with slash) instead of default (ynabber-NORDIGEN_BANKID.json)
Datafile string `envconfig:"NORDIGEN_DATAFILE"`

// PayeeSource is a list of sources for Payee candidates, the first
// method that yields a result will be used. Valid options are:
// unstructured and name.
// PayeeSource is a list of sources for Payee candidates, the first method
// that yields a result will be used. Valid options are: unstructured, name
// and additional.
//
// Option unstructured equals to the `RemittanceInformationUnstructured`
// filed from Nordigen while name equals either `debtorName` or
// `creditorName`.
PayeeSource []string `envconfig:"NORDIGEN_PAYEE_SOURCE" default:"unstructured,name"`
// * unstructured: uses the `RemittanceInformationUnstructured` field
// * name: uses either the either `debtorName` or `creditorName` field
// * additional: uses the `AdditionalInformation` field
PayeeSource []string `envconfig:"NORDIGEN_PAYEE_SOURCE" default:"unstructured,name,additional"`

// PayeeStrip is a list of words to remove from Payee. For example:
// "foo,bar"
Expand Down
23 changes: 17 additions & 6 deletions reader/nordigen/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,33 @@ func (Default) Map(cfg ynabber.Config, account ynabber.Account, t nordigen.Trans
return ynabber.Transaction{}, fmt.Errorf("failed to parse string to time: %w", err)
}

// Get the Payee data source
// Get the Payee from the first data source that returns data in the order
// defined by config
payee := ""
for _, source := range cfg.Nordigen.PayeeSource {
if payee == "" {
switch source {
// Unstructured should properly have been called "remittance" but
// its not. Some banks use this field as Payee.
case "unstructured":
payee = t.RemittanceInformationUnstructured
// Unstructured data may need some formatting, some banks
// inserts the amount and date which will cause every
// transaction to create a new Payee
payee = payeeStripNonAlphanumeric(payee)

// Name is using either creditor or debtor as the payee
case "name":
// Creditor/debtor name can be used as is
// Use either one
if t.CreditorName != "" {
payee = t.CreditorName
} else if t.DebtorName != "" {
payee = t.DebtorName
}
case "unstructured":
// Unstructured data may need some formatting
payee = t.RemittanceInformationUnstructured
payee = payeeStripNonAlphanumeric(payee)

// Additional uses AdditionalInformation as payee
case "additional":
payee = t.AdditionalInformation
default:
return ynabber.Transaction{}, fmt.Errorf("unrecognized PayeeSource: %s", source)
}
Expand Down
40 changes: 40 additions & 0 deletions reader/nordigen/nordigen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,46 @@ func TestTransactionToYnabber(t *testing.T) {
},
wantErr: false,
},
{
// Test transaction from SEB_KORT_AB_NO_SKHSFI21
name: "SEB_KORT_AB_NO_SKHSFI21",
args: args{
cfg: defaultConfig,
account: ynabber.Account{Name: "foo", IBAN: "bar"},
t: nordigen.Transaction{
TransactionId: "foobar",
EntryReference: "",
BookingDate: "2023-02-24",
ValueDate: "2023-02-24",
TransactionAmount: struct {
Amount string "json:\"amount,omitempty\""
Currency string "json:\"currency,omitempty\""
}{Amount: "10", Currency: "NOK"},
CreditorName: "",
CreditorAccount: struct {
Iban string "json:\"iban,omitempty\""
}{Iban: "0"},
UltimateCreditor: "",
DebtorName: "",
DebtorAccount: struct {
Iban string "json:\"iban,omitempty\""
}{Iban: ""},
UltimateDebtor: "",
RemittanceInformationUnstructured: "",
RemittanceInformationUnstructuredArray: []string{""},
BankTransactionCode: "PURCHASE",
AdditionalInformation: "PASCAL AS"},
},
want: ynabber.Transaction{
Account: ynabber.Account{Name: "foo", IBAN: "bar"},
ID: ynabber.ID("foobar"),
Date: time.Date(2023, time.February, 24, 0, 0, 0, 0, time.UTC),
Payee: "PASCAL AS",
Memo: "",
Amount: ynabber.Milliunits(10000),
},
wantErr: false,
},
}

for _, tt := range tests {
Expand Down

0 comments on commit f5676a5

Please sign in to comment.