forked from OpenBazaar/openbazaar-go
/
sale_record.go
91 lines (83 loc) · 2.55 KB
/
sale_record.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
package repo
import (
"strings"
"time"
"github.com/OpenBazaar/openbazaar-go/pb"
)
var (
VendorDisputeTimeout_lastInterval = time.Duration(45*24) * time.Hour
)
// SaleRecord represents a one-to-one relationship with records
// in the SQL datastore
type SaleRecord struct {
Contract *pb.RicardianContract
OrderID string
OrderState pb.OrderState
Timestamp time.Time
LastDisputeTimeoutNotifiedAt time.Time
}
// SupportsTimedEscrowRelease indicates whether the underlying AcceptedCurrency supports
// a time-bassed release behavior.
// TODO: Express this from the wallet-interface instead
func (r *SaleRecord) SupportsTimedEscrowRelease() bool {
if r.Contract.BuyerOrder != nil &&
r.Contract.BuyerOrder.Payment != nil {
switch strings.ToUpper(r.Contract.BuyerOrder.Payment.AmountCurrency.Code) {
case "BTC":
return true
case "TBTC":
return true
case "BCH":
return true
case "TBCH":
return true
case "LTC":
return true
case "TLTC":
return true
case "ZEC":
return false
case "TZEC":
return false
case "ETH":
return true
case "TETH":
return true
}
}
return false
}
// IsDisputeable returns whether the Sale is in a state that it can be disputed with a
// third-party moderator
func (r *SaleRecord) IsDisputeable() bool {
if r.IsModeratedContract() {
switch r.OrderState {
case pb.OrderState_PARTIALLY_FULFILLED, pb.OrderState_FULFILLED:
return true
}
}
return false
}
// IsModeratedContract indicates whether the SaleRecord has a contract which includes
// a third-party moderator
func (r *SaleRecord) IsModeratedContract() bool {
return r.Contract != nil && r.Contract.BuyerOrder != nil && r.Contract.BuyerOrder.Payment != nil && r.Contract.BuyerOrder.Payment.Method == pb.Order_Payment_MODERATED
}
// BuildVendorDisputeTimeoutLastNotification returns a Notification that alerts a SaleRecord
// is more than 45 days old and already expired
func (r *SaleRecord) BuildVendorDisputeTimeoutLastNotification(createdAt time.Time) *Notification {
notification := &VendorDisputeTimeout{
ID: NewNotificationID(),
ExpiresIn: uint(0),
OrderID: r.OrderID,
Thumbnail: Thumbnail{},
Type: NotifierTypeVendorDisputeTimeout,
}
if len(r.Contract.VendorListings) > 0 && len(r.Contract.VendorListings[0].Item.Images) > 0 {
notification.Thumbnail = Thumbnail{
Tiny: r.Contract.VendorListings[0].Item.Images[0].Tiny,
Small: r.Contract.VendorListings[0].Item.Images[0].Small,
}
}
return NewNotification(notification, createdAt, false)
}