Fix ATM transfer between accounts support
This commit is contained in:
parent
876edee3a7
commit
37e1445f3f
15
config.go
15
config.go
@ -12,9 +12,8 @@ type Config struct {
|
||||
}
|
||||
|
||||
type ConfigAccount struct {
|
||||
Name string `json:"name"`
|
||||
Firefly3Id string `json:"firefly3_id,omitempty"`
|
||||
MonobankId string `json:"monobank_id,omitempty"`
|
||||
Firefly3Name string `json:"firefly3_name,omitempty"`
|
||||
MonobankId string `json:"monobank_id,omitempty"`
|
||||
}
|
||||
|
||||
type ConfigTransactionTypes struct {
|
||||
@ -57,16 +56,6 @@ func ReadConfig(path string) (Config, error) {
|
||||
return config, nil
|
||||
}
|
||||
|
||||
func ConfigGetAccountByName(config Config, q string) ConfigAccount {
|
||||
for _, row := range config.Accounts {
|
||||
if row.Name == q {
|
||||
return row
|
||||
}
|
||||
}
|
||||
|
||||
return ConfigAccount{}
|
||||
}
|
||||
|
||||
func ConfigGetAccountByMonobankId(config Config, q string) ConfigAccount {
|
||||
for _, row := range config.Accounts {
|
||||
if row.MonobankId == q {
|
||||
|
15
webhook.go
15
webhook.go
@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"github.com/antihax/optional"
|
||||
"main/firefly3"
|
||||
monobank "main/monobank/api/webhook/models"
|
||||
@ -52,7 +53,7 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) {
|
||||
account := ConfigGetAccountByMonobankId(config, monobankTransaction.Data.Account)
|
||||
|
||||
// cancel if one of account ids is empty
|
||||
if len(account.Firefly3Id) == 0 || len(account.MonobankId) == 0 {
|
||||
if len(account.Firefly3Name) == 0 || len(account.MonobankId) == 0 {
|
||||
LogString("cannot find firefly3 or monobank ids (" + monobankTransaction.Data.Account + ")")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
return
|
||||
@ -126,10 +127,10 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// create firefly3 transaction
|
||||
firefly3Transaction := firefly3.TransactionSplitStore{
|
||||
Date: time.Unix(int64(monobankTransaction.Data.StatementItem.Time), 0).Add(time.Hour * 2),
|
||||
Notes: string(monobankTransactionJson),
|
||||
Amount: strconv.Itoa(int(math.Abs(math.Round(float64(monobankTransaction.Data.StatementItem.Amount/100)))) - int(math.Abs(math.Round(float64(monobankTransaction.Data.StatementItem.CommissionRate/100))))),
|
||||
SourceId: account.Firefly3Id,
|
||||
Date: time.Unix(int64(monobankTransaction.Data.StatementItem.Time), 0).Add(time.Hour * 2),
|
||||
Notes: string(monobankTransactionJson),
|
||||
Amount: strconv.Itoa(int(math.Abs(math.Round(float64(monobankTransaction.Data.StatementItem.Amount/100)))) - int(math.Abs(math.Round(float64(monobankTransaction.Data.StatementItem.CommissionRate/100))))),
|
||||
SourceName: account.Firefly3Name,
|
||||
}
|
||||
|
||||
// check max sum
|
||||
@ -172,7 +173,7 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) {
|
||||
Notes: string(monobankTransactionJson),
|
||||
Description: "Transfer fee",
|
||||
Amount: strconv.Itoa(int(math.Abs(math.Round(float64(monobankTransaction.Data.StatementItem.CommissionRate / 100))))),
|
||||
SourceId: account.Firefly3Id,
|
||||
SourceName: account.Firefly3Name,
|
||||
})
|
||||
}
|
||||
|
||||
@ -186,7 +187,7 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) {
|
||||
Transactions: []firefly3.TransactionSplitStore{transaction},
|
||||
}, &transactionOpts)
|
||||
if err != nil {
|
||||
LogString(err.Error())
|
||||
fmt.Printf(err.Error())
|
||||
w.WriteHeader(http.StatusOK)
|
||||
return
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user