Add support for swap source&destination

This commit is contained in:
Illya Marchenko 2024-03-29 22:42:44 +02:00
parent b5d81243e0
commit de4369997d
Signed by: stuzer05
GPG Key ID: A6ABAAA9268F9F4F
3 changed files with 51 additions and 44 deletions

@ -25,10 +25,11 @@ type ConfigTransactionTypes struct {
} }
type ConfigTransactionTypeFirefly3 struct { type ConfigTransactionTypeFirefly3 struct {
Type string `json:"type,omitempty"` Type string `json:"type,omitempty"`
Destination string `json:"destination,omitempty"` Destination string `json:"destination,omitempty"`
Description string `json:"description,omitempty"` Description string `json:"description,omitempty"`
Category string `json:"category,omitempty"` Category string `json:"category,omitempty"`
IsUseDestinationAsSource bool `json:"is_use_destination_as_source,omitempty"`
} }
func ReadConfig(path string) (Config, error) { func ReadConfig(path string) (Config, error) {

@ -52,6 +52,15 @@
"destination": "Mono White" "destination": "Mono White"
} }
}, },
{
"names": ["Термінал EasyPay", "City24", "Термінал City24"],
"firefly3": {
"type": "transfer",
"description": "Transfer between accounts",
"destination": "Wallet cash (UAH)",
"is_use_destination_as_source": true
}
},
{ {
"names": ["Novapay"], "names": ["Novapay"],
"firefly3": { "firefly3": {

@ -72,47 +72,44 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) {
SourceId: account.Firefly3Id, SourceId: account.Firefly3Id,
} }
// Special transaction cases // match transaction with config
if slices.Contains([]string{"Термінал EasyPay", "City24", "Термінал City24"}, monobankTransaction.Data.StatementItem.Description) { for _, row := range config.TransactionTypes {
firefly3Transaction.Type_ = &firefly3TransactionTypeTransfer // check name & mcc
firefly3Transaction.Description = "Transfer between accounts" if !(slices.Contains(row.Names, monobankTransaction.Data.StatementItem.Description) || slices.Contains(row.MccCodes, monobankTransaction.Data.StatementItem.Mcc)) {
firefly3Transaction.SourceName = "Wallet cash (UAH)" // test continue
firefly3Transaction.DestinationId = account.Firefly3Id // test
firefly3Transactions = append(firefly3Transactions, firefly3Transaction)
} else {
for _, row := range config.TransactionTypes {
// check name & mcc
if !(slices.Contains(row.Names, monobankTransaction.Data.StatementItem.Description) || slices.Contains(row.MccCodes, monobankTransaction.Data.StatementItem.Mcc)) {
continue
}
// 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
firefly3Transactions = append(firefly3Transactions, firefly3Transaction)
break
} }
// 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
firefly3Transactions = append(firefly3Transactions, firefly3Transaction)
// swap source and destination
if row.Firefly3.IsUseDestinationAsSource {
firefly3Transaction.SourceName, firefly3Transaction.DestinationName = firefly3Transaction.DestinationName, firefly3Transaction.SourceName
}
break
} }
// record transfer fee // record transfer fee