-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransaction.go
72 lines (58 loc) · 1.66 KB
/
transaction.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package lunchmoney
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"github.com/dylanmazurek/lunchmoney/models"
"github.com/dylanmazurek/lunchmoney/util/constants"
)
type ListTransactionFilter struct {
}
func (c *Client) ListTransaction() (*[]models.Transaction, error) {
urlString := fmt.Sprintf("%s/%s", constants.Config.APIBaseURL, constants.Path.Transactions)
requestUrl, err := url.Parse(urlString)
if err != nil {
return nil, err
}
params := &url.Values{
"start": {"01/01/2023"},
"end": {"25/12/2023"},
}
req, err := c.NewRequest(http.MethodGet, requestUrl.String(), nil, params)
if err != nil {
return nil, err
}
var transactions models.TransactionResponse
err = c.Do(req, &transactions)
return &transactions.Transactions, err
}
func (c *Client) InsertTransactions(transactions []models.Transaction, debitAsNegative bool) (*[]int64, error) {
insertReqBody := &models.InsertRequest{
Transactions: transactions,
ApplyRules: true,
SkipDuplicates: true,
CheckForRecurring: true,
DebitAsNegative: debitAsNegative,
SkipBalanceUpdate: true,
}
insertJson, err := json.Marshal(&insertReqBody)
if err != nil {
return nil, err
}
req, err := c.NewRequest(http.MethodPost, constants.Path.Transactions, bytes.NewReader(insertJson), nil)
if err != nil {
return nil, err
}
var transactionInsertResponse models.InsertResponse
err = c.Do(req, &transactionInsertResponse)
if len(transactionInsertResponse.Errors) > 0 {
for _, oneErr := range transactionInsertResponse.Errors {
newError := errors.New(oneErr)
err = errors.Join(err, newError)
}
}
return &transactionInsertResponse.Ids, err
}