forked from OpenBazaar/openbazaar-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cases.go
389 lines (359 loc) · 10.7 KB
/
cases.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
package db
import (
"database/sql"
"encoding/json"
"github.com/OpenBazaar/jsonpb"
"github.com/OpenBazaar/openbazaar-go/pb"
"github.com/OpenBazaar/openbazaar-go/repo"
"strconv"
"sync"
"time"
)
type CasesDB struct {
db *sql.DB
lock sync.RWMutex
}
func (c *CasesDB) Put(caseID string, state pb.OrderState, buyerOpened bool, claim string) error {
c.lock.Lock()
defer c.lock.Unlock()
readInt := 0
buyerOpenedInt := 0
if buyerOpened {
buyerOpenedInt = 1
}
tx, err := c.db.Begin()
if err != nil {
return err
}
stm := `insert or replace into cases(caseID, state, read, timestamp, buyerOpened, claim, buyerPayoutAddress, vendorPayoutAddress) values(?,?,?,?,?,?,?,?)`
stmt, err := tx.Prepare(stm)
if err != nil {
return err
}
defer stmt.Close()
_, err = stmt.Exec(
caseID,
int(state),
readInt,
int(time.Now().Unix()),
buyerOpenedInt,
claim,
"",
"",
)
if err != nil {
tx.Rollback()
return err
}
tx.Commit()
return nil
}
func (c *CasesDB) UpdateBuyerInfo(caseID string, buyerContract *pb.RicardianContract, buyerValidationErrors []string, buyerPayoutAddress string, buyerOutpoints []*pb.Outpoint) error {
m := jsonpb.Marshaler{
EnumsAsInts: false,
EmitDefaults: true,
Indent: " ",
OrigName: false,
}
var buyerOut string
var err error
if buyerContract != nil {
buyerOut, err = m.MarshalToString(buyerContract)
if err != nil {
return err
}
}
buyerErrorsOut, err := json.Marshal(buyerValidationErrors)
if err != nil {
return err
}
var buyerOutpointsOut []byte
if buyerOutpoints != nil {
buyerOutpointsOut, err = json.Marshal(buyerOutpoints)
if err != nil {
return err
}
}
c.lock.Lock()
defer c.lock.Unlock()
_, err = c.db.Exec("update cases set buyerContract=?, buyerValidationErrors=?, buyerPayoutAddress=?, buyerOutpoints=? where caseID=?", buyerOut, string(buyerErrorsOut), buyerPayoutAddress, string(buyerOutpointsOut), caseID)
if err != nil {
return err
}
return nil
}
func (c *CasesDB) UpdateVendorInfo(caseID string, vendorContract *pb.RicardianContract, vendorValidationErrors []string, vendorPayoutAddress string, vendorOutpoints []*pb.Outpoint) error {
m := jsonpb.Marshaler{
EnumsAsInts: false,
EmitDefaults: true,
Indent: " ",
OrigName: false,
}
var vendorOut string
var err error
if vendorContract != nil {
vendorOut, err = m.MarshalToString(vendorContract)
if err != nil {
return err
}
}
vendorErrorsOut, err := json.Marshal(vendorValidationErrors)
if err != nil {
return err
}
var vendorOutpointsOut []byte
if vendorOutpoints != nil {
vendorOutpointsOut, err = json.Marshal(vendorOutpoints)
if err != nil {
return err
}
}
c.lock.Lock()
defer c.lock.Unlock()
_, err = c.db.Exec("update cases set vendorContract=?, vendorValidationErrors=?, vendorPayoutAddress=?, vendorOutpoints=? where caseID=?", vendorOut, string(vendorErrorsOut), vendorPayoutAddress, string(vendorOutpointsOut), caseID)
if err != nil {
return err
}
return nil
}
func (c *CasesDB) MarkAsRead(orderID string) error {
c.lock.Lock()
defer c.lock.Unlock()
_, err := c.db.Exec("update cases set read=? where caseID=?", 1, orderID)
if err != nil {
return err
}
return nil
}
func (c *CasesDB) MarkAsClosed(caseID string, resolution *pb.DisputeResolution) error {
m := jsonpb.Marshaler{
EnumsAsInts: false,
EmitDefaults: true,
Indent: " ",
OrigName: false,
}
var rOut string
var err error
if resolution != nil {
rOut, err = m.MarshalToString(resolution)
if err != nil {
return err
}
}
c.lock.Lock()
defer c.lock.Unlock()
_, err = c.db.Exec("update cases set disputeResolution=?, state=? where caseID=?", rOut, int(pb.OrderState_RESOLVED), caseID)
if err != nil {
return err
}
return nil
}
func (c *CasesDB) Delete(orderID string) error {
c.lock.Lock()
defer c.lock.Unlock()
_, err := c.db.Exec("delete from cases where caseID=?", orderID)
if err != nil {
return err
}
return nil
}
func (c *CasesDB) GetAll(offsetId string, limit int) ([]repo.Case, error) {
c.lock.RLock()
defer c.lock.RUnlock()
var stm string
if offsetId != "" {
stm = "select caseID, timestamp, buyerContract, vendorContract, buyerOpened, state, read from cases where rowid>(select rowid from cases where caseID=?) limit " + strconv.Itoa(limit) + " ;"
} else {
stm = "select caseID, timestamp, buyerContract, vendorContract, buyerOpened, state, read from cases limit " + strconv.Itoa(limit) + ";"
}
rows, err := c.db.Query(stm, offsetId)
if err != nil {
return nil, err
}
defer rows.Close()
var ret []repo.Case
for rows.Next() {
var caseID string
var buyerContract, vendorContract []byte
var timestamp, buyerOpenedInt, stateInt, readInt int
if err := rows.Scan(&caseID, ×tamp, &buyerContract, &vendorContract, &buyerOpenedInt, &stateInt, &readInt); err != nil {
return ret, err
}
read := false
if readInt > 0 {
read = true
}
buyerOpened := false
if buyerOpenedInt > 0 {
buyerOpened = true
}
var total uint64
var title, thumbnail, vendorId, vendorHandle, buyerId, buyerHandle string
contract := new(pb.RicardianContract)
err := jsonpb.UnmarshalString(string(buyerContract), contract)
if err != nil {
jsonpb.UnmarshalString(string(vendorContract), contract)
}
if contract != nil {
if len(contract.VendorListings) > 0 {
if contract.VendorListings[0].VendorID != nil {
vendorId = contract.VendorListings[0].VendorID.Guid
vendorHandle = contract.VendorListings[0].VendorID.BlockchainID
}
if contract.VendorListings[0].Item != nil {
title = contract.VendorListings[0].Item.Title
if len(contract.VendorListings[0].Item.Images) > 0 {
thumbnail = contract.VendorListings[0].Item.Images[0].Tiny
}
}
}
if contract.BuyerOrder != nil {
if contract.BuyerOrder.BuyerID != nil {
buyerId = contract.BuyerOrder.BuyerID.Guid
buyerHandle = contract.BuyerOrder.BuyerID.BlockchainID
}
if contract.BuyerOrder.Payment != nil {
total = contract.BuyerOrder.Payment.Amount
}
}
}
ret = append(ret, repo.Case{
CaseId: caseID,
Timestamp: time.Unix(int64(timestamp), 0),
Title: title,
Thumbnail: thumbnail,
Total: total,
VendorId: vendorId,
VendorHandle: vendorHandle,
BuyerId: buyerId,
BuyerHandle: buyerHandle,
BuyerOpened: buyerOpened,
State: pb.OrderState(stateInt).String(),
Read: read,
})
}
return ret, nil
}
func (c *CasesDB) GetCaseMetadata(caseID string) (buyerContract, vendorContract *pb.RicardianContract, buyerValidationErrors, vendorValidationErrors []string, state pb.OrderState, read bool, timestamp time.Time, buyerOpened bool, claim string, resolution *pb.DisputeResolution, err error) {
c.lock.RLock()
defer c.lock.RUnlock()
stmt, err := c.db.Prepare("select buyerContract, vendorContract, buyerValidationErrors, vendorValidationErrors, state, read, timestamp, buyerOpened, claim, disputeResolution from cases where caseID=?")
defer stmt.Close()
var buyerCon []byte
var vendorCon []byte
var buyerErrors []byte
var vendorErrors []byte
var stateInt int
var readInt *int
var ts int
var buyerOpenedInt int
var disputResolution []byte
err = stmt.QueryRow(caseID).Scan(&buyerCon, &vendorCon, &buyerErrors, &vendorErrors, &stateInt, &readInt, &ts, &buyerOpenedInt, &claim, &disputResolution)
if err != nil {
return nil, nil, []string{}, []string{}, pb.OrderState(0), false, time.Time{}, false, "", nil, err
}
brc := new(pb.RicardianContract)
if string(buyerCon) != "" {
err = jsonpb.UnmarshalString(string(buyerCon), brc)
if err != nil {
return nil, nil, []string{}, []string{}, pb.OrderState(0), false, time.Time{}, false, "", nil, err
}
} else {
brc = nil
}
vrc := new(pb.RicardianContract)
if string(vendorCon) != "" {
err = jsonpb.UnmarshalString(string(vendorCon), vrc)
if err != nil {
return nil, nil, []string{}, []string{}, pb.OrderState(0), false, time.Time{}, false, "", nil, err
}
} else {
vrc = nil
}
read = false
if readInt != nil && *readInt == 1 {
read = true
}
if buyerOpenedInt == 1 {
buyerOpened = true
}
var berr []string
err = json.Unmarshal(buyerErrors, &berr)
if err != nil {
return nil, nil, []string{}, []string{}, pb.OrderState(0), false, time.Time{}, false, "", nil, err
}
var verr []string
err = json.Unmarshal(vendorErrors, &verr)
if err != nil {
return nil, nil, []string{}, []string{}, pb.OrderState(0), false, time.Time{}, false, "", nil, err
}
resolution = new(pb.DisputeResolution)
if string(disputResolution) != "" {
err = jsonpb.UnmarshalString(string(disputResolution), resolution)
if err != nil {
return nil, nil, []string{}, []string{}, pb.OrderState(0), false, time.Time{}, false, "", nil, err
}
} else {
resolution = nil
}
return brc, vrc, berr, verr, pb.OrderState(stateInt), read, time.Unix(int64(ts), 0), buyerOpened, claim, resolution, nil
}
func (c *CasesDB) GetPayoutDetails(caseID string) (buyerContract, vendorContract *pb.RicardianContract, buyerPayoutAddress, vendorPayoutAddress string, buyerOutpoints, vendorOutpoints []*pb.Outpoint, state pb.OrderState, err error) {
c.lock.RLock()
defer c.lock.RUnlock()
stmt, err := c.db.Prepare("select buyerContract, vendorContract, buyerPayoutAddress, vendorPayoutAddress, buyerOutpoints, vendorOutpoints, state from cases where caseID=?")
var buyerCon []byte
var vendorCon []byte
var buyerOuts []byte
var vendorOuts []byte
var buyerAddr string
var vendorAddr string
var stateInt int
err = stmt.QueryRow(caseID).Scan(&buyerCon, &vendorCon, &buyerAddr, &vendorAddr, &buyerOuts, &vendorOuts, &stateInt)
if err != nil {
return nil, nil, "", "", nil, nil, pb.OrderState(0), err
}
brc := new(pb.RicardianContract)
if string(buyerCon) != "" {
err = jsonpb.UnmarshalString(string(buyerCon), brc)
if err != nil {
return nil, nil, "", "", nil, nil, pb.OrderState(0), err
}
} else {
brc = nil
}
vrc := new(pb.RicardianContract)
if string(vendorCon) != "" {
err = jsonpb.UnmarshalString(string(vendorCon), vrc)
if err != nil {
return nil, nil, "", "", nil, nil, pb.OrderState(0), err
}
} else {
vrc = nil
}
var buyerOutpointsOut []pb.Outpoint
if len(buyerOuts) > 0 {
err = json.Unmarshal(buyerOuts, &buyerOutpointsOut)
if err != nil {
return nil, nil, "", "", nil, nil, pb.OrderState(0), err
}
}
var vendorOutpointsOut []pb.Outpoint
if len(vendorOuts) > 0 {
err = json.Unmarshal(vendorOuts, &vendorOutpointsOut)
if err != nil {
return nil, nil, "", "", nil, nil, pb.OrderState(0), err
}
}
toPointer := func(op []pb.Outpoint) []*pb.Outpoint {
if len(op) == 0 {
return nil
}
ret := make([]*pb.Outpoint, len(op))
for i, o := range op {
ret[i] = &o
}
return ret
}
return brc, vrc, buyerAddr, vendorAddr, toPointer(buyerOutpointsOut), toPointer(vendorOutpointsOut), pb.OrderState(stateInt), nil
}