Skip to content

Commit

Permalink
Added Notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
CanYumusak committed Dec 21, 2023
1 parent 4ec009b commit d86eb8c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 11 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
31 changes: 23 additions & 8 deletions reader/nordigen/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/martinohansen/ynabber"
"log"
"os"
"os/exec"
"path"
"strconv"
"time"
Expand All @@ -26,11 +28,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 +41,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 +83,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 +96,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 +111,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")
}
}
6 changes: 3 additions & 3 deletions reader/nordigen/nordigen.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ func BulkReader(cfg ynabber.Config) (t []ynabber.Transaction, err error) {
BankID: cfg.Nordigen.BankID,
File: dataFile(cfg),
}
r, err := Authorization.Wrapper()
r, err := Authorization.Wrapper(cfg)
if err != nil {
return nil, fmt.Errorf("failed to authorize: %w", err)
}
Expand All @@ -107,13 +107,13 @@ func BulkReader(cfg ynabber.Config) (t []ynabber.Transaction, err error) {
// Handle expired, or suspended accounts by recreating the
// requisition.
switch accountMetadata.Status {
case "EXPIRED", "SUSPENDED":
case "EXPIRED", "SUSPENDED ":
log.Printf(
"Account: %s is %s. Going to recreate the requisition...",
account,
accountMetadata.Status,
)
Authorization.CreateAndSave()
Authorization.CreateAndSave(cfg)
}

account := ynabber.Account{
Expand Down

0 comments on commit d86eb8c

Please sign in to comment.