-
Notifications
You must be signed in to change notification settings - Fork 8
/
balance.go
98 lines (80 loc) · 2.54 KB
/
balance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package blnk
import (
"fmt"
"github.com/jerry-enebeli/blnk/internal/notification"
"github.com/jerry-enebeli/blnk/model"
)
func NewBalanceTracker() *model.BalanceTracker {
return &model.BalanceTracker{
Balances: make(map[string]*model.Balance),
Frequencies: make(map[string]int),
}
}
func (l *Blnk) checkBalanceMonitors(updatedBalance *model.Balance) {
// Fetch monitors for this balance using datasource
monitors, _ := l.datasource.GetBalanceMonitors(updatedBalance.BalanceID)
// Check each monitor's condition
for _, monitor := range monitors {
if monitor.CheckCondition(updatedBalance) {
fmt.Printf("Condition met for balance: %s\n", monitor.MonitorID)
go func(monitor model.BalanceMonitor) {
err := SendWebhook(NewWebhook{
Event: "balance.monitor",
Payload: monitor,
})
if err != nil {
notification.NotifyError(err)
return
}
}(monitor)
}
}
}
func (l *Blnk) getOrCreateBalanceByIndicator(indicator, currency string) (*model.Balance, error) {
balance, err := l.datasource.GetBalanceByIndicator(indicator, currency)
if err != nil {
balance = &model.Balance{
Indicator: indicator,
LedgerID: GeneralLedgerID,
Currency: currency,
} //TODO refactor
// Save the new balance to the datasource
_, err := l.datasource.CreateBalance(*balance)
if err != nil {
return nil, err
}
balance, err = l.datasource.GetBalanceByIndicator(indicator, currency)
if err != nil {
return nil, err
}
return balance, nil
}
return balance, nil
}
func (l *Blnk) CreateBalance(balance model.Balance) (model.Balance, error) {
return l.datasource.CreateBalance(balance)
}
func (l *Blnk) GetBalanceByID(id string, include []string) (*model.Balance, error) {
return l.datasource.GetBalanceByID(id, include)
}
func (l *Blnk) GetAllBalances() ([]model.Balance, error) {
return l.datasource.GetAllBalances()
}
func (l *Blnk) CreateMonitor(monitor model.BalanceMonitor) (model.BalanceMonitor, error) {
return l.datasource.CreateMonitor(monitor)
}
func (l *Blnk) GetMonitorByID(id string) (*model.BalanceMonitor, error) {
return l.datasource.GetMonitorByID(id)
}
func (l *Blnk) GetAllMonitors() ([]model.BalanceMonitor, error) {
return l.datasource.GetAllMonitors()
}
func (l *Blnk) GetBalanceMonitors(balanceId string) ([]model.BalanceMonitor, error) {
return l.datasource.GetBalanceMonitors(balanceId)
}
func (l *Blnk) UpdateMonitor(monitor *model.BalanceMonitor) error {
return l.datasource.UpdateMonitor(monitor)
}
func (l *Blnk) DeleteMonitor(id string) error {
return l.datasource.DeleteMonitor(id)
}