Skip to content

Commit

Permalink
Merge branch 'main' into martin/reader-writer-interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
martinohansen committed Dec 22, 2023
2 parents ef30fbb + 05f8d35 commit bbd551b
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
3 changes: 3 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ type Config struct {
// PayeeStrip is depreciated please use Nordigen.PayeeStrip instead
PayeeStrip []string `envconfig:"YNABBER_PAYEE_STRIP"`

// SecretID is used to create requisition
NotificationScript string `envconfig:"NOTIFICATION_SCRIPT"`

// Reader and/or writer specific settings
Nordigen Nordigen
YNAB YNAB
Expand Down
32 changes: 24 additions & 8 deletions reader/nordigen/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import (
"fmt"
"log"
"os"
"os/exec"
"path"
"strconv"
"time"

"github.com/martinohansen/ynabber"

"github.com/frieser/nordigen-go-lib/v2"
)

Expand All @@ -26,11 +29,11 @@ func (auth Authorization) Store() string {

// AuthorizationWrapper tries to get requisition from disk, if it fails it will
// create a new and store that one to disk.
func (auth Authorization) Wrapper() (nordigen.Requisition, error) {
func (auth Authorization) Wrapper(cfg *ynabber.Config) (nordigen.Requisition, error) {
requisitionFile, err := os.ReadFile(auth.Store())
if errors.Is(err, os.ErrNotExist) {
log.Print("Requisition is not found")
return auth.CreateAndSave()
return auth.CreateAndSave(*cfg)
} else if err != nil {
return nordigen.Requisition{}, fmt.Errorf("ReadFile: %w", err)
}
Expand All @@ -39,24 +42,24 @@ func (auth Authorization) Wrapper() (nordigen.Requisition, error) {
err = json.Unmarshal(requisitionFile, &requisition)
if err != nil {
log.Print("Failed to parse requisition file")
return auth.CreateAndSave()
return auth.CreateAndSave(*cfg)
}

switch requisition.Status {
case "EX":
log.Printf("Requisition is expired")
return auth.CreateAndSave()
return auth.CreateAndSave(*cfg)
case "LN":
return requisition, nil
default:
log.Printf("Unsupported requisition status: %s", requisition.Status)
return auth.CreateAndSave()
return auth.CreateAndSave(*cfg)
}
}

func (auth Authorization) CreateAndSave() (nordigen.Requisition, error) {
func (auth Authorization) CreateAndSave(cfg ynabber.Config) (nordigen.Requisition, error) {
log.Print("Creating new requisition...")
requisition, err := auth.Create()
requisition, err := auth.Create(cfg)
if err != nil {
return nordigen.Requisition{}, fmt.Errorf("AuthorizationCreate: %w", err)
}
Expand All @@ -81,7 +84,7 @@ func (auth Authorization) Save(requisition nordigen.Requisition) error {
return nil
}

func (auth Authorization) Create() (nordigen.Requisition, error) {
func (auth Authorization) Create(cfg ynabber.Config) (nordigen.Requisition, error) {
requisition := nordigen.Requisition{
Redirect: "https://raw.githubusercontent.com/martinohansen/ynabber/main/ok.html",
Reference: strconv.Itoa(int(time.Now().Unix())),
Expand All @@ -94,6 +97,7 @@ func (auth Authorization) Create() (nordigen.Requisition, error) {
return nordigen.Requisition{}, fmt.Errorf("CreateRequisition: %w", err)
}

auth.Notify(cfg, r)
log.Printf("Initiate requisition by going to: %s", r.Link)

// Keep waiting for the user to accept the requisition
Expand All @@ -108,3 +112,15 @@ func (auth Authorization) Create() (nordigen.Requisition, error) {

return r, nil
}

func (auth Authorization) Notify(cfg ynabber.Config, r nordigen.Requisition) {
if cfg.NotificationScript != "" {
cmd := exec.Command(cfg.NotificationScript, r.Link)
_, err := cmd.Output()
if err != nil {
log.Println("Could not notify user about new requisition: ", err)
}
} else {
log.Println("No Notification Script set")
}
}
4 changes: 2 additions & 2 deletions reader/nordigen/nordigen.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (r Reader) Bulk() (t []ynabber.Transaction, err error) {
BankID: r.Config.Nordigen.BankID,
File: dataFile(*r.Config),
}
req, err := Authorization.Wrapper()
req, err := Authorization.Wrapper(r.Config)
if err != nil {
return nil, fmt.Errorf("failed to authorize: %w", err)
}
Expand All @@ -117,7 +117,7 @@ func (r Reader) Bulk() (t []ynabber.Transaction, err error) {
account,
accountMetadata.Status,
)
Authorization.CreateAndSave()
Authorization.CreateAndSave(*r.Config)
}

account := ynabber.Account{
Expand Down

0 comments on commit bbd551b

Please sign in to comment.