From 876edee3a7817f3e96e2b90b60ac48e3cd3a8c0a Mon Sep 17 00:00:00 2001 From: stuzer05 Date: Sat, 30 Mar 2024 16:49:51 +0200 Subject: [PATCH] Add "--monobank-transaction" command --- http.go | 5 ----- main.go | 43 ++++++++++++++++++++++++++++++++++++------- webhook.go | 7 +++++++ 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/http.go b/http.go index d3a42e7..b8247b4 100644 --- a/http.go +++ b/http.go @@ -14,11 +14,6 @@ func readResponseBody(r *http.Request) (monobank.Transaction, error) { return monobank.Transaction{}, err } - //fmt.Println(string(body)) - //w.WriteHeader(http.StatusOK) - //return - - //body = []byte("{\"type\":\"StatementItem\",\"data\":{\"account\":\"4723djMLsLOCzhoeYjxqRw\",\"statementItem\":{\"id\":\"cSMr2xlFsfWPFeDLTg\",\"time\":1711806353,\"description\":\"Скасування. Bolt\",\"mcc\":4121,\"originalMcc\":4121,\"amount\":10300,\"operationAmount\":10300,\"currencyCode\":980,\"commissionRate\":0,\"cashbackAmount\":0,\"balance\":7974533,\"hold\":false}}}") LogString(string(body)) // check empty body diff --git a/main.go b/main.go index 651d685..4850252 100644 --- a/main.go +++ b/main.go @@ -1,11 +1,15 @@ package main import ( + "flag" "fmt" "github.com/joho/godotenv" + "io" "log" "net/http" + "net/http/httptest" "os" + "strings" ) // https://api.monobank.ua/docs/index.html#tag/Kliyentski-personalni-dani/paths/~1personal~1statement~1{account}~1{from}~1{to}/get @@ -29,13 +33,38 @@ func main() { return } - // set webhook - http.HandleFunc("/webhook", handleWebhook) + // flags + flagDoTransaction := flag.String("monobank-transaction", "", "run monobank transaction JSON manually") - // listen server - fmt.Println("Webhook server listening on " + os.Getenv("LISTEN")) - err = http.ListenAndServe(os.Getenv("LISTEN"), nil) - if err != nil { - panic(err.Error()) + flag.Parse() + + // manual transaction + if len(*flagDoTransaction) > 0 { + w := httptest.NewRecorder() + + r := &http.Request{ + Method: http.MethodPost, + Header: make(http.Header), + Body: io.NopCloser(strings.NewReader(*flagDoTransaction)), + } + r.Header.Set("Content-Type", "application/json") + + handleWebhook(w, r) + + // @todo error logging + //response := w.Result() + //if response.StatusCode != http.StatusOK { + // os.Exit(1) + //} + } else { + // set webhook + http.HandleFunc("/webhook", handleWebhook) + + // listen server + fmt.Println("Webhook server listening on " + os.Getenv("LISTEN")) + err = http.ListenAndServe(os.Getenv("LISTEN"), nil) + if err != nil { + panic(err.Error()) + } } } diff --git a/webhook.go b/webhook.go index 84a666c..23bca8e 100644 --- a/webhook.go +++ b/webhook.go @@ -85,7 +85,12 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) { } // find matching transaction to delete + isDeleted := false for _, tRows := range oldTransactions.Data { + if isDeleted { + break + } + for _, tRow := range tRows.Attributes.Transactions { // validate notes is json notesBytes := bytes.NewBufferString(tRow.Notes).Bytes() @@ -107,6 +112,8 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) { // delete transaction opts := firefly3.TransactionsApiDeleteTransactionOpts{} firefly3Client.TransactionsApi.DeleteTransaction(context.Background(), tRows.Id, &opts) + + isDeleted = true } } }