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
}
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)
if err != nil {
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)
err = c.updateTodayYNABCapitalGainsTransaction(accountID, ynabTransactionID, balanceDelta)
if err != nil {
return fmt.Errorf("failed to update YNAB capital gains transaction for account ID '%s': %v", accountID, err)
}
log.Printf("Capital gains transaction for YNAB account '%s' amount is: %d", accountID, balanceDelta)
return nil
}

View File

@ -7,8 +7,7 @@ import (
)
// Reference: https://api.ynab.com/v1#/Transactions/
type BaseTransaction struct {
type Transaction struct {
Type string `json:"type,omitempty"`
ParentTransactionID interface{} `json:"parent_transaction_id,omitempty"`
ID string `json:"id,omitempty"`
@ -31,32 +30,32 @@ type BaseTransaction struct {
Deleted bool `json:"deleted,omitempty"`
}
// Used for single transaction requests
type TransactionRequest struct {
Transaction Transaction `json:"transaction,omitempty"`
}
// Used for single transaction responses
type Transaction struct {
type TransactionResponse struct {
Data struct {
TransactionIDs []string `json:"transaction_ids,omitempty"`
Transaction BaseTransaction `json:"transaction"`
ServerKnowledge int `json:"server_knowledge,omitempty"`
TransactionIDs []string `json:"transaction_ids,omitempty"`
Transaction Transaction `json:"transaction"`
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
type Transactions struct {
type TransactionsResponse struct {
Data struct {
Transactions []BaseTransaction `json:"transactions"`
ServerKnowledge int `json:"server_knowledge"`
Transactions []Transaction `json:"transactions"`
ServerKnowledge int `json:"server_knowledge"`
} `json:"data"`
}
// Accepts a YNAB account ID and timestamp and returns all transactions in that account
// since the date provided
func (c *Client) GetAccountTransactions(accountID string, sinceDate time.Time) (*Transactions, error) {
response := Transactions{}
func (c *Client) GetAccountTransactions(accountID string, sinceDate time.Time) (*TransactionsResponse, error) {
response := TransactionsResponse{}
urlQuery := url.Values{}
urlQuery.Add("since_date", sinceDate.Format("2006-01-02"))
@ -84,43 +83,30 @@ func (c *Client) getTodayYnabCapitalGainsTransaction(accountID string) (string,
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
// 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 {
transaction := TransactionRequest{}
transaction.Transaction.AccountID = accountID
transaction.Transaction.ID = transactionID
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"))
request := TransactionRequest{
Transaction: Transaction{
AccountID: accountID,
Amount: amount,
Date: time.Now().In(c.loc).Format("2006-01-02"),
Cleared: "cleared",
Approved: true,
PayeeName: "Capital Gains or Losses",
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 {
return fmt.Errorf("failed to put transaction: %v", err)
return fmt.Errorf("request failed: %v", err)
}
return nil
}