simplify ynab package
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Steven Polley 2024-03-23 13:39:50 -06:00
parent e6d62a3e7b
commit 079ab596f8
2 changed files with 40 additions and 65 deletions

View File

@ -64,22 +64,11 @@ func (c *Client) SetAccountBalance(accountID string, balance int) error {
return nil // If balanceDelta is 0 do not create a transaction i.e. market is closed today return nil // If balanceDelta is 0 do not create a transaction i.e. market is closed today
} }
if ynabTransactionID == "" {
// there is no transaction - so create a new one
err = c.createTodayYNABCapitalGainsTransaction(accountID, balanceDelta)
if err != nil {
return fmt.Errorf("failed to create YNAB capital gains transaction for account ID '%s': %v", accountID, err)
}
log.Printf("Creating new capital gains transaction for YNAB account '%s' for amount: %d", accountID, balanceDelta)
} else {
// there is an existing transaction - so update the existing one
err = c.updateTodayYNABCapitalGainsTransaction(accountID, ynabTransactionID, balanceDelta) err = c.updateTodayYNABCapitalGainsTransaction(accountID, ynabTransactionID, balanceDelta)
if err != nil { if err != nil {
return fmt.Errorf("failed to update YNAB capital gains transaction for account ID '%s': %v", accountID, err) return fmt.Errorf("failed to update YNAB capital gains transaction for account ID '%s': %v", accountID, err)
} }
log.Printf("Updating existing capital gains transaction for YNAB account '%s' for amount: %d", accountID, balanceDelta) log.Printf("Capital gains transaction for YNAB account '%s' amount is: %d", accountID, balanceDelta)
}
return nil return nil
} }

View File

@ -7,8 +7,7 @@ import (
) )
// Reference: https://api.ynab.com/v1#/Transactions/ // Reference: https://api.ynab.com/v1#/Transactions/
type Transaction struct {
type BaseTransaction struct {
Type string `json:"type,omitempty"` Type string `json:"type,omitempty"`
ParentTransactionID interface{} `json:"parent_transaction_id,omitempty"` ParentTransactionID interface{} `json:"parent_transaction_id,omitempty"`
ID string `json:"id,omitempty"` ID string `json:"id,omitempty"`
@ -31,32 +30,32 @@ type BaseTransaction struct {
Deleted bool `json:"deleted,omitempty"` Deleted bool `json:"deleted,omitempty"`
} }
// Used for single transaction requests
type TransactionRequest struct {
Transaction Transaction `json:"transaction,omitempty"`
}
// Used for single transaction responses // Used for single transaction responses
type Transaction struct { type TransactionResponse struct {
Data struct { Data struct {
TransactionIDs []string `json:"transaction_ids,omitempty"` TransactionIDs []string `json:"transaction_ids,omitempty"`
Transaction BaseTransaction `json:"transaction"` Transaction Transaction `json:"transaction"`
ServerKnowledge int `json:"server_knowledge,omitempty"` ServerKnowledge int `json:"server_knowledge,omitempty"`
} }
} }
// Used for single transaction requests
type TransactionRequest struct {
Transaction BaseTransaction `json:"transaction,omitempty"`
}
// Used for multiple transaction requests / responses // Used for multiple transaction requests / responses
type Transactions struct { type TransactionsResponse struct {
Data struct { Data struct {
Transactions []BaseTransaction `json:"transactions"` Transactions []Transaction `json:"transactions"`
ServerKnowledge int `json:"server_knowledge"` ServerKnowledge int `json:"server_knowledge"`
} `json:"data"` } `json:"data"`
} }
// Accepts a YNAB account ID and timestamp and returns all transactions in that account // Accepts a YNAB account ID and timestamp and returns all transactions in that account
// since the date provided // since the date provided
func (c *Client) GetAccountTransactions(accountID string, sinceDate time.Time) (*Transactions, error) { func (c *Client) GetAccountTransactions(accountID string, sinceDate time.Time) (*TransactionsResponse, error) {
response := Transactions{} response := TransactionsResponse{}
urlQuery := url.Values{} urlQuery := url.Values{}
urlQuery.Add("since_date", sinceDate.Format("2006-01-02")) urlQuery.Add("since_date", sinceDate.Format("2006-01-02"))
@ -84,43 +83,30 @@ func (c *Client) getTodayYnabCapitalGainsTransaction(accountID string) (string,
return "", 0, nil return "", 0, nil
} }
// Accepts a YNAB account ID and transaction amount and creates a new YNAB transaction
func (c *Client) createTodayYNABCapitalGainsTransaction(accountID string, amount int) error {
transaction := TransactionRequest{}
transaction.Transaction.AccountID = accountID
transaction.Transaction.Amount = amount
transaction.Transaction.Date = time.Now().In(c.loc).Format("2006-01-02")
transaction.Transaction.Cleared = "cleared"
transaction.Transaction.Approved = true
transaction.Transaction.PayeeName = "Capital Gains or Losses"
transaction.Transaction.Memo = fmt.Sprintf("Quoted at: %s", time.Now().In(c.loc).Format("2006-01-02 15:04:05"))
response := &Transaction{}
err := c.post("/transactions", response, transaction)
if err != nil {
return fmt.Errorf("failed to post transaction: %v", err)
}
return nil
}
// Accepts a YNAB account ID, transaction ID and transaction amount and updates the YNAB transaction with the matching ID // Accepts a YNAB account ID, transaction ID and transaction amount and updates the YNAB transaction with the matching ID
// If transaction ID is blank, a new transaction is created for the amount specified
func (c *Client) updateTodayYNABCapitalGainsTransaction(accountID string, transactionID string, amount int) error { func (c *Client) updateTodayYNABCapitalGainsTransaction(accountID string, transactionID string, amount int) error {
transaction := TransactionRequest{} request := TransactionRequest{
transaction.Transaction.AccountID = accountID Transaction: Transaction{
transaction.Transaction.ID = transactionID AccountID: accountID,
transaction.Transaction.Amount = amount Amount: amount,
transaction.Transaction.Date = time.Now().In(c.loc).Format("2006-01-02") Date: time.Now().In(c.loc).Format("2006-01-02"),
transaction.Transaction.Cleared = "cleared" Cleared: "cleared",
transaction.Transaction.Approved = true Approved: true,
transaction.Transaction.PayeeName = "Capital Gains or Losses" PayeeName: "Capital Gains or Losses",
transaction.Transaction.Memo = fmt.Sprintf("Quoted at: %s", time.Now().In(c.loc).Format("2006-01-02 15:04:05")) Memo: fmt.Sprintf("Quoted at: %s", time.Now().In(c.loc).Format("2006-01-02 15:04:05")),
},
}
response := &TransactionResponse{}
var err error
if transactionID == "" { // create transaction
err = c.post("/transactions", response, request)
} else { // update existing transaction
err = c.put(fmt.Sprintf("/transactions/%s", transactionID), response, request)
}
response := &Transaction{}
err := c.put(fmt.Sprintf("/transactions/%s", transactionID), response, transaction)
if err != nil { if err != nil {
return fmt.Errorf("failed to put transaction: %v", err) return fmt.Errorf("request failed: %v", err)
} }
return nil return nil
} }