From 20cb7a9eccbe9664a23122c62fbf83d78f1a6785 Mon Sep 17 00:00:00 2001 From: stuzer05 Date: Wed, 27 Mar 2024 19:06:44 +0200 Subject: [PATCH] Add personal accounts support, fix multiple transactions store --- main.go | 48 +++++++++++++++---- .../api/client_info/requests/client_info.go | 2 +- .../api/statement/models/statement_request.go | 7 +++ .../statement/models/statement_response.go | 22 +++++++++ monobank/api/statement/requests/statement.go | 20 ++++++++ monobank/request.go | 2 +- shop_config.go | 2 + 7 files changed, 91 insertions(+), 12 deletions(-) create mode 100644 monobank/api/statement/models/statement_request.go create mode 100644 monobank/api/statement/models/statement_response.go create mode 100644 monobank/api/statement/requests/statement.go diff --git a/main.go b/main.go index 7193789..556260b 100644 --- a/main.go +++ b/main.go @@ -42,7 +42,7 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) { //w.WriteHeader(http.StatusOK) //return - //body = []byte("{\"type\":\"StatementItem\",\"data\":{\"account\":\"4723djMLsLOCzhoeYjxqRw\",\"statementItem\":{\"id\":\"XKCZ__9Eaf0ihZ3Dwg\",\"time\":1711458930,\"description\":\"414950****7166\",\"mcc\":4829,\"originalMcc\":4829,\"amount\":-249600,\"operationAmount\":-249600,\"currencyCode\":980,\"commissionRate\":9600,\"cashbackAmount\":0,\"balance\":9223637,\"hold\":true,\"receiptId\":\"8285-K777-PBHC-T9BX\"}}}") + //body = []byte("{\"type\":\"StatementItem\",\"data\":{\"account\":\"4723djMLsLOCzhoeYjxqRw\",\"statementItem\":{\"id\":\"5_NQ0arGAmp2pyNzvA\",\"time\":1711544958,\"description\":\"Ілля Ш.\",\"mcc\":4829,\"originalMcc\":4829,\"amount\":-572000,\"operationAmount\":-572000,\"currencyCode\":980,\"commissionRate\":22000,\"cashbackAmount\":0,\"balance\":8101246,\"hold\":true,\"receiptId\":\"EMXC-P266-90PC-EB8C\"}}}") LogString(string(body)) if len(string(body)) == 0 { @@ -60,6 +60,18 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) { return } + //statement, err := requests.Statement(models2.StatementRequest{ + // Account: transaction.Data.Account, + // From: transaction.Data.StatementItem.Time, + // To: transaction.Data.StatementItem.Time, + //}) + //if err != nil { + // fmt.Printf("%+v", err.Error()) + // w.WriteHeader(http.StatusOK) + // return + //} + //fmt.Printf("%+v", statement) + // init firefly3 client clientConf := firefly3.NewConfiguration() clientConf.BasePath = os.Getenv("FIREFLY3_API_URL") @@ -102,7 +114,7 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) { Type_: &transactionTypeWithdrawal, Date: time.Unix(int64(transaction.Data.StatementItem.Time), 0).Add(time.Hour * 2), Notes: string(body), - Amount: strconv.Itoa(int(math.Abs(math.Round(float64(transaction.Data.StatementItem.Amount / 100))))), + Amount: strconv.Itoa(int(math.Abs(math.Round(float64(transaction.Data.StatementItem.Amount/100)))) - int(math.Abs(math.Round(float64(transaction.Data.StatementItem.CommissionRate/100))))), SourceId: account.Id, } @@ -120,6 +132,8 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) { for _, row := range ShopConfig { if slices.Contains(row.Names, transaction.Data.StatementItem.Description) || slices.Contains(row.MCCCodes, transaction.Data.StatementItem.Mcc) { firefly3Transaction.Description = row.TransactionDescription + firefly3Transaction.DestinationName = row.TransactionDestination + firefly3Transaction.CategoryName = row.TransactionCategory firefly3Transactions = append(firefly3Transactions, firefly3Transaction) break } @@ -140,14 +154,18 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) { if len(firefly3Transactions) > 0 { transactionOpts := firefly3.TransactionsApiStoreTransactionOpts{} - _, _, err = client.TransactionsApi.StoreTransaction(ctx, firefly3.TransactionStore{ - ApplyRules: true, - Transactions: firefly3Transactions, - }, &transactionOpts) - if err != nil { - LogString(err.Error()) - w.WriteHeader(http.StatusOK) - return + + for _, transaction := range firefly3Transactions { + _, _, err = client.TransactionsApi.StoreTransaction(ctx, firefly3.TransactionStore{ + ApplyRules: true, + Transactions: []firefly3.TransactionSplitStore{transaction}, + }, &transactionOpts) + if err != nil { + LogString(err.Error()) + fmt.Println(err.Error()) + w.WriteHeader(http.StatusOK) + return + } } } @@ -188,6 +206,16 @@ func main() { TransactionDescription: "JetBrains: GoLand", }) + /** + * People + */ + ShopConfig = append(ShopConfig, ShopConfigItem{ + Names: []string{"Ілля Ш."}, + TransactionDescription: "Legal services: Alva Privacy Law Firm", + TransactionDestination: "Legal: Alva Privacy Law Firm", + TransactionCategory: "Legal services", + }) + /** * Other */ diff --git a/monobank/api/client_info/requests/client_info.go b/monobank/api/client_info/requests/client_info.go index 875c545..3973e94 100644 --- a/monobank/api/client_info/requests/client_info.go +++ b/monobank/api/client_info/requests/client_info.go @@ -9,7 +9,7 @@ import ( func ClientInfo() (models2.ClientInfo, error) { data := models2.ClientInfo{} - responseJson, err := monobank.Request("GET", "https://firefly3.monobank.ua/personal/client-info", struct{}{}) + responseJson, err := monobank.Request("GET", "https://api.monobank.ua/personal/client-info", struct{}{}) if err != nil { return data, err } diff --git a/monobank/api/statement/models/statement_request.go b/monobank/api/statement/models/statement_request.go new file mode 100644 index 0000000..46424f2 --- /dev/null +++ b/monobank/api/statement/models/statement_request.go @@ -0,0 +1,7 @@ +package models + +type StatementRequest struct { + Account string `json:"account"` + From int `json:"from"` + To int `json:"to"` +} diff --git a/monobank/api/statement/models/statement_response.go b/monobank/api/statement/models/statement_response.go new file mode 100644 index 0000000..9a37857 --- /dev/null +++ b/monobank/api/statement/models/statement_response.go @@ -0,0 +1,22 @@ +package models + +type Statement struct { + ID string `json:"id"` + Time int `json:"time"` + Description string `json:"description"` + Mcc int `json:"mcc"` + OriginalMcc int `json:"originalMcc"` + Hold bool `json:"hold"` + Amount int `json:"amount"` + OperationAmount int `json:"operationAmount"` + CurrencyCode int `json:"currencyCode"` + CommissionRate int `json:"commissionRate"` + CashbackAmount int `json:"cashbackAmount"` + Balance int `json:"balance"` + Comment string `json:"comment"` + ReceiptID string `json:"receiptId"` + InvoiceID string `json:"invoiceId"` + CounterEdrpou string `json:"counterEdrpou"` + CounterIban string `json:"counterIban"` + CounterName string `json:"counterName"` +} diff --git a/monobank/api/statement/requests/statement.go b/monobank/api/statement/requests/statement.go new file mode 100644 index 0000000..b2f1305 --- /dev/null +++ b/monobank/api/statement/requests/statement.go @@ -0,0 +1,20 @@ +package requests + +import ( + "encoding/json" + "main/monobank" + models2 "main/monobank/api/statement/models" + "strconv" +) + +func Statement(request models2.StatementRequest) ([]models2.Statement, error) { + data := []models2.Statement{} + + responseJson, err := monobank.Request("GET", "https://api.monobank.ua/personal/statement/"+request.Account+"/"+strconv.Itoa(request.From)+"/"+strconv.Itoa(request.To), struct{}{}) + if err != nil { + return data, err + } + json.Unmarshal([]byte(responseJson), &data) + + return data, nil +} diff --git a/monobank/request.go b/monobank/request.go index ad8dbf7..6ed9246 100644 --- a/monobank/request.go +++ b/monobank/request.go @@ -21,7 +21,7 @@ func Request(method string, url string, data interface{}) (string, error) { return "", err } - r.Header.Add("Accept", "application/vnd.firefly3+json") + r.Header.Add("Accept", "application/vnd.api+json") r.Header.Add("Content-Type", "application/json") r.Header.Add("X-Token", apiMonabankToken) diff --git a/shop_config.go b/shop_config.go index 6e71c50..d866b96 100644 --- a/shop_config.go +++ b/shop_config.go @@ -4,4 +4,6 @@ type ShopConfigItem struct { MCCCodes []int Names []string TransactionDescription string + TransactionDestination string + TransactionCategory string }