-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcollect-tax.go
More file actions
221 lines (190 loc) · 5.42 KB
/
Copy pathcollect-tax.go
File metadata and controls
221 lines (190 loc) · 5.42 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"encoding/json"
"log"
"strconv"
"strings"
"time"
"github.com/google/uuid"
"github.com/maxence-charriere/go-app/v10/pkg/app"
shell "github.com/stateless-minds/go-ipfs-api"
)
// payment is a component that holds cyber-gubi. A component is a
// customizable, independent, and reusable UI element. It is created by
// embedding app.Compo into a struct.
type tax struct {
app.Compo
sh *shell.Shell
loggedIn bool
userID string
taxUserID string
taxAmount int
wallet Wallet
taxWallet Wallet
}
func (t *tax) OnMount(ctx app.Context) {
sh := shell.NewShell("localhost:5001")
t.sh = sh
ctx.GetState("loggedIn", &t.loggedIn)
if !t.loggedIn {
ctx.Navigate("/auth")
}
urlPath := app.Window().URL().Path
fragments := strings.Split(urlPath, "tax/")
if len(fragments) > 1 {
t.taxUserID = fragments[1]
}
if t.taxUserID == "" {
ctx.Navigate("/auth")
}
ctx.GetState("userID", &t.userID)
ctx.GetState("balance", &t.wallet)
taxWallet, err := t.getBalance(t.taxUserID)
if err != nil {
log.Fatal("wallet not found")
}
t.taxWallet = taxWallet
}
func (t *tax) getBalance(userID string) (balance Wallet, err error) {
b, err := t.sh.OrbitDocsQuery(dbWallet, "_id", userID)
if err != nil {
return Wallet{}, err
}
if len(b) == 0 {
return Wallet{}, err
}
wallets := []Wallet{}
err = json.Unmarshal(b, &wallets) // Unmarshal the byte slice directly
if err != nil {
return Wallet{}, err
}
return wallets[0], nil
}
func (t *tax) updateBalance(userID string, balance, income int, date string) error {
wallet := Wallet{
ID: userID,
Balance: balance,
Income: income,
LastReceived: date,
}
walletJSON, err := json.Marshal(wallet)
if err != nil {
return err
}
err = t.sh.OrbitDocsPut(dbWallet, walletJSON)
if err != nil {
return err
}
return nil
}
func (t *tax) storeTransaction(transaction Transaction) error {
transactionJSON, err := json.Marshal(transaction)
if err != nil {
return err
}
err = t.sh.OrbitDocsPut(dbTransaction, transactionJSON)
if err != nil {
return err
}
return nil
}
func (t *tax) withdrawTax(ctx app.Context, e app.Event) {
e.PreventDefault()
valid := app.Window().GetElementByID("pay-form").Call("reportValidity").Bool()
if valid {
transaction := Transaction{}
transaction.ID = uuid.NewString()
transaction.SenderID = t.taxUserID
transaction.ReceiverID = t.userID
transaction.TotalCost = t.taxAmount * 100
transaction.Timestamp = time.Now()
transaction.Date = strconv.Itoa(time.Now().Year()) + "/" + strconv.Itoa(int(time.Now().Month()))
if t.taxWallet.Balance-transaction.TotalCost < 0 {
ctx.Notifications().New(app.Notification{
Title: "Error",
Body: "Not enough funds.",
})
return
}
// update taxable balance
err := t.updateBalance(t.taxUserID, t.taxWallet.Balance-transaction.TotalCost, t.taxWallet.Income, t.taxWallet.LastReceived)
if err != nil {
log.Fatal(err)
}
// get treasury balance
receiverBalance, err := t.getBalance(t.userID)
if err != nil {
log.Fatal(err)
}
// update receiver balance
err = t.updateBalance(transaction.ReceiverID, receiverBalance.Balance+transaction.TotalCost, receiverBalance.Income, receiverBalance.LastReceived)
if err != nil {
// rollback sender balance
err := t.updateBalance(t.taxUserID, t.taxWallet.Balance+transaction.TotalCost, t.taxWallet.Income, t.taxWallet.LastReceived)
if err != nil {
log.Fatal(err)
}
return
}
// store transaction
err = t.storeTransaction(transaction)
if err != nil {
// rollback sender balance
err = t.updateBalance(t.taxUserID, t.taxWallet.Balance+transaction.TotalCost, t.taxWallet.Income, t.taxWallet.LastReceived)
if err != nil {
log.Fatal(err)
}
// rollback receiver balance
err = t.updateBalance(transaction.ReceiverID, receiverBalance.Balance-transaction.TotalCost, receiverBalance.Income, receiverBalance.LastReceived)
if err != nil {
log.Fatal(err)
}
return
}
t.wallet.Balance = t.wallet.Balance + transaction.TotalCost
ctx.Update()
ctx.Notifications().New(app.Notification{
Title: "Success",
Body: "Payment successful!",
})
}
}
// The Render method is where the component appearance is defined. Here, a
// payment form is displayed.
func (t *tax) Render() app.UI {
return app.Div().Class("container").Body(
app.Div().Class("mobile").Body(
app.Div().Class("header").Body(
newNav(),
app.Div().Class("header-summary").Body(
app.Span().Class("logo").Text("cyber-gubi"),
app.Div().Class("summary-text").Body(
app.Span().Text("Treasury"),
),
app.Div().Class("summary-balance").Body(
app.Span().Text(strconv.Itoa(t.wallet.Balance/100)+" GUBI"),
),
),
),
app.Div().ID("content").Body(
app.Div().Class("card").Body(
app.Div().Class("upper-row").Body(
app.Div().Class("card-item").Body(
app.Span().Class("span-header").Text("Collect Tax"),
app.Form().ID("pay-form").Body(
app.Label().Class("tax-label").Text("Balance"),
app.Input().Type("number").Value(t.taxWallet.Balance/100).Disabled(true),
app.Input().Type("number").Placeholder("Amount to collect").Required(true).OnChange(t.ValueTo(&t.taxAmount)),
app.Div().Class("drawer drawer-pay").Body(
app.Div().Class("menu-btn").Body(
app.Button().Class("submit").Type("submit").Text("Withdraw").OnClick(t.withdrawTax),
),
),
),
),
),
),
),
),
)
}