Add personal accounts support, fix multiple transactions store

This commit is contained in:
Illya Marchenko 2024-03-27 19:06:44 +02:00
parent 3c245cd38d
commit 20cb7a9ecc
Signed by: stuzer05
GPG Key ID: A6ABAAA9268F9F4F
7 changed files with 91 additions and 12 deletions

48
main.go

@ -42,7 +42,7 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) {
//w.WriteHeader(http.StatusOK) //w.WriteHeader(http.StatusOK)
//return //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)) LogString(string(body))
if len(string(body)) == 0 { if len(string(body)) == 0 {
@ -60,6 +60,18 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) {
return 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 // init firefly3 client
clientConf := firefly3.NewConfiguration() clientConf := firefly3.NewConfiguration()
clientConf.BasePath = os.Getenv("FIREFLY3_API_URL") clientConf.BasePath = os.Getenv("FIREFLY3_API_URL")
@ -102,7 +114,7 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) {
Type_: &transactionTypeWithdrawal, Type_: &transactionTypeWithdrawal,
Date: time.Unix(int64(transaction.Data.StatementItem.Time), 0).Add(time.Hour * 2), Date: time.Unix(int64(transaction.Data.StatementItem.Time), 0).Add(time.Hour * 2),
Notes: string(body), 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, SourceId: account.Id,
} }
@ -120,6 +132,8 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) {
for _, row := range ShopConfig { for _, row := range ShopConfig {
if slices.Contains(row.Names, transaction.Data.StatementItem.Description) || slices.Contains(row.MCCCodes, transaction.Data.StatementItem.Mcc) { if slices.Contains(row.Names, transaction.Data.StatementItem.Description) || slices.Contains(row.MCCCodes, transaction.Data.StatementItem.Mcc) {
firefly3Transaction.Description = row.TransactionDescription firefly3Transaction.Description = row.TransactionDescription
firefly3Transaction.DestinationName = row.TransactionDestination
firefly3Transaction.CategoryName = row.TransactionCategory
firefly3Transactions = append(firefly3Transactions, firefly3Transaction) firefly3Transactions = append(firefly3Transactions, firefly3Transaction)
break break
} }
@ -140,14 +154,18 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) {
if len(firefly3Transactions) > 0 { if len(firefly3Transactions) > 0 {
transactionOpts := firefly3.TransactionsApiStoreTransactionOpts{} transactionOpts := firefly3.TransactionsApiStoreTransactionOpts{}
_, _, err = client.TransactionsApi.StoreTransaction(ctx, firefly3.TransactionStore{
ApplyRules: true, for _, transaction := range firefly3Transactions {
Transactions: firefly3Transactions, _, _, err = client.TransactionsApi.StoreTransaction(ctx, firefly3.TransactionStore{
}, &transactionOpts) ApplyRules: true,
if err != nil { Transactions: []firefly3.TransactionSplitStore{transaction},
LogString(err.Error()) }, &transactionOpts)
w.WriteHeader(http.StatusOK) if err != nil {
return LogString(err.Error())
fmt.Println(err.Error())
w.WriteHeader(http.StatusOK)
return
}
} }
} }
@ -188,6 +206,16 @@ func main() {
TransactionDescription: "JetBrains: GoLand", 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 * Other
*/ */

@ -9,7 +9,7 @@ import (
func ClientInfo() (models2.ClientInfo, error) { func ClientInfo() (models2.ClientInfo, error) {
data := models2.ClientInfo{} 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 { if err != nil {
return data, err return data, err
} }

@ -0,0 +1,7 @@
package models
type StatementRequest struct {
Account string `json:"account"`
From int `json:"from"`
To int `json:"to"`
}

@ -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"`
}

@ -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
}

@ -21,7 +21,7 @@ func Request(method string, url string, data interface{}) (string, error) {
return "", err 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("Content-Type", "application/json")
r.Header.Add("X-Token", apiMonabankToken) r.Header.Add("X-Token", apiMonabankToken)

@ -4,4 +4,6 @@ type ShopConfigItem struct {
MCCCodes []int MCCCodes []int
Names []string Names []string
TransactionDescription string TransactionDescription string
TransactionDestination string
TransactionCategory string
} }