ynab-portfolio-monitor/ynab/transactions.go

114 lines
4.3 KiB
Go
Raw Normal View History

2024-03-31 01:42:58 +00:00
// Package ynab provides a very simple API client for getting account data and setting account balances.
2023-11-12 19:04:22 +00:00
package ynab
import (
"fmt"
"net/url"
"time"
)
// Reference: https://api.ynab.com/v1#/Transactions/
2024-03-24 01:00:30 +00:00
type transaction struct {
2023-11-12 19:04:22 +00:00
Type string `json:"type,omitempty"`
ParentTransactionID interface{} `json:"parent_transaction_id,omitempty"`
ID string `json:"id,omitempty"`
Date string `json:"date,omitempty"`
Amount int `json:"amount,omitempty"`
Memo string `json:"memo,omitempty"`
Cleared string `json:"cleared,omitempty"`
Approved bool `json:"approved,omitempty"`
FlagColor interface{} `json:"flag_color,omitempty"`
AccountID string `json:"account_id,omitempty"`
AccountName string `json:"account_name,omitempty"`
PayeeID string `json:"payee_id,omitempty"`
PayeeName string `json:"payee_name,omitempty"`
CategoryID string `json:"category_id,omitempty"`
CategoryName string `json:"category_name,omitempty"`
TransferAccountID interface{} `json:"transfer_account_id,omitempty"`
TransferTransactionID interface{} `json:"transfer_transaction_id,omitempty"`
MatchedTransactionID interface{} `json:"matched_transaction_id,omitempty"`
ImportID string `json:"import_id,omitempty"`
Deleted bool `json:"deleted,omitempty"`
}
2024-03-23 19:39:50 +00:00
// Used for single transaction requests
2024-03-24 01:00:30 +00:00
type transactionRequest struct {
Transaction transaction `json:"transaction,omitempty"`
2024-03-23 19:39:50 +00:00
}
2023-11-12 20:06:39 +00:00
// Used for single transaction responses
2024-03-24 01:00:30 +00:00
type transactionResponse struct {
2023-11-12 19:04:22 +00:00
Data struct {
2024-03-23 19:39:50 +00:00
TransactionIDs []string `json:"transaction_ids,omitempty"`
2024-03-24 01:00:30 +00:00
Transaction transaction `json:"transaction"`
2024-03-23 19:39:50 +00:00
ServerKnowledge int `json:"server_knowledge,omitempty"`
2023-11-12 19:04:22 +00:00
}
}
// Used for multiple transaction requests / responses
2024-03-24 01:00:30 +00:00
type transactionListResponse struct {
2023-11-12 19:04:22 +00:00
Data struct {
2024-03-24 01:00:30 +00:00
Transactions []transaction `json:"transactions"`
2024-03-23 19:39:50 +00:00
ServerKnowledge int `json:"server_knowledge"`
2023-11-12 19:04:22 +00:00
} `json:"data"`
}
// Accepts a YNAB account ID and timestamp and returns all transactions in that account
// since the date provided
2024-03-24 01:00:30 +00:00
func (c *Client) GetAccountTransactions(accountID string, sinceDate time.Time) (*transactionListResponse, error) {
response := transactionListResponse{}
2023-11-12 19:04:22 +00:00
urlQuery := url.Values{}
urlQuery.Add("since_date", sinceDate.Format("2006-01-02"))
err := c.get(fmt.Sprintf("/accounts/%s/transactions", accountID), &response, urlQuery)
if err != nil {
return nil, fmt.Errorf("failed to get account transactions: %v", err)
}
return &response, nil
}
// Accepts a YNAB account ID and returns the transaction ID, amount and an error for the
// the first transaction found with Payee Name "Capital Gains or Losses"
2023-11-12 23:50:46 +00:00
func (c *Client) getTodayYnabCapitalGainsTransaction(accountID string) (string, int, error) {
2023-11-13 23:30:59 +00:00
ynabTransactions, err := c.GetAccountTransactions(accountID, time.Now().In(c.loc))
2023-11-12 19:04:22 +00:00
if err != nil {
return "", 0, fmt.Errorf("failed to get ynab transactions: %v", err)
}
for _, transaction := range ynabTransactions.Data.Transactions {
if transaction.PayeeName != "Capital Gains or Losses" {
continue
}
return transaction.ID, transaction.Amount, nil
}
return "", 0, nil
}
2023-11-12 20:06:39 +00:00
// Accepts a YNAB account ID, transaction ID and transaction amount and updates the YNAB transaction with the matching ID
2024-03-23 19:39:50 +00:00
// If transaction ID is blank, a new transaction is created for the amount specified
2023-11-12 23:50:46 +00:00
func (c *Client) updateTodayYNABCapitalGainsTransaction(accountID string, transactionID string, amount int) error {
2024-03-24 01:00:30 +00:00
request := transactionRequest{
Transaction: transaction{
2024-03-23 19:39:50 +00:00
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")),
},
}
2024-03-24 01:00:30 +00:00
response := &transactionResponse{}
2024-03-23 19:39:50 +00:00
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)
}
2023-11-12 19:04:22 +00:00
if err != nil {
2024-03-23 19:39:50 +00:00
return fmt.Errorf("request failed: %v", err)
2023-11-12 19:04:22 +00:00
}
return nil
}