Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit 0a699e1

Browse files
authored
Merge pull request #1973 from OpenBazaar/orderapi
Include currency definition in sales, purchases, and cases db models
2 parents 023a395 + 07d0034 commit 0a699e1

File tree

9 files changed

+100
-68
lines changed

9 files changed

+100
-68
lines changed

core/disputes.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,11 @@ func (n *OpenBazaarNode) ProcessDisputeOpen(rc *pb.RicardianContract, peerID str
303303
DisputerHandle = contract.VendorListings[0].VendorID.Handle
304304
DisputeeID = order.BuyerID.PeerID
305305
DisputeeHandle = order.BuyerID.Handle
306-
err = n.Datastore.Cases().Put(orderID, pb.OrderState_DISPUTED, false, rc.Dispute.Claim, db.PaymentCoinForContract(contract), db.CoinTypeForContract(contract))
306+
paymentCoin, err := db.PaymentCoinForContract(contract)
307+
if err != nil {
308+
return err
309+
}
310+
err = n.Datastore.Cases().Put(orderID, pb.OrderState_DISPUTED, false, rc.Dispute.Claim, paymentCoin, db.CoinTypeForContract(contract))
307311
if err != nil {
308312
return err
309313
}
@@ -316,7 +320,11 @@ func (n *OpenBazaarNode) ProcessDisputeOpen(rc *pb.RicardianContract, peerID str
316320
DisputerHandle = order.BuyerID.Handle
317321
DisputeeID = contract.VendorListings[0].VendorID.PeerID
318322
DisputeeHandle = contract.VendorListings[0].VendorID.Handle
319-
err = n.Datastore.Cases().Put(orderID, pb.OrderState_DISPUTED, true, rc.Dispute.Claim, db.PaymentCoinForContract(contract), db.CoinTypeForContract(contract))
323+
paymentCoin, err := db.PaymentCoinForContract(contract)
324+
if err != nil {
325+
return err
326+
}
327+
err = n.Datastore.Cases().Put(orderID, pb.OrderState_DISPUTED, true, rc.Dispute.Claim, paymentCoin, db.CoinTypeForContract(contract))
320328
if err != nil {
321329
return err
322330
}

repo/db/cases.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ func (c *CasesDB) PutRecord(dispute *repo.DisputeCaseRecord) error {
2626
c.lock.Lock()
2727
defer c.lock.Unlock()
2828

29+
if dispute.PaymentCoin.String() == "" {
30+
return errors.New("payment coin field is empty")
31+
}
32+
2933
var readInt, buyerOpenedInt uint
3034
if dispute.IsBuyerInitiated {
3135
buyerOpenedInt = 1
@@ -283,13 +287,18 @@ func (c *CasesDB) GetAll(stateFilter []pb.OrderState, searchTerm string, sortByA
283287
}
284288
}
285289

290+
cv, err := repo.NewCurrencyValueWithLookup(total.String(), paymentCoin)
291+
if err != nil {
292+
return nil, 0, err
293+
}
294+
286295
ret = append(ret, repo.Case{
287296
CaseId: caseID,
288297
Slug: slug,
289298
Timestamp: time.Unix(int64(timestamp), 0),
290299
Title: title,
291300
Thumbnail: thumbnail,
292-
Total: total.String(),
301+
Total: *cv,
293302
VendorId: vendorId,
294303
VendorHandle: vendorHandle,
295304
BuyerId: buyerId,

repo/db/cases_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,10 +1067,6 @@ func TestCasesDB_Put_CoinType(t *testing.T) {
10671067
coinType string
10681068
cryptoListing bool
10691069
}{
1070-
{
1071-
"",
1072-
true,
1073-
},
10741070
{
10751071
"TBTC",
10761072
true,
@@ -1092,7 +1088,6 @@ func TestCasesDB_Put_CoinType(t *testing.T) {
10921088
t.Fatal(err)
10931089
}
10941090

1095-
//contract.VendorListings[0].Metadata.CoinType = testCoin
10961091
paymentCoin := repo.CurrencyCode(test.coinType)
10971092

10981093
if test.cryptoListing {

repo/db/purchases.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ func (p *PurchasesDB) Put(orderID string, contract pb.RicardianContract, state p
6767
if dispute != nil {
6868
disputedAt = int(dispute.Timestamp.Seconds)
6969
}
70+
paymentCoin, err := PaymentCoinForContract(&contract)
71+
if err != nil {
72+
return err
73+
}
7074
_, err = stmt.Exec(
7175
orderID,
7276
out,
@@ -81,7 +85,7 @@ func (p *PurchasesDB) Put(orderID string, contract pb.RicardianContract, state p
8185
shippingName,
8286
shippingAddress,
8387
paymentAddr,
84-
PaymentCoinForContract(&contract),
88+
paymentCoin,
8589
CoinTypeForContract(&contract),
8690
disputedAt,
8791
)
@@ -193,13 +197,18 @@ func (p *PurchasesDB) GetAll(stateFilter []pb.OrderState, searchTerm string, sor
193197
coinType = ""
194198
}
195199

200+
cv, err := repo.NewCurrencyValueWithLookup(totalStr, paymentCoin)
201+
if err != nil {
202+
return nil, 0, err
203+
}
204+
196205
ret = append(ret, repo.Purchase{
197206
OrderId: orderID,
198207
Slug: slug,
199208
Timestamp: time.Unix(int64(timestamp), 0),
200209
Title: title,
201210
Thumbnail: thumbnail,
202-
Total: totalStr,
211+
Total: *cv,
203212
VendorId: vendorID,
204213
VendorHandle: vendorHandle,
205214
ShippingName: shippingName,

repo/db/purchases_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -933,7 +933,6 @@ func TestPurchasesDB_Put_PaymentCoin(t *testing.T) {
933933
{[]string{"TBTC", "TBCH"}, "TBCH", "TBCH"},
934934
{[]string{"TBTC", "TBCH"}, "", "TBTC"},
935935
{[]string{"TBCH", "TBTC"}, "", "TBCH"},
936-
{[]string{}, "", ""},
937936
}
938937
)
939938

repo/db/sales.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ func (s *SalesDB) Put(orderID string, contract pb.RicardianContract, state pb.Or
6767
address = contract.VendorOrderConfirmation.PaymentAddress
6868
}
6969

70+
paymentCoin, err := PaymentCoinForContract(&contract)
71+
if err != nil {
72+
return err
73+
}
74+
7075
_, err = stmt.Exec(
7176
orderID,
7277
out,
@@ -81,7 +86,7 @@ func (s *SalesDB) Put(orderID string, contract pb.RicardianContract, state pb.Or
8186
shippingName,
8287
shippingAddress,
8388
address,
84-
PaymentCoinForContract(&contract),
89+
paymentCoin,
8590
CoinTypeForContract(&contract),
8691
)
8792
if err != nil {
@@ -193,13 +198,18 @@ func (s *SalesDB) GetAll(stateFilter []pb.OrderState, searchTerm string, sortByA
193198
coinType = ""
194199
}
195200

201+
cv, err := repo.NewCurrencyValueWithLookup(totalStr, paymentCoin)
202+
if err != nil {
203+
return nil, 0, err
204+
}
205+
196206
ret = append(ret, repo.Sale{
197207
OrderId: orderID,
198208
Slug: slug,
199209
Timestamp: time.Unix(int64(timestamp), 0),
200210
Title: title,
201211
Thumbnail: thumbnail,
202-
Total: totalStr,
212+
Total: *cv,
203213
BuyerId: buyerID,
204214
BuyerHandle: buyerHandle,
205215
ShippingName: shippingName,

repo/db/sales_test.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,6 @@ func TestSalesDB_Put_PaymentCoin(t *testing.T) {
737737
{[]string{"TBTC", "TBCH"}, "TBCH", "TBCH"},
738738
{[]string{"TBTC", "TBCH"}, "", "TBTC"},
739739
{[]string{"TBCH", "TBTC"}, "", "TBCH"},
740-
{[]string{}, "", ""},
741740
}
742741
)
743742

repo/db/util.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
11
package db
22

3-
import "github.com/OpenBazaar/openbazaar-go/pb"
3+
import (
4+
"errors"
5+
"github.com/OpenBazaar/openbazaar-go/pb"
6+
)
47

5-
func PaymentCoinForContract(contract *pb.RicardianContract) string {
8+
func PaymentCoinForContract(contract *pb.RicardianContract) (string, error) {
69
if contract.BuyerOrder.Payment.AmountCurrency != nil &&
710
contract.BuyerOrder.Payment.AmountCurrency.Code != "" {
8-
return contract.BuyerOrder.Payment.AmountCurrency.Code
11+
return contract.BuyerOrder.Payment.AmountCurrency.Code, nil
912
}
1013
if contract.BuyerOrder.Payment.Coin != "" {
11-
return contract.BuyerOrder.Payment.Coin
14+
return contract.BuyerOrder.Payment.Coin, nil
1215
}
1316
if len(contract.VendorListings[0].Metadata.AcceptedCurrencies) > 0 {
14-
return contract.VendorListings[0].Metadata.AcceptedCurrencies[0]
17+
return contract.VendorListings[0].Metadata.AcceptedCurrencies[0], nil
1518
}
16-
return ""
19+
return "", errors.New("payment coin not found")
1720
}
1821

1922
func CoinTypeForContract(contract *pb.RicardianContract) string {

repo/models.go

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -63,60 +63,60 @@ type Metadata struct {
6363
}
6464

6565
type Purchase struct {
66-
OrderId string `json:"orderId"`
67-
Slug string `json:"slug"`
68-
Timestamp time.Time `json:"timestamp"`
69-
Title string `json:"title"`
70-
Thumbnail string `json:"thumbnail"`
71-
Total string `json:"total"`
72-
VendorId string `json:"vendorId"`
73-
VendorHandle string `json:"vendorHandle"`
74-
ShippingName string `json:"shippingName"`
75-
ShippingAddress string `json:"shippingAddress"`
76-
CoinType string `json:"coinType"`
77-
PaymentCoin string `json:"paymentCoin"`
78-
State string `json:"state"`
79-
Read bool `json:"read"`
80-
Moderated bool `json:"moderated"`
81-
UnreadChatMessages int `json:"unreadChatMessages"`
66+
OrderId string `json:"orderId"`
67+
Slug string `json:"slug"`
68+
Timestamp time.Time `json:"timestamp"`
69+
Title string `json:"title"`
70+
Thumbnail string `json:"thumbnail"`
71+
Total CurrencyValue `json:"total"`
72+
VendorId string `json:"vendorId"`
73+
VendorHandle string `json:"vendorHandle"`
74+
ShippingName string `json:"shippingName"`
75+
ShippingAddress string `json:"shippingAddress"`
76+
CoinType string `json:"coinType"`
77+
PaymentCoin string `json:"paymentCoin"`
78+
State string `json:"state"`
79+
Read bool `json:"read"`
80+
Moderated bool `json:"moderated"`
81+
UnreadChatMessages int `json:"unreadChatMessages"`
8282
}
8383

8484
type Sale struct {
85-
OrderId string `json:"orderId"`
86-
Slug string `json:"slug"`
87-
Timestamp time.Time `json:"timestamp"`
88-
Title string `json:"title"`
89-
Thumbnail string `json:"thumbnail"`
90-
Total string `json:"total"`
91-
BuyerId string `json:"buyerId"`
92-
BuyerHandle string `json:"buyerHandle"`
93-
ShippingName string `json:"shippingName"`
94-
ShippingAddress string `json:"shippingAddress"`
95-
CoinType string `json:"coinType"`
96-
PaymentCoin string `json:"paymentCoin"`
97-
State string `json:"state"`
98-
Read bool `json:"read"`
99-
Moderated bool `json:"moderated"`
100-
UnreadChatMessages int `json:"unreadChatMessages"`
85+
OrderId string `json:"orderId"`
86+
Slug string `json:"slug"`
87+
Timestamp time.Time `json:"timestamp"`
88+
Title string `json:"title"`
89+
Thumbnail string `json:"thumbnail"`
90+
Total CurrencyValue `json:"total"`
91+
BuyerId string `json:"buyerId"`
92+
BuyerHandle string `json:"buyerHandle"`
93+
ShippingName string `json:"shippingName"`
94+
ShippingAddress string `json:"shippingAddress"`
95+
CoinType string `json:"coinType"`
96+
PaymentCoin string `json:"paymentCoin"`
97+
State string `json:"state"`
98+
Read bool `json:"read"`
99+
Moderated bool `json:"moderated"`
100+
UnreadChatMessages int `json:"unreadChatMessages"`
101101
}
102102

103103
type Case struct {
104-
CaseId string `json:"caseId"`
105-
Slug string `json:"slug"`
106-
Timestamp time.Time `json:"timestamp"`
107-
Title string `json:"title"`
108-
Thumbnail string `json:"thumbnail"`
109-
Total string `json:"total"`
110-
BuyerId string `json:"buyerId"`
111-
BuyerHandle string `json:"buyerHandle"`
112-
VendorId string `json:"vendorId"`
113-
VendorHandle string `json:"vendorHandle"`
114-
CoinType string `json:"coinType"`
115-
PaymentCoin string `json:"paymentCoin"`
116-
BuyerOpened bool `json:"buyerOpened"`
117-
State string `json:"state"`
118-
Read bool `json:"read"`
119-
UnreadChatMessages int `json:"unreadChatMessages"`
104+
CaseId string `json:"caseId"`
105+
Slug string `json:"slug"`
106+
Timestamp time.Time `json:"timestamp"`
107+
Title string `json:"title"`
108+
Thumbnail string `json:"thumbnail"`
109+
Total CurrencyValue `json:"total"`
110+
BuyerId string `json:"buyerId"`
111+
BuyerHandle string `json:"buyerHandle"`
112+
VendorId string `json:"vendorId"`
113+
VendorHandle string `json:"vendorHandle"`
114+
CoinType string `json:"coinType"`
115+
PaymentCoin string `json:"paymentCoin"`
116+
BuyerOpened bool `json:"buyerOpened"`
117+
State string `json:"state"`
118+
Read bool `json:"read"`
119+
UnreadChatMessages int `json:"unreadChatMessages"`
120120
}
121121

122122
type UnfundedOrder struct {

0 commit comments

Comments
 (0)