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

View File

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

View File

@ -52,6 +52,15 @@
"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"],
"firefly3": {

View File

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