This commit is contained in:
Illya Marchenko 2024-03-26 11:26:35 +02:00
parent 87d9fb261b
commit 9bcacf0a01
Signed by: stuzer05
GPG Key ID: A6ABAAA9268F9F4F

186
main.go

@ -10,6 +10,7 @@ import (
"log" "log"
"main/firefly3" "main/firefly3"
"main/monobank/api/webhook/models" "main/monobank/api/webhook/models"
"math"
"net/http" "net/http"
"os" "os"
"strconv" "strconv"
@ -23,13 +24,29 @@ import (
// curl -X POST https://monobank-firefly3.stuzer.link/webhook -H 'Content-Type: application/json' -d '{"test":123}' // curl -X POST https://monobank-firefly3.stuzer.link/webhook -H 'Content-Type: application/json' -d '{"test":123}'
func logString(str string) {
if len(os.Getenv("LOG_FILE")) == 0 {
return
}
f, err := os.OpenFile(os.Getenv("LOG_FILE"), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println(err)
}
defer f.Close()
if _, err := f.WriteString(str + "\n"); err != nil {
fmt.Println(err)
}
}
func handleWebhook(w http.ResponseWriter, r *http.Request) { func handleWebhook(w http.ResponseWriter, r *http.Request) {
fmt.Println("Webhook received!") logString("-----------------\nwebhook received!")
// read body bytes // read body bytes
body, err := io.ReadAll(r.Body) body, err := io.ReadAll(r.Body)
if err != nil { if err != nil {
fmt.Println(err) logString(err.Error())
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
return return
} }
@ -38,48 +55,40 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) {
//w.WriteHeader(http.StatusOK) //w.WriteHeader(http.StatusOK)
//return //return
//body := []byte("{\"type\":\"StatementItem\",\"data\":{\"account\":\"jJPAm0cfwAJv3C0I-kYpTA\",\"statementItem\":{\"id\":\"-YdIgUpWDogXEMSceQ\",\"time\":1711354414,\"description\":\"З чорної картки\",\"mcc\":4829,\"originalMcc\":4829,\"amount\":100,\"operationAmount\":100,\"currencyCode\":980,\"commissionRate\":0,\"cashbackAmount\":0,\"balance\":100,\"hold\":true}}}") //body = []byte("{\"type\":\"StatementItem\",\"data\":{\"account\":\"jJPAm0cfwAJv3C0I-kYpTA\",\"statementItem\":{\"id\":\"-YdIgUpWDogXEMSceQ\",\"time\":1711354414,\"description\":\"З чорної картки\",\"mcc\":4829,\"originalMcc\":4829,\"amount\":100,\"operationAmount\":100,\"currencyCode\":980,\"commissionRate\":0,\"cashbackAmount\":0,\"balance\":100,\"hold\":true}}}")
logString(string(body))
// log body
f, err := os.OpenFile("/tmp/monobank-filefly3.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
fmt.Println(err)
}
defer f.Close()
if _, err := f.WriteString(string(body) + "\n"); err != nil {
fmt.Println(err)
}
// parse body // parse body
var transaction models.Transaction var transaction models.Transaction
err = json.Unmarshal(body, &transaction) err = json.Unmarshal(body, &transaction)
if err != nil { if err != nil {
fmt.Println(err) logString(err.Error())
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
return return
} }
// init firefly3 client // init firefly3 client
clientConf := firefly3.NewConfiguration() clientConf := firefly3.NewConfiguration()
clientConf.BasePath = "https://firefly3.stuzer.link/api" clientConf.BasePath = os.Getenv("FIREFLY3_API_URL")
clientConf.AddDefaultHeader("Authorization", "Bearer "+os.Getenv("FIREFLY3_TOKEN")) clientConf.AddDefaultHeader("Authorization", "Bearer "+os.Getenv("FIREFLY3_TOKEN"))
client := firefly3.NewAPIClient(clientConf) client := firefly3.NewAPIClient(clientConf)
ctx := context.Background() ctx := context.Background()
// process transaction
transactionTypeWithdrawal := firefly3.WITHDRAWAL_TransactionTypeProperty transactionTypeWithdrawal := firefly3.WITHDRAWAL_TransactionTypeProperty
transactionTypeTransfer := firefly3.TRANSFER_TransactionTypeProperty transactionTypeTransfer := firefly3.TRANSFER_TransactionTypeProperty
var transactionList []firefly3.TransactionSplitStore var transactionList []firefly3.TransactionSplitStore
// process transaction // get firefly3 account
// get account
listOpts := firefly3.AccountsApiListAccountOpts{ listOpts := firefly3.AccountsApiListAccountOpts{
Type_: optional.NewInterface("asset"), Type_: optional.NewInterface("asset"),
} }
accounts, _, err := client.AccountsApi.ListAccount(ctx, &listOpts) accounts, _, err := client.AccountsApi.ListAccount(ctx, &listOpts)
if err != nil { if err != nil {
log.Fatalf(err.Error()) logString(err.Error())
w.WriteHeader(http.StatusOK)
return
} }
var account firefly3.AccountRead var account firefly3.AccountRead
@ -90,34 +99,30 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) {
} }
if len(account.Id) == 0 { if len(account.Id) == 0 {
fmt.Printf("unable to find account %s in firefly3\n", transaction.Data.Account) logString("unable to find account " + transaction.Data.Account + " in firefly3")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
return return
} }
t := firefly3.TransactionSplitStore{
Type_: &transactionTypeWithdrawal,
Date: time.Now(), // time.Unix(int64(time.Now()), 0)
Notes: string(body),
Amount: strconv.Itoa(int(math.Round(float64(transaction.Data.StatementItem.Amount / 100)))),
SourceId: account.Id,
}
// create transaction // create transaction
switch transaction.Data.StatementItem.Description { switch transaction.Data.StatementItem.Description {
case "З чорної картки": case "З чорної картки":
transactionList = append(transactionList, firefly3.TransactionSplitStore{ t.Type_ = &transactionTypeTransfer
Type_: &transactionTypeTransfer, t.Description = "Transfer between accounts"
Date: time.Now(), // time.Unix(int64(time.Now()), 0) t.DestinationId = "60"
Amount: strconv.Itoa(transaction.Data.StatementItem.Amount / 100),
Description: "Transfer between accounts",
Notes: string(body),
SourceId: account.Id,
DestinationId: "60",
})
break break
case "З білої картки": case "З білої картки":
transactionList = append(transactionList, firefly3.TransactionSplitStore{ t.Type_ = &transactionTypeTransfer
Type_: &transactionTypeTransfer, t.Description = "Transfer between accounts"
Date: time.Now(), // time.Unix(int64(time.Now()), 0) t.DestinationId = "1"
Amount: strconv.Itoa(transaction.Data.StatementItem.Amount / 100),
Description: "Transfer between accounts",
Notes: string(body),
SourceId: account.Id,
DestinationId: "1",
})
break break
case "АТБ": case "АТБ":
case "Велмарт": case "Велмарт":
@ -125,14 +130,7 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) {
case "Glovo": case "Glovo":
case "zakaz.ua": case "zakaz.ua":
case "Мегамаркет": case "Мегамаркет":
transactionList = append(transactionList, firefly3.TransactionSplitStore{ t.Description = "Groceries"
Type_: &transactionTypeWithdrawal,
Date: time.Now(), // time.Unix(int64(time.Now()), 0)
Amount: strconv.Itoa(transaction.Data.StatementItem.Amount / 100),
Description: "Groceries",
Notes: string(body),
SourceId: account.Id,
})
break break
case "Аптека Доброго Дня": case "Аптека Доброго Дня":
case "Аптека оптових цін": case "Аптека оптових цін":
@ -140,113 +138,45 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) {
case "Аптека Гала": case "Аптека Гала":
case "Аптека АНЦ": case "Аптека АНЦ":
case "APTEKA 7": case "APTEKA 7":
transactionList = append(transactionList, firefly3.TransactionSplitStore{ t.Description = "Medications"
Type_: &transactionTypeWithdrawal,
Date: time.Now(), // time.Unix(int64(time.Now()), 0)
Amount: strconv.Itoa(transaction.Data.StatementItem.Amount / 100),
Description: "Medications",
Notes: string(body),
SourceId: account.Id,
})
break break
case "Київ Цифровий": case "Київ Цифровий":
case "Київпастранс": case "Київпастранс":
transactionList = append(transactionList, firefly3.TransactionSplitStore{ t.Description = "Public transport"
Type_: &transactionTypeWithdrawal,
Date: time.Now(), // time.Unix(int64(time.Now()), 0)
Amount: strconv.Itoa(transaction.Data.StatementItem.Amount / 100),
Description: "Public transport",
Notes: string(body),
SourceId: account.Id,
})
break break
case "McDonald's": case "McDonald's":
transactionList = append(transactionList, firefly3.TransactionSplitStore{ t.Description = "McDonalds"
Type_: &transactionTypeWithdrawal,
Date: time.Now(), // time.Unix(int64(time.Now()), 0)
Amount: strconv.Itoa(transaction.Data.StatementItem.Amount / 100),
Description: "McDonalds",
Notes: string(body),
SourceId: account.Id,
})
break break
case "LeoCafe": case "LeoCafe":
transactionList = append(transactionList, firefly3.TransactionSplitStore{ t.Description = "Cafe"
Type_: &transactionTypeWithdrawal,
Date: time.Now(), // time.Unix(int64(time.Now()), 0)
Amount: strconv.Itoa(transaction.Data.StatementItem.Amount / 100),
Description: "Cafe",
Notes: string(body),
SourceId: account.Id,
})
break break
case "Mafia": case "Mafia":
transactionList = append(transactionList, firefly3.TransactionSplitStore{ t.Description = "Restaurant"
Type_: &transactionTypeWithdrawal,
Date: time.Now(), // time.Unix(int64(time.Now()), 0)
Amount: strconv.Itoa(transaction.Data.StatementItem.Amount / 100),
Description: "Restaurant",
Notes: string(body),
SourceId: account.Id,
})
break break
case "Lumberjack Barberhouse": case "Lumberjack Barberhouse":
transactionList = append(transactionList, firefly3.TransactionSplitStore{ t.Description = "Lumberjack: haircut"
Type_: &transactionTypeWithdrawal,
Date: time.Now(), // time.Unix(int64(time.Now()), 0)
Amount: strconv.Itoa(transaction.Data.StatementItem.Amount / 100),
Description: "Lumberjack: haircut",
Notes: string(body),
SourceId: account.Id,
})
break break
case "Hetzner": case "Hetzner":
transactionList = append(transactionList, firefly3.TransactionSplitStore{ t.Description = "Hetzner: vps2"
Type_: &transactionTypeWithdrawal,
Date: time.Now(), // time.Unix(int64(time.Now()), 0)
Amount: strconv.Itoa(transaction.Data.StatementItem.Amount / 100),
Description: "Hetzner: vps2",
Notes: string(body),
SourceId: account.Id,
})
case "YouTube": case "YouTube":
transactionList = append(transactionList, firefly3.TransactionSplitStore{ t.Description = "YouTube membership: Latte ASMR"
Type_: &transactionTypeWithdrawal,
Date: time.Now(), // time.Unix(int64(time.Now()), 0)
Amount: strconv.Itoa(transaction.Data.StatementItem.Amount / 100),
Description: "YouTube membership: Latte ASMR",
Notes: string(body),
SourceId: account.Id,
})
case "Київстар +380672463500": case "Київстар +380672463500":
transactionList = append(transactionList, firefly3.TransactionSplitStore{ t.Description = "Kyivstar: +380672463500"
Type_: &transactionTypeWithdrawal,
Date: time.Now(), // time.Unix(int64(time.Now()), 0)
Amount: strconv.Itoa(transaction.Data.StatementItem.Amount / 100),
Description: "Kyivstar: +380672463500",
Notes: string(body),
SourceId: account.Id,
})
case "Lifecell +380732463500": case "Lifecell +380732463500":
transactionList = append(transactionList, firefly3.TransactionSplitStore{ t.Description = "Lifecell: +380732463500"
Type_: &transactionTypeWithdrawal,
Date: time.Now(), // time.Unix(int64(time.Now()), 0)
Amount: strconv.Itoa(transaction.Data.StatementItem.Amount / 100),
Description: "Lifecell: +380732463500",
Notes: string(body),
SourceId: account.Id,
})
break break
} }
if len(transactionList) > 0 { if len(t.Description) > 0 {
transactionList = append(transactionList, t)
transactionOpts := firefly3.TransactionsApiStoreTransactionOpts{} transactionOpts := firefly3.TransactionsApiStoreTransactionOpts{}
_, _, err = client.TransactionsApi.StoreTransaction(ctx, firefly3.TransactionStore{ _, _, err = client.TransactionsApi.StoreTransaction(ctx, firefly3.TransactionStore{
ApplyRules: true, ApplyRules: true,
Transactions: transactionList, Transactions: transactionList,
}, &transactionOpts) }, &transactionOpts)
if err != nil { if err != nil {
fmt.Println(err.Error()) logString(err.Error())
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
return return
} }