2024-03-27 22:28:01 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-03-30 16:23:50 +02:00
|
|
|
"bytes"
|
2024-03-27 22:28:01 +02:00
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2024-04-03 21:55:53 +03:00
|
|
|
"fmt"
|
2024-04-10 16:21:55 +03:00
|
|
|
firefly3 "gitea.stuzer.link/stuzer05/go-firefly3"
|
2024-03-30 16:23:50 +02:00
|
|
|
"github.com/antihax/optional"
|
2024-04-10 16:21:55 +03:00
|
|
|
monobank "github.com/vtopc/go-monobank"
|
2024-03-27 22:28:01 +02:00
|
|
|
"math"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"slices"
|
|
|
|
"strconv"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func handleWebhook(w http.ResponseWriter, r *http.Request) {
|
|
|
|
firefly3TransactionTypeWithdrawal := firefly3.WITHDRAWAL_TransactionTypeProperty
|
2024-03-29 22:18:37 +02:00
|
|
|
firefly3TransactionTypeDeposit := firefly3.DEPOSIT_TransactionTypeProperty
|
2024-03-27 22:28:01 +02:00
|
|
|
firefly3TransactionTypeTransfer := firefly3.TRANSFER_TransactionTypeProperty
|
|
|
|
|
|
|
|
// read request
|
2024-04-10 16:21:55 +03:00
|
|
|
monobankTransaction, err := readRequestBody(r)
|
2024-03-27 22:28:01 +02:00
|
|
|
if err != nil {
|
|
|
|
LogString(err.Error())
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// get body json string (for logging)
|
|
|
|
monobankTransactionJson, err := json.Marshal(monobankTransaction)
|
|
|
|
if err != nil {
|
|
|
|
LogString(err.Error())
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// read config
|
|
|
|
var config Config
|
|
|
|
config, err = ReadConfig(os.Getenv("CONFIG_PATH"))
|
|
|
|
if err != nil {
|
|
|
|
LogString(err.Error())
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// find accounts
|
2024-04-10 16:21:55 +03:00
|
|
|
account := ConfigGetAccountByMonobankId(config, monobankTransaction.Data.AccountID)
|
2024-03-27 22:28:01 +02:00
|
|
|
|
|
|
|
// cancel if one of account ids is empty
|
2024-04-03 21:55:53 +03:00
|
|
|
if len(account.Firefly3Name) == 0 || len(account.MonobankId) == 0 {
|
2024-04-10 16:21:55 +03:00
|
|
|
LogString("cannot find firefly3 or monobank ids (" + monobankTransaction.Data.AccountID + ")")
|
2024-03-27 22:28:01 +02:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// init firefly3 client
|
|
|
|
clientConf := firefly3.NewConfiguration()
|
|
|
|
clientConf.BasePath = os.Getenv("FIREFLY3_API_URL")
|
|
|
|
clientConf.AddDefaultHeader("Authorization", "Bearer "+os.Getenv("FIREFLY3_TOKEN"))
|
|
|
|
firefly3Client := firefly3.NewAPIClient(clientConf)
|
|
|
|
|
2024-03-30 16:23:50 +02:00
|
|
|
// create firefly3 transactions list
|
2024-03-27 22:28:01 +02:00
|
|
|
var firefly3Transactions []firefly3.TransactionSplitStore
|
|
|
|
|
2024-03-29 22:42:44 +02:00
|
|
|
// match transaction with config
|
|
|
|
for _, row := range config.TransactionTypes {
|
|
|
|
|
2024-03-30 16:23:50 +02:00
|
|
|
// is refund
|
2024-04-10 16:21:55 +03:00
|
|
|
if slices.Contains(row.NamesRefund, monobankTransaction.Data.Transaction.Description) {
|
2024-03-30 16:23:50 +02:00
|
|
|
opts := firefly3.TransactionsApiListTransactionOpts{
|
|
|
|
Limit: optional.NewInt32(999),
|
|
|
|
Type_: optional.NewInterface("withdrawal"),
|
|
|
|
Start: optional.NewString(time.Now().AddDate(0, 0, -7).Format("2006-01-02")), // one week before
|
|
|
|
}
|
|
|
|
oldTransactions, _, err := firefly3Client.TransactionsApi.ListTransaction(context.Background(), &opts)
|
|
|
|
if err != nil {
|
|
|
|
LogString(err.Error())
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// find matching transaction to delete
|
2024-03-30 16:49:51 +02:00
|
|
|
isDeleted := false
|
2024-03-30 16:23:50 +02:00
|
|
|
for _, tRows := range oldTransactions.Data {
|
2024-03-30 16:49:51 +02:00
|
|
|
if isDeleted {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2024-03-30 16:23:50 +02:00
|
|
|
for _, tRow := range tRows.Attributes.Transactions {
|
|
|
|
// validate notes is json
|
|
|
|
notesBytes := bytes.NewBufferString(tRow.Notes).Bytes()
|
|
|
|
if !json.Valid(notesBytes) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// read monobank transaction
|
|
|
|
var monobankTransaction monobank.Transaction
|
|
|
|
err = json.Unmarshal(notesBytes, &monobankTransaction)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// find transaction
|
2024-04-10 16:21:55 +03:00
|
|
|
sum := int(math.Abs(math.Round(float64(monobankTransaction.Amount/100)))) - int(math.Abs(math.Round(float64(monobankTransaction.CommissionRate/100))))
|
2024-03-30 16:23:50 +02:00
|
|
|
sum2, _ := strconv.ParseFloat(tRow.Amount, 64)
|
2024-04-10 16:21:55 +03:00
|
|
|
if slices.Contains(row.Names, monobankTransaction.Description) && sum == int(sum2) {
|
2024-03-30 16:23:50 +02:00
|
|
|
// delete transaction
|
|
|
|
opts := firefly3.TransactionsApiDeleteTransactionOpts{}
|
|
|
|
firefly3Client.TransactionsApi.DeleteTransaction(context.Background(), tRows.Id, &opts)
|
2024-03-30 16:49:51 +02:00
|
|
|
|
|
|
|
isDeleted = true
|
2024-03-30 16:23:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-29 22:42:44 +02:00
|
|
|
break
|
2024-03-30 16:23:50 +02:00
|
|
|
} else {
|
|
|
|
// check name & mcc
|
2024-04-10 16:21:55 +03:00
|
|
|
if !(slices.Contains(row.Names, monobankTransaction.Data.Transaction.Description) || slices.Contains(row.MccCodes, int(monobankTransaction.Data.Transaction.MCC))) {
|
2024-03-30 16:23:50 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// create firefly3 transaction
|
|
|
|
firefly3Transaction := firefly3.TransactionSplitStore{
|
2024-04-10 16:21:55 +03:00
|
|
|
Date: time.Unix(monobankTransaction.Data.Transaction.Time.Unix(), 0).Add(time.Hour * 2),
|
2024-04-03 21:55:53 +03:00
|
|
|
Notes: string(monobankTransactionJson),
|
2024-04-10 16:21:55 +03:00
|
|
|
Amount: strconv.Itoa(int(math.Abs(math.Round(float64(monobankTransaction.Data.Transaction.Amount/100)))) - int(math.Abs(math.Round(float64(monobankTransaction.Data.Transaction.CommissionRate/100))))),
|
2024-04-03 21:55:53 +03:00
|
|
|
SourceName: account.Firefly3Name,
|
2024-03-30 16:23:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// check max sum
|
|
|
|
sum, _ := strconv.Atoi(firefly3Transaction.Amount)
|
|
|
|
if row.SumMax > 0 && sum > row.SumMax {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// make transaction
|
|
|
|
switch row.Firefly3.Type {
|
|
|
|
case "withdrawal":
|
|
|
|
firefly3Transaction.Type_ = &firefly3TransactionTypeWithdrawal
|
|
|
|
break
|
|
|
|
case "deposit":
|
|
|
|
firefly3Transaction.Type_ = &firefly3TransactionTypeDeposit
|
|
|
|
break
|
|
|
|
case "transfer":
|
|
|
|
firefly3Transaction.Type_ = &firefly3TransactionTypeTransfer
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
firefly3Transaction.Type_ = &firefly3TransactionTypeWithdrawal
|
|
|
|
}
|
|
|
|
|
|
|
|
firefly3Transaction.Description = row.Firefly3.Description
|
|
|
|
firefly3Transaction.DestinationName = row.Firefly3.Destination
|
|
|
|
firefly3Transaction.CategoryName = row.Firefly3.Category
|
|
|
|
|
|
|
|
// swap source and destination
|
|
|
|
if row.Firefly3.IsUseDestinationAsSource {
|
|
|
|
firefly3Transaction.SourceName, firefly3Transaction.DestinationName = firefly3Transaction.DestinationName, firefly3Transaction.SourceName
|
|
|
|
}
|
|
|
|
|
|
|
|
firefly3Transactions = append(firefly3Transactions, firefly3Transaction)
|
2024-03-29 22:18:37 +02:00
|
|
|
break
|
2024-03-27 22:28:01 +02:00
|
|
|
}
|
|
|
|
}
|
2024-04-10 16:21:55 +03:00
|
|
|
if monobankTransaction.Data.Transaction.CommissionRate > 0 {
|
2024-03-27 22:28:01 +02:00
|
|
|
firefly3Transactions = append(firefly3Transactions, firefly3.TransactionSplitStore{
|
|
|
|
Type_: &firefly3TransactionTypeWithdrawal,
|
2024-04-10 16:21:55 +03:00
|
|
|
Date: time.Unix(monobankTransaction.Data.Transaction.Time.Unix(), 0).Add(time.Hour * 2),
|
2024-03-27 22:28:01 +02:00
|
|
|
Notes: string(monobankTransactionJson),
|
|
|
|
Description: "Transfer fee",
|
2024-04-10 16:21:55 +03:00
|
|
|
Amount: strconv.Itoa(int(math.Abs(math.Round(float64(monobankTransaction.Data.Transaction.CommissionRate / 100))))),
|
2024-04-03 21:55:53 +03:00
|
|
|
SourceName: account.Firefly3Name,
|
2024-03-27 22:28:01 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// log firefly3 transactions
|
|
|
|
if len(firefly3Transactions) > 0 {
|
|
|
|
transactionOpts := firefly3.TransactionsApiStoreTransactionOpts{}
|
|
|
|
|
|
|
|
for _, transaction := range firefly3Transactions {
|
|
|
|
_, _, err = firefly3Client.TransactionsApi.StoreTransaction(context.Background(), firefly3.TransactionStore{
|
|
|
|
ApplyRules: true,
|
|
|
|
Transactions: []firefly3.TransactionSplitStore{transaction},
|
|
|
|
}, &transactionOpts)
|
|
|
|
if err != nil {
|
2024-04-03 21:55:53 +03:00
|
|
|
fmt.Printf(err.Error())
|
2024-03-27 22:28:01 +02:00
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|