From 5ac140d3c2ea1b8b4284ee4c45ccd758806baf2a Mon Sep 17 00:00:00 2001 From: stuzer05 Date: Wed, 27 Mar 2024 21:41:17 +0200 Subject: [PATCH] Refactor --- config.go | 11 ++-- config.json | 143 -------------------------------------------- config.json.example | 10 +++- main.go | 41 +++++-------- 4 files changed, 30 insertions(+), 175 deletions(-) delete mode 100644 config.json diff --git a/config.go b/config.go index d3cce59..605c6e7 100644 --- a/config.go +++ b/config.go @@ -18,15 +18,16 @@ type ConfigAccount struct { } type ConfigTransactionTypes struct { - Names []string `json:"names"` - Firefly3 ConfigTransactionTypeFirefly3 `json:"firefly3,omitempty"` + Names []string `json:"names,omitempty"` MccCodes []int `json:"mcc_codes,omitempty"` + Firefly3 ConfigTransactionTypeFirefly3 `json:"firefly3,omitempty"` } type ConfigTransactionTypeFirefly3 struct { - Description string `json:"description"` - Destination string `json:"destination"` - Category string `json:"category"` + Type string `json:"type,omitempty"` + Destination string `json:"destination,omitempty"` + Description string `json:"description,omitempty"` + Category string `json:"category,omitempty"` } func ReadConfig(path string) (Config, error) { diff --git a/config.json b/config.json deleted file mode 100644 index 19e0673..0000000 --- a/config.json +++ /dev/null @@ -1,143 +0,0 @@ -{ - "accounts": [ - { - "name": "Mono Black", - "firefly3_id": "1", - "monobank_id": "4723djMLsLOCzhoeYjxqRw" - }, - { - "name": "Mono White", - "firefly3_id": "1", - "monobank_id": "jJPAm0cfwAJv3C0I-kYpTA" - }, - { - "name": "Wallet cash (UAH)", - "firefly3_id": "4" - } - ], - "transaction_types": [ - { - "names": ["Hetzner"], - "firefly3": { - "description": "Hetzner: vps2" - } - }, - { - "names": ["YouTube"], - "firefly3": { - "description": "YouTube membership: Latte ASMR" - } - }, - { - "names": ["Київстар +380672463500"], - "firefly3": { - "description": "Kyivstar: +380672463500" - } - }, - { - "names": ["Lifecell +380732463500"], - "firefly3": { - "description": "Lifecell: +380732463500" - } - }, - { - "names": ["JetBrains"], - "firefly3": { - "description": "JetBrains: GoLand" - } - }, - { - "names": ["Telegram"], - "firefly3": { - "description": "Telegram premium" - } - }, - { - "names": ["Платіж Tefal Initial+"], - "firefly3": { - "description": "Credit payment: TEFAL OptiGrill+ Initial GC706D34", - "destination": "Credit: TEFAL OptiGrill+ Initial GC706D34" - } - }, - { - "names": ["Платіж Dyson"], - "firefly3": { - "description": "Credit payment: Dyson Supersonic HD07 Nickel/Copper", - "destination": "Credit: Dyson Supersonic HD07 Nickel/Copper" - } - }, - { - "names": ["Платіж Pixel Watch 2"], - "firefly3": { - "description": "Credit payment: Google Pixel Watch 2 Wi-Fi", - "destination": "Credit: Google Pixel Watch 2 Wi-Fi" - } - }, - { - "names": ["Платіж Apple Watch 9"], - "firefly3": { - "description": "Credit payment: Apple Watch Series 9", - "destination": "Credit: Apple Watch Series 9" - } - }, - { - "names": ["Олена А."], - "firefly3": { - "description": "Doctor visit: Елена Ахрипова (психотерапевт)" - } - }, - { - "names": ["Ілля Ш."], - "firefly3": { - "description": "Legal services: Alva Privacy Law Firm", - "destination": "Legal: Alva Privacy Law Firm", - "category": "Legal services" - } - }, - { - "names": ["АТБ", "Велмарт", "Novus", "Glovo", "zakaz.ua", "Мегамаркет", "Сільпо", "Фора", "METRO"], - "mcc_codes": [5411, 5499, 5451, 5422, 5412, 5921], - "firefly3": { - "description": "Groceries" - } - }, - { - "names": ["Аптека Доброго Дня", "Аптека оптових цін", "Аптека Копійка", "Аптека Гала", "Аптека АНЦ", "APTEKA 7", "Біла ромашка", "vidshkod ekv apt12", "Будемо Здорові Разом", "Apteka Ants", "Аптека Шар@"], - "mcc_codes": [5411, 5499, 5451, 5422, 5412, 5921], - "firefly3": { - "description": "Medications" - } - }, - { - "names": ["Київ Цифровий", "Київпастранс"], - "mcc_codes": [4131, 4111, 4112], - "firefly3": { - "description": "Public transport" - } - }, - { - "names": ["McDonald’s"], - "firefly3": { - "description": "McDonalds" - } - }, - { - "names": ["LeoCafe"], - "firefly3": { - "description": "Cafe" - } - }, - { - "names": ["Bolt food", "Glovo", "Menya Musashi", "Mafia"], - "firefly3": { - "description": "Restaurant" - } - }, - { - "names": ["Lumberjack Barberhouse"], - "firefly3": { - "description": "Lumberjack: haircut" - } - } - ] -} \ No newline at end of file diff --git a/config.json.example b/config.json.example index 41311c3..db2422b 100644 --- a/config.json.example +++ b/config.json.example @@ -43,6 +43,14 @@ "firefly3": { "description": "Groceries" } - } + }, + { + "names": ["З чорної картки"], + "firefly3": { + "type": "transfer", + "description": "Transfer between accounts", + "destination": "Mono White" + } + }, ] } \ No newline at end of file diff --git a/main.go b/main.go index 4443920..0a1da99 100644 --- a/main.go +++ b/main.go @@ -35,7 +35,7 @@ func readResponseBody(r *http.Request) (monobank.Transaction, error) { //w.WriteHeader(http.StatusOK) //return - //body = []byte("{\"type\":\"StatementItem\",\"data\":{\"account\":\"4723djMLsLOCzhoeYjxqRw\",\"statementItem\":{\"id\":\"5_NQ0arGAmp2pyNzvA\",\"time\":1711544958,\"description\":\"Ілля Ш.\",\"mcc\":4829,\"originalMcc\":4829,\"amount\":-572000,\"operationAmount\":-572000,\"currencyCode\":980,\"commissionRate\":22000,\"cashbackAmount\":0,\"balance\":8101246,\"hold\":true,\"receiptId\":\"EMXC-P266-90PC-EB8C\"}}}") + body = []byte("{\"type\":\"StatementItem\",\"data\":{\"account\":\"4723djMLsLOCzhoeYjxqRw\",\"statementItem\":{\"id\":\"5_NQ0arGAmp2pyNzvA\",\"time\":1711544958,\"description\":\"З чорної картки\",\"mcc\":4829,\"originalMcc\":4829,\"amount\":-572000,\"operationAmount\":-572000,\"currencyCode\":980,\"commissionRate\":22000,\"cashbackAmount\":0,\"balance\":8101246,\"hold\":true,\"receiptId\":\"EMXC-P266-90PC-EB8C\"}}}") LogString(string(body)) // check empty body @@ -105,7 +105,6 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) { var firefly3Transactions []firefly3.TransactionSplitStore firefly3Transaction := firefly3.TransactionSplitStore{ - Type_: &firefly3TransactionTypeWithdrawal, Date: time.Unix(int64(monobankTransaction.Data.StatementItem.Time), 0).Add(time.Hour * 2), Notes: string(monobankTransactionJson), Amount: strconv.Itoa(int(math.Abs(math.Round(float64(monobankTransaction.Data.StatementItem.Amount/100)))) - int(math.Abs(math.Round(float64(monobankTransaction.Data.StatementItem.CommissionRate/100))))), @@ -113,36 +112,26 @@ func handleWebhook(w http.ResponseWriter, r *http.Request) { } // Special transaction cases - if slices.Contains([]string{"З чорної картки"}, monobankTransaction.Data.StatementItem.Description) { + if slices.Contains([]string{"Термінал EasyPay", "City24", "Термінал City24"}, monobankTransaction.Data.StatementItem.Description) { firefly3Transaction.Type_ = &firefly3TransactionTypeTransfer firefly3Transaction.Description = "Transfer between accounts" - firefly3Transaction.DestinationId = ConfigGetAccountByName(config, "Mono White").Firefly3Id - firefly3Transactions = append(firefly3Transactions, firefly3Transaction) - } else if slices.Contains([]string{"З білої картки"}, monobankTransaction.Data.StatementItem.Description) { - firefly3Transaction.Type_ = &firefly3TransactionTypeTransfer - firefly3Transaction.Description = "Transfer between accounts" - firefly3Transaction.DestinationId = ConfigGetAccountByName(config, "Mono Black").Firefly3Id - firefly3Transactions = append(firefly3Transactions, firefly3Transaction) - } else if slices.Contains([]string{"Термінал City24"}, monobankTransaction.Data.StatementItem.Description) { - firefly3Transaction.Type_ = &firefly3TransactionTypeTransfer - firefly3Transaction.Description = "Transfer between accounts" - firefly3Transaction.SourceId = ConfigGetAccountByName(config, "Wallet cash (UAH)").Firefly3Id - firefly3Transaction.DestinationId = firefly3Transaction.SourceId - firefly3Transactions = append(firefly3Transactions, firefly3Transaction) - } else if slices.Contains([]string{"Термінал EasyPay", "City24", "Термінал City24"}, monobankTransaction.Data.StatementItem.Description) { - firefly3Transaction.Type_ = &firefly3TransactionTypeTransfer - firefly3Transaction.Description = "Transfer between accounts" - firefly3Transaction.SourceId = ConfigGetAccountByName(config, "Wallet cash (UAH)").Firefly3Id // test - firefly3Transaction.DestinationId = account.Firefly3Id // test - firefly3Transactions = append(firefly3Transactions, firefly3Transaction) - } else if slices.Contains([]string{"Банкомат DN00"}, monobankTransaction.Data.StatementItem.Description) { - firefly3Transaction.Type_ = &firefly3TransactionTypeTransfer - firefly3Transaction.Description = "Transfer between accounts" - firefly3Transaction.DestinationId = ConfigGetAccountByName(config, "Wallet cash (UAH)").Firefly3Id // test + firefly3Transaction.SourceName = "Wallet cash (UAH)" // test + firefly3Transaction.DestinationId = account.Firefly3Id // test firefly3Transactions = append(firefly3Transactions, firefly3Transaction) } else { for _, row := range config.TransactionTypes { if slices.Contains(row.Names, monobankTransaction.Data.StatementItem.Description) || slices.Contains(row.MccCodes, monobankTransaction.Data.StatementItem.Mcc) { + switch row.Firefly3.Type { + case "withdrawal": + firefly3Transaction.Type_ = &firefly3TransactionTypeWithdrawal + 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