diff --git a/src/main.go b/src/main.go index ce6b430..0625f13 100644 --- a/src/main.go +++ b/src/main.go @@ -1,10 +1,16 @@ package main import ( + "bytes" + "encoding/json" + "fmt" "github.com/joho/godotenv" + "io" "log" "net/http" + "net/smtp" "os" + "time" ) type BudgetClient struct { @@ -14,6 +20,41 @@ type BudgetClient struct { fullUrl string } +type BudgetMonthsResponse struct { + Data struct { + Month string `json:"month"` + IncomeAvailable int `json:"incomeAvailable"` + LastMonthOverspent int `json:"lastMonthOverspent"` + ForNextMonth int `json:"forNextMonth"` + TotalBudgeted int `json:"totalBudgeted"` + ToBudget int `json:"toBudget"` + FromLastMonth int `json:"fromLastMonth"` + TotalIncome int `json:"totalIncome"` + TotalSpent int `json:"totalSpent"` + TotalBalance int `json:"totalBalance"` + CategoryGroups []struct { + ID string `json:"id"` + Name string `json:"name"` + IsIncome bool `json:"is_income"` + Hidden bool `json:"hidden"` + Budgeted int `json:"budgeted"` + Spent int `json:"spent"` + Balance int `json:"balance"` + Categories []struct { + ID string `json:"id"` + Name string `json:"name"` + IsIncome bool `json:"is_income"` + Hidden bool `json:"hidden"` + GroupID string `json:"group_id"` + Budgeted int `json:"budgeted"` + Spent int `json:"spent"` + Balance int `json:"balance"` + Carryover bool `json:"carryover"` + } `json:"categories"` + } `json:"categoryGroups"` + } `json:"data"` +} + func CreateBudgetClient(baseUrl string, apiKey string, syncId string) *BudgetClient { fullUrl := baseUrl + "/v1/budgets/" + syncId client := BudgetClient{baseUrl, apiKey, syncId, fullUrl} @@ -25,7 +66,7 @@ func CreateBudgetClient(baseUrl string, apiKey string, syncId string) *BudgetCli // method is POST, GET, ect // route is the route to call with a leading / ex. /accounts/banksync // headers is a map of strings expecting "header" "value" -func (b BudgetClient) callApi(method string, route string, headers map[string]string) bool { +func (b BudgetClient) callApi(method string, route string, headers map[string]string) *http.Response { var httpClient http.Client req, err := http.NewRequest(method, b.fullUrl+route, http.NoBody) if err != nil { @@ -39,12 +80,35 @@ func (b BudgetClient) callApi(method string, route string, headers map[string]st if err != nil { log.Fatal(err) } - log.Println(resp) + return resp +} + +// Triggers a bank sync, returns true if successful +func (b BudgetClient) BankSync() bool { + resp := b.callApi("POST", "/accounts/banksync", nil) return resp.StatusCode == http.StatusOK } -func (b BudgetClient) BankSync() bool { - return b.callApi("POST", "/accounts/banksync", nil) +func (b BudgetClient) GetBudgetMonths() *BudgetMonthsResponse { + currentTime := time.Now() + year, month := currentTime.Year(), int(currentTime.Month()) + budgetMonth := fmt.Sprintf("%v-%v", year, month) + resp := b.callApi("GET", "/months/"+budgetMonth, nil) + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK { + log.Fatal("GetBudgetAmounts failed with: " + string(resp.Status)) + } + var data bytes.Buffer + + _, err := io.Copy(&data, resp.Body) + if err != nil { + log.Println(err) + } + + var budgetMonths BudgetMonthsResponse + err = json.Unmarshal(data.Bytes(), &budgetMonths) + return &budgetMonths + } func main() { @@ -54,9 +118,34 @@ func main() { log.Fatal("Error loading .env file") } } - var baseUrl = os.Getenv("BASE_URL") - var apiKey = os.Getenv("API_KEY") - var syncId = os.Getenv("SYNC_ID") - client := CreateBudgetClient(baseUrl, apiKey, syncId) - client.BankSync() + //var baseUrl = os.Getenv("BASE_URL") + //var apiKey = os.Getenv("API_KEY") + //var syncId = os.Getenv("SYNC_ID") + var smtpUsername = os.Getenv("SMTP_USERNAME") + var smtpPassword = os.Getenv("SMTP_PASSWORD") + var smtpHost = os.Getenv("SMTP_HOST") + var smtpRecipients = os.Getenv("SMTP_RECIPIENTS") + //client := CreateBudgetClient(baseUrl, apiKey, syncId) + //if !client.BankSync() { + // log.Println("Bank Sync failed, information may not be up to date") + //} + // budgetMonths := client.GetBudgetMonths() + + const message = ` + Good day, + Your budget as of %s: + Total: %d + ` + + var auth smtp.Auth + if os.Getenv("ENVIRONMENT") == "dev" { + auth = nil + } else { + auth = smtp.PlainAuth("", smtpUsername, smtpPassword, smtpHost) + } + err := smtp.SendMail(smtpHost, auth, smtpUsername, []string{smtpRecipients}, []byte(message)) + if err != nil { + log.Fatal(err) + } + }