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

Commit 22ea550

Browse files
committed
Extracted method core.GetOrder(), added unit test for it
1 parent 3fd0924 commit 22ea550

File tree

3 files changed

+86
-48
lines changed

3 files changed

+86
-48
lines changed

api/jsonapi.go

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,53 +1637,12 @@ func (i *jsonAPIHandler) POSTResyncBlockchain(w http.ResponseWriter, r *http.Req
16371637

16381638
func (i *jsonAPIHandler) GETOrder(w http.ResponseWriter, r *http.Request) {
16391639
_, orderID := path.Split(r.URL.Path)
1640-
var (
1641-
err error
1642-
isSale bool
1643-
contract *pb.RicardianContract
1644-
state pb.OrderState
1645-
funded bool
1646-
records []*wallet.TransactionRecord
1647-
read bool
1648-
paymentCoin *repo.CurrencyCode
1649-
)
1650-
contract, state, funded, records, read, paymentCoin, err = i.node.Datastore.Purchases().GetByOrderId(orderID)
1651-
if err != nil {
1652-
contract, state, funded, records, read, paymentCoin, err = i.node.Datastore.Sales().GetByOrderId(orderID)
1653-
if err != nil {
1654-
ErrorResponse(w, http.StatusNotFound, "Order not found")
1655-
return
1656-
}
1657-
isSale = true
1658-
}
1659-
resp := new(pb.OrderRespApi)
1660-
resp.Contract = contract
1661-
resp.Funded = funded
1662-
resp.Read = read
1663-
resp.State = state
16641640

1665-
// TODO: Remove once broken contracts are migrated
1666-
lookupCoin := contract.BuyerOrder.Payment.Coin
1667-
_, err = repo.LoadCurrencyDefinitions().Lookup(lookupCoin)
1668-
if err != nil {
1669-
log.Warningf("invalid BuyerOrder.Payment.Coin (%s) on order (%s)", lookupCoin, orderID)
1670-
contract.BuyerOrder.Payment.Coin = paymentCoin.String()
1671-
}
1672-
1673-
paymentTxs, refundTx, err := i.node.BuildTransactionRecords(contract, records, state)
1641+
resp, err := i.node.GetOrder(orderID)
16741642
if err != nil {
1675-
ErrorResponse(w, http.StatusInternalServerError, err.Error())
1676-
return
1677-
}
1678-
resp.PaymentAddressTransactions = paymentTxs
1679-
resp.RefundAddressTransaction = refundTx
1680-
1681-
unread, err := i.node.Datastore.Chat().GetUnreadCount(orderID)
1682-
if err != nil {
1683-
ErrorResponse(w, http.StatusInternalServerError, err.Error())
1643+
ErrorResponse(w, http.StatusNotFound, "Order not found")
16841644
return
16851645
}
1686-
resp.UnreadChatMessages = uint64(unread)
16871646

16881647
m := jsonpb.Marshaler{
16891648
EnumsAsInts: false,
@@ -1696,11 +1655,7 @@ func (i *jsonAPIHandler) GETOrder(w http.ResponseWriter, r *http.Request) {
16961655
ErrorResponse(w, http.StatusInternalServerError, err.Error())
16971656
return
16981657
}
1699-
if isSale {
1700-
i.node.Datastore.Sales().MarkAsRead(orderID)
1701-
} else {
1702-
i.node.Datastore.Purchases().MarkAsRead(orderID)
1703-
}
1658+
17041659
SanitizedResponseM(w, out, new(pb.OrderRespApi))
17051660
}
17061661

core/order.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,63 @@ const (
7878
CryptocurrencyPurchasePaymentAddressMaxLength = 512
7979
)
8080

81+
func (n *OpenBazaarNode) GetOrder(orderID string) (*pb.OrderRespApi, error) {
82+
var (
83+
err error
84+
isSale bool
85+
contract *pb.RicardianContract
86+
state pb.OrderState
87+
funded bool
88+
records []*wallet.TransactionRecord
89+
read bool
90+
paymentCoin *repo.CurrencyCode
91+
)
92+
contract, state, funded, records, read, paymentCoin, err = n.Datastore.Purchases().GetByOrderId(orderID)
93+
if err != nil {
94+
contract, state, funded, records, read, paymentCoin, err = n.Datastore.Sales().GetByOrderId(orderID)
95+
if err != nil {
96+
return nil, errors.New("order not found")
97+
}
98+
isSale = true
99+
}
100+
resp := new(pb.OrderRespApi)
101+
resp.Contract = contract
102+
resp.Funded = funded
103+
resp.Read = read
104+
resp.State = state
105+
106+
// TODO: Remove once broken contracts are migrated
107+
lookupCoin := contract.BuyerOrder.Payment.Coin
108+
_, err = repo.LoadCurrencyDefinitions().Lookup(lookupCoin)
109+
if err != nil {
110+
log.Warningf("invalid BuyerOrder.Payment.Coin (%s) on order (%s)", lookupCoin, orderID)
111+
contract.BuyerOrder.Payment.Coin = paymentCoin.String()
112+
}
113+
114+
paymentTxs, refundTx, err := n.BuildTransactionRecords(contract, records, state)
115+
if err != nil {
116+
log.Errorf(err.Error())
117+
return nil, err
118+
}
119+
resp.PaymentAddressTransactions = paymentTxs
120+
resp.RefundAddressTransaction = refundTx
121+
122+
unread, err := n.Datastore.Chat().GetUnreadCount(orderID)
123+
if err != nil {
124+
log.Errorf(err.Error())
125+
return nil, err
126+
}
127+
resp.UnreadChatMessages = uint64(unread)
128+
129+
if isSale {
130+
n.Datastore.Sales().MarkAsRead(orderID)
131+
} else {
132+
n.Datastore.Purchases().MarkAsRead(orderID)
133+
}
134+
135+
return resp, nil
136+
}
137+
81138
// Purchase - add ricardian contract
82139
func (n *OpenBazaarNode) Purchase(data *PurchaseData) (orderID string, paymentAddress string, paymentAmount uint64, vendorOnline bool, err error) {
83140
contract, err := n.createContractWithOrder(data)

core/order_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package core_test
22

33
import (
4+
"github.com/OpenBazaar/openbazaar-go/test/factory"
5+
"github.com/go-errors/errors"
46
"testing"
57

68
"github.com/OpenBazaar/openbazaar-go/core"
@@ -356,3 +358,27 @@ func TestOpenBazaarNode_CalculateOrderTotal(t *testing.T) {
356358
t.Error("Calculated wrong order total")
357359
}
358360
}
361+
362+
func TestOpenBazaarNode_GetOrder(t *testing.T) {
363+
node, err := test.NewNode()
364+
if err != nil {
365+
t.Error(err)
366+
}
367+
368+
contract := factory.NewContract()
369+
orderID, err := node.CalcOrderID(contract.BuyerOrder)
370+
err = node.Datastore.Purchases().Put(orderID, *contract, pb.OrderState_AWAITING_PAYMENT, false)
371+
372+
orderResponse, err := node.GetOrder(orderID)
373+
if err != nil {
374+
t.Error(err)
375+
}
376+
377+
if orderResponse.Funded {
378+
t.Error(errors.New("Should not be funded"))
379+
}
380+
381+
if orderResponse.State != pb.OrderState_AWAITING_PAYMENT {
382+
t.Error(errors.New("Expected state OrderState_AWAITING_PAYMENT"))
383+
}
384+
}

0 commit comments

Comments
 (0)