Add duplicate transaction check from log and transaction description LIKE match

This commit is contained in:
2024-08-28 22:41:05 +03:00
parent e1c9f56921
commit fb7796c475
5 changed files with 88 additions and 15 deletions

View File

@ -12,6 +12,7 @@ import (
"os"
"slices"
"strconv"
"strings"
"time"
)
@ -28,6 +29,15 @@ func ImportTransaction(monobankTransaction monobank.WebHookResponse) error {
return err
}
// check if transaction hs been logged
isTransactionAlreadyLogged, err := LogContainsTransactionID(monobankTransaction.Data.StatementItem.Id)
if err != nil {
return err
}
if isTransactionAlreadyLogged {
return nil
}
// find accounts
destAccount := App().Config.GetAccountByMonobankId(monobankTransaction.Data.Account)
@ -92,8 +102,21 @@ func ImportTransaction(monobankTransaction monobank.WebHookResponse) error {
}
break
} else {
// check name match
isDescriptionMatch := false
if row.NamesLooseMatch {
for _, name := range row.Names {
if strings.HasPrefix(monobankTransaction.Data.StatementItem.Description, name) {
isDescriptionMatch = true
break
}
}
} else {
isDescriptionMatch = slices.Contains(row.Names, monobankTransaction.Data.StatementItem.Description)
}
// check name & mcc
if !(slices.Contains(row.Names, monobankTransaction.Data.StatementItem.Description) || slices.Contains(row.MccCodes, int(monobankTransaction.Data.StatementItem.Mcc))) {
if !(isDescriptionMatch || slices.Contains(row.MccCodes, int(monobankTransaction.Data.StatementItem.Mcc))) {
continue
}

View File

@ -1,6 +1,8 @@
package app
import (
"bufio"
"encoding/json"
"fmt"
"os"
)
@ -20,3 +22,49 @@ func LogString(str string) {
fmt.Println(err)
}
}
func LogContainsTransactionID(transactionID string) (bool, error) {
if len(os.Getenv("LOG_FILE")) == 0 {
return false, nil
}
// open the log file for reading.
logFile, err := os.Open(os.Getenv("LOG_FILE"))
if err != nil {
return false, fmt.Errorf("error opening log file: %w", err)
}
defer logFile.Close()
// create a new scanner to read the log file line by line.
scanner := bufio.NewScanner(logFile)
// iterate over each line of the log file.
for scanner.Scan() {
// unmarshal the JSON data from the current line.
var transactionData struct {
Data struct {
StatementItem struct {
ID string `json:"id"`
} `json:"statementItem"`
} `json:"data"`
}
err := json.Unmarshal(scanner.Bytes(), &transactionData)
if err != nil {
// skip lines that are not valid JSON.
continue
}
// check if the transaction ID matches the given ID.
if transactionData.Data.StatementItem.ID == transactionID {
return true, nil
}
}
// check for any errors that occurred during scanning.
if err := scanner.Err(); err != nil {
return false, fmt.Errorf("error scanning log file: %w", err)
}
// transaction ID not found in the log file.
return false, nil
}