Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Basic Shell Notifications #56

Merged
merged 1 commit into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading