Skip to content

Commit

Permalink
Merge pull request #53 from martinohansen/nordea-fix
Browse files Browse the repository at this point in the history
feat(Nordigen): add config to change ID
  • Loading branch information
martinohansen committed May 19, 2023
2 parents 697d62e + b1ec8e8 commit 4ec009b
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,20 +56,20 @@ docker run \

## Readers

Currently supported readers and verified banks
Currently tested readers and verified banks

| Reader | Bank | |
|----------|-----------------|---|
| Nordigen | ALANDSBANKEN_AABAFI22 | ✅
| | NORDEA_NDEADKKK | ✅
| | NORDEA_NDEADKKK | ✅[^1]
| | NORDEA_NDEAFIHH | ✅
| | NORWEGIAN_FI_NORWNOK1 | ✅
| | S_PANKKI_SBANFIHH | ✅

Note that additional banks may also be supported, but they have not been
specifically verified and tested yet. Please open an
[issue](https://github.com/martinohansen/ynabber/issues/new) if you have
problems with a specific bank.
Please open an [issue](https://github.com/martinohansen/ynabber/issues/new) if
you have problems with a specific bank.

[^1]: Set NORDIGEN_TRANSACTION_ID to "InternalTransactionId" if using YNAB_IMPORT_ID_V2

## Contributing

Expand Down
9 changes: 9 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,15 @@ type Nordigen struct {
// PayeeStrip is a list of words to remove from Payee. For example:
// "foo,bar"
PayeeStrip []string `envconfig:"NORDIGEN_PAYEE_STRIP"`

// TransactionID picks the field to use as transaction ID. This is relevant
// for some banks where the ID provided by the bank is not consistent. For
// example with NORDEA_NDEADKKK the TransactionId changes with time, which
// might cause hard to debug duplicate entries in YNAB. Only change this if
// you have a good reason to do so.
//
// Valid options are: TransactionId, InternalTransactionId
TransactionID string `envconfig:"NORDIGEN_TRANSACTION_ID" default:"TransactionId"`
}

// YNAB related settings
Expand Down
16 changes: 13 additions & 3 deletions reader/nordigen/mapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type Mapper interface {
type Default struct{}

// Map Nordigen transactions using the default mapper
func (Default) Map(cfg ynabber.Config, account ynabber.Account, t nordigen.Transaction) (ynabber.Transaction, error) {
func (Default) Map(cfg ynabber.Config, a ynabber.Account, t nordigen.Transaction) (ynabber.Transaction, error) {
amount, err := strconv.ParseFloat(t.TransactionAmount.Amount, 64)
if err != nil {
return ynabber.Transaction{}, fmt.Errorf("failed to convert string to float: %w", err)
Expand Down Expand Up @@ -61,9 +61,19 @@ func (Default) Map(cfg ynabber.Config, account ynabber.Account, t nordigen.Trans
}
}

// Get the ID from the first data source that returns data as defined in the
// config
var id string
switch cfg.Nordigen.TransactionID {
case "InternalTransactionId":
id = t.InternalTransactionId
default:
id = t.TransactionId
}

return ynabber.Transaction{
Account: account,
ID: ynabber.ID(t.TransactionId),
Account: a,
ID: ynabber.ID(id),
Date: date,
Payee: ynabber.Payee(payee),
Memo: t.RemittanceInformationUnstructured,
Expand Down
2 changes: 1 addition & 1 deletion reader/nordigen/nordigen.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func BulkReader(cfg ynabber.Config) (t []ynabber.Transaction, err error) {
}

if cfg.Debug {
log.Printf("Transactions received from Nordigen: %s\n", transactions)
log.Printf("Transactions received from Nordigen: %+v", transactions)
}

x, err := transactionsToYnabber(cfg, account, transactions)
Expand Down
10 changes: 5 additions & 5 deletions writer/ynab/ynab.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ func BulkWriter(cfg ynabber.Config, t []ynabber.Transaction) error {
return nil
}

if cfg.Debug {
log.Printf("Request to YNAB: %+v", y)
}

url := fmt.Sprintf("https://api.youneedabudget.com/v1/budgets/%s/transactions", cfg.YNAB.BudgetID)

payload, err := json.Marshal(y)
Expand All @@ -172,10 +176,6 @@ func BulkWriter(cfg ynabber.Config, t []ynabber.Transaction) error {

client := &http.Client{}

if cfg.Debug {
log.Printf("Request to YNAB: %s\n", payload)
}

req, err := http.NewRequest("POST", url, bytes.NewBuffer(payload))
if err != nil {
return err
Expand All @@ -191,7 +191,7 @@ func BulkWriter(cfg ynabber.Config, t []ynabber.Transaction) error {

if cfg.Debug {
b, _ := httputil.DumpResponse(res, true)
log.Printf("Response from YNAB: %s\n", b)
log.Printf("Response from YNAB: %s", b)
}

if res.StatusCode != http.StatusCreated {
Expand Down

0 comments on commit 4ec009b

Please sign in to comment.