Update library

This commit is contained in:
Illya Marchenko 2024-05-12 12:35:18 +03:00
parent 29a5248974
commit 00968aee30
Signed by: stuzer05
GPG Key ID: A6ABAAA9268F9F4F
2 changed files with 12 additions and 17 deletions

@ -43,7 +43,7 @@ func ImportTransaction(monobankTransaction monobank.WebHookResponse) error {
for _, row := range App().Config.TransactionTypes { for _, row := range App().Config.TransactionTypes {
// is refund // is refund
if slices.Contains(row.NamesRefund, *monobankTransaction.Data.StatementItem.Description) { if slices.Contains(row.NamesRefund, monobankTransaction.Data.StatementItem.Description) {
opts := firefly3.TransactionsApiListTransactionOpts{ opts := firefly3.TransactionsApiListTransactionOpts{
Limit: optional.NewInt32(999), Limit: optional.NewInt32(999),
Type_: optional.NewInterface("withdrawal"), Type_: optional.NewInterface("withdrawal"),
@ -76,9 +76,9 @@ func ImportTransaction(monobankTransaction monobank.WebHookResponse) error {
} }
// find transaction // find transaction
sum := int(math.Abs(math.Round(float64(*monobankTransaction.Amount/100)))) - int(math.Abs(math.Round(float64(*monobankTransaction.CommissionRate/100)))) sum := int(math.Abs(math.Round(monobankTransaction.Amount/100))) - int(math.Abs(math.Round(monobankTransaction.CommissionRate/100)))
sum2, _ := strconv.ParseFloat(tRow.Amount, 64) sum2, _ := strconv.ParseFloat(tRow.Amount, 64)
if slices.Contains(row.Names, *monobankTransaction.Description) && sum == int(sum2) { if slices.Contains(row.Names, monobankTransaction.Description) && sum == int(sum2) {
// delete transaction // delete transaction
opts := firefly3.TransactionsApiDeleteTransactionOpts{} opts := firefly3.TransactionsApiDeleteTransactionOpts{}
App().Firefly3Client.TransactionsApi.DeleteTransaction(context.Background(), tRows.Id, &opts) App().Firefly3Client.TransactionsApi.DeleteTransaction(context.Background(), tRows.Id, &opts)
@ -90,15 +90,15 @@ func ImportTransaction(monobankTransaction monobank.WebHookResponse) error {
break break
} else { } else {
// check name & mcc // check name & mcc
if !(slices.Contains(row.Names, *monobankTransaction.Data.StatementItem.Description) || slices.Contains(row.MccCodes, int(*monobankTransaction.Data.StatementItem.Mcc))) { if !(slices.Contains(row.Names, monobankTransaction.Data.StatementItem.Description) || slices.Contains(row.MccCodes, int(monobankTransaction.Data.StatementItem.Mcc))) {
continue continue
} }
// create firefly3 transaction // create firefly3 transaction
firefly3Transaction := firefly3.TransactionSplitStore{ firefly3Transaction := firefly3.TransactionSplitStore{
Date: time.Unix(int64(*monobankTransaction.Data.StatementItem.Time), 0).Add(time.Hour * time.Duration(timezoneHoursDiff)), Date: time.Unix(int64(monobankTransaction.Data.StatementItem.Time), 0).Add(time.Hour * time.Duration(timezoneHoursDiff)),
Notes: string(monobankTransactionJson), 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))))), Amount: strconv.Itoa(int(math.Abs(math.Round(monobankTransaction.Data.StatementItem.Amount/100))) - int(math.Abs(math.Round(monobankTransaction.Data.StatementItem.CommissionRate/100)))),
SourceName: account.Firefly3Name, SourceName: account.Firefly3Name,
} }
@ -135,13 +135,13 @@ func ImportTransaction(monobankTransaction monobank.WebHookResponse) error {
break break
} }
} }
if *monobankTransaction.Data.StatementItem.CommissionRate > 0 { if monobankTransaction.Data.StatementItem.CommissionRate > 0 {
firefly3Transactions = append(firefly3Transactions, firefly3.TransactionSplitStore{ firefly3Transactions = append(firefly3Transactions, firefly3.TransactionSplitStore{
Type_: &firefly3TransactionTypeWithdrawal, Type_: &firefly3TransactionTypeWithdrawal,
Date: time.Unix(int64(*monobankTransaction.Data.StatementItem.Time), 0).Add(time.Hour * time.Duration(timezoneHoursDiff)), Date: time.Unix(int64(monobankTransaction.Data.StatementItem.Time), 0).Add(time.Hour * time.Duration(timezoneHoursDiff)),
Notes: string(monobankTransactionJson), Notes: string(monobankTransactionJson),
Description: "Transfer fee", Description: "Transfer fee",
Amount: strconv.Itoa(int(math.Abs(math.Round(float64(*monobankTransaction.Data.StatementItem.CommissionRate / 100))))), Amount: strconv.Itoa(int(math.Abs(math.Round(monobankTransaction.Data.StatementItem.CommissionRate / 100)))),
SourceName: account.Firefly3Name, SourceName: account.Firefly3Name,
}) })
} }

11
main.go

@ -35,16 +35,14 @@ func main() {
// manual transaction // manual transaction
if *flagMonobankListAccounts { if *flagMonobankListAccounts {
// get monobank accounts // get monobank accounts
req := monobank.ApiPersonalClientInfoGetRequest{} res, _, err := app.App().MonobankClient.Api.PersonalClientInfoGet(context.Background(), os.Getenv("MONOBANK_TOKEN"))
req = req.XToken(os.Getenv("MONOBANK_TOKEN"))
res, _, err := app.App().MonobankClient.DefaultApi.PersonalClientInfoGetExecute(req)
if err != nil { if err != nil {
log.Fatalln(err.Error()) log.Fatalln(err.Error())
} }
// list accounts // list accounts
for _, row := range res.Accounts { for _, row := range res.Accounts {
fmt.Printf("%v\t%v\n", *row.Id, (*row.MaskedPan)[0]) fmt.Printf("%v\t%v\n", row.Id, (*row.MaskedPan)[0])
} }
} else if *flagFirefly3ListAccounts { } else if *flagFirefly3ListAccounts {
// get firefly3 accounts // get firefly3 accounts
@ -81,10 +79,7 @@ func main() {
webhookUrl := fmt.Sprintf("https://%s/webhook/%s", os.Getenv("MONOBANK_WEBHOOK_DOMAIN"), os.Getenv("MONOBANK_WEBHOOK_SECRET")) webhookUrl := fmt.Sprintf("https://%s/webhook/%s", os.Getenv("MONOBANK_WEBHOOK_DOMAIN"), os.Getenv("MONOBANK_WEBHOOK_SECRET"))
// register monobank webhook // register monobank webhook
req := monobank.ApiPersonalWebhookPostRequest{} _, err := app.App().MonobankClient.Api.PersonalWebhookPost(context.Background(), monobank.SetWebHook{WebHookUrl: webhookUrl}, os.Getenv("MONOBANK_TOKEN"))
req = req.XToken(os.Getenv("MONOBANK_TOKEN"))
req = req.SetWebHook(monobank.SetWebHook{WebHookUrl: &webhookUrl})
_, err := app.App().MonobankClient.DefaultApi.PersonalWebhookPostExecute(req)
if err != nil { if err != nil {
log.Fatalln("failed to register monobank webhook") log.Fatalln("failed to register monobank webhook")
} }