You've already forked monobank-firefly3-bot
Add duplicate transaction check from log and transaction description LIKE match
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
Reference in New Issue
Block a user