Small refactorings
This commit is contained in:
parent
c5a0f49719
commit
46b9b82f30
@ -5,7 +5,7 @@ import (
|
||||
"embed"
|
||||
"fmt"
|
||||
|
||||
_ "github.com/jackc/pgx/v4/stdlib"
|
||||
_ "github.com/jackc/pgx/v4/stdlib" // needed for pg connection
|
||||
"github.com/pressly/goose/v3"
|
||||
)
|
||||
|
||||
|
@ -70,7 +70,6 @@ func (ynab *YNABImport) ImportAssignments(context context.Context, r io.Reader)
|
||||
|
||||
count := 0
|
||||
for _, record := range csvData[1:] {
|
||||
|
||||
dateString := record[0]
|
||||
date, err := time.Parse("Jan 2006", dateString)
|
||||
if err != nil {
|
||||
@ -183,12 +182,37 @@ func (ynab *YNABImport) ImportTransactions(context context.Context, r io.Reader)
|
||||
}
|
||||
|
||||
payeeName := record[3]
|
||||
if strings.HasPrefix(payeeName, "Transfer : ") {
|
||||
// Transaction is a transfer to
|
||||
var shouldReturn bool
|
||||
var returnValue error
|
||||
openTransfers, shouldReturn, returnValue = ynab.ImportTransaction(payeeName, context, transaction, accountName, openTransfers, account, amount)
|
||||
if shouldReturn {
|
||||
return returnValue
|
||||
}
|
||||
|
||||
count++
|
||||
}
|
||||
|
||||
for _, openTransfer := range openTransfers {
|
||||
fmt.Printf("Saving unmatched transfer from %s to %s on %s over %f as regular transaction\n",
|
||||
openTransfer.FromAccount, openTransfer.ToAccount, openTransfer.Date, openTransfer.Amount.GetFloat64())
|
||||
_, err = ynab.queries.CreateTransaction(context, openTransfer.CreateTransactionParams)
|
||||
if err != nil {
|
||||
return fmt.Errorf("save transaction %v: %w", openTransfer.CreateTransactionParams, err)
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Printf("Imported %d transactions\n", count)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ynab *YNABImport) ImportTransaction(payeeName string, context context.Context, transaction CreateTransactionParams, accountName string, openTransfers []Transfer, account *Account, amount Numeric) ([]Transfer, bool, error) {
|
||||
if strings.HasPrefix(payeeName, "Transfer : ") {
|
||||
transferToAccountName := payeeName[11:]
|
||||
transferToAccount, err := ynab.GetAccount(context, transferToAccountName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get transfer account %s: %w", transferToAccountName, err)
|
||||
return nil, true, fmt.Errorf("get transfer account %s: %w", transferToAccountName, err)
|
||||
}
|
||||
|
||||
transfer := Transfer{
|
||||
@ -221,11 +245,11 @@ func (ynab *YNABImport) ImportTransactions(context context.Context, r io.Reader)
|
||||
|
||||
_, err = ynab.queries.CreateTransaction(context, transfer.CreateTransactionParams)
|
||||
if err != nil {
|
||||
return fmt.Errorf("save transaction %v: %w", transfer.CreateTransactionParams, err)
|
||||
return nil, true, fmt.Errorf("save transaction %v: %w", transfer.CreateTransactionParams, err)
|
||||
}
|
||||
_, err = ynab.queries.CreateTransaction(context, openTransfer.CreateTransactionParams)
|
||||
if err != nil {
|
||||
return fmt.Errorf("save transaction %v: %w", openTransfer.CreateTransactionParams, err)
|
||||
return nil, true, fmt.Errorf("save transaction %v: %w", openTransfer.CreateTransactionParams, err)
|
||||
}
|
||||
break
|
||||
}
|
||||
@ -236,30 +260,16 @@ func (ynab *YNABImport) ImportTransactions(context context.Context, r io.Reader)
|
||||
} else {
|
||||
payeeID, err := ynab.GetPayee(context, payeeName)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get payee %s: %w", payeeName, err)
|
||||
return nil, true, fmt.Errorf("get payee %s: %w", payeeName, err)
|
||||
}
|
||||
transaction.PayeeID = payeeID
|
||||
|
||||
_, err = ynab.queries.CreateTransaction(context, transaction)
|
||||
if err != nil {
|
||||
return fmt.Errorf("save transaction %v: %w", transaction, err)
|
||||
return nil, true, fmt.Errorf("save transaction %v: %w", transaction, err)
|
||||
}
|
||||
}
|
||||
|
||||
count++
|
||||
}
|
||||
|
||||
for _, openTransfer := range openTransfers {
|
||||
fmt.Printf("Saving unmatched transfer from %s to %s on %s over %f as regular transaction\n", openTransfer.FromAccount, openTransfer.ToAccount, openTransfer.Date, openTransfer.Amount.GetFloat64())
|
||||
_, err = ynab.queries.CreateTransaction(context, openTransfer.CreateTransactionParams)
|
||||
if err != nil {
|
||||
return fmt.Errorf("save transaction %v: %w", openTransfer.CreateTransactionParams, err)
|
||||
}
|
||||
|
||||
}
|
||||
fmt.Printf("Imported %d transactions\n", count)
|
||||
|
||||
return nil
|
||||
return openTransfers, false, nil
|
||||
}
|
||||
|
||||
func trimLastChar(s string) string {
|
||||
|
Loading…
x
Reference in New Issue
Block a user