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

Commit

Permalink
[#1409] POSTCloseDispute handles missing BuyerOrder.Payment.Coin
Browse files Browse the repository at this point in the history
  • Loading branch information
placer14 committed Jan 17, 2019
1 parent 6e196ae commit 30962dd
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
11 changes: 8 additions & 3 deletions api/jsonapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -2096,21 +2096,26 @@ func (i *jsonAPIHandler) POSTOpenDispute(w http.ResponseWriter, r *http.Request)
}

func (i *jsonAPIHandler) POSTCloseDispute(w http.ResponseWriter, r *http.Request) {
type dispute struct {
type disputeParams struct {
OrderID string `json:"orderId"`
Resolution string `json:"resolution"`
BuyerPercentage float32 `json:"buyerPercentage"`
VendorPercentage float32 `json:"vendorPercentage"`
}
decoder := json.NewDecoder(r.Body)
var d dispute
var d disputeParams
err := decoder.Decode(&d)
if err != nil {
ErrorResponse(w, http.StatusBadRequest, err.Error())
return
}

err = i.node.CloseDispute(d.OrderID, d.BuyerPercentage, d.VendorPercentage, d.Resolution)
disputeCase, err := i.node.Datastore.Cases().GetByCaseID(d.OrderID)
if err != nil {
ErrorResponse(w, http.StatusNotFound, err.Error())
}

err = i.node.CloseDispute(disputeCase.CaseID, d.BuyerPercentage, d.VendorPercentage, d.Resolution, disputeCase.PaymentCoin)
if err != nil {
switch err {
case core.ErrCaseNotFound:
Expand Down
2 changes: 2 additions & 0 deletions api/jsonapi_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,10 @@ func TestPosts(t *testing.T) {

func TestCloseDisputeBlocksWhenExpired(t *testing.T) {
dbSetup := func(testRepo *test.Repository) error {
paymentCoin := repo.CurrencyCode("BTC")
expired := factory.NewExpiredDisputeCaseRecord()
expired.CaseID = "expiredCase"
expired.PaymentCoin = &paymentCoin
for _, r := range []*repo.DisputeCaseRecord{expired} {
if err := testRepo.DB.Cases().PutRecord(r); err != nil {
return err
Expand Down
8 changes: 7 additions & 1 deletion core/disputes.go
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ func (n *OpenBazaarNode) ProcessDisputeOpen(rc *pb.RicardianContract, peerID str
}

// CloseDispute - close a dispute
func (n *OpenBazaarNode) CloseDispute(orderID string, buyerPercentage, vendorPercentage float32, resolution string) error {
func (n *OpenBazaarNode) CloseDispute(orderID string, buyerPercentage, vendorPercentage float32, resolution string, paymentCoinHint *repo.CurrencyCode) error {
var payDivision = repo.PayoutRatio{Buyer: buyerPercentage, Vendor: vendorPercentage}
if err := payDivision.Validate(); err != nil {
return err
Expand Down Expand Up @@ -468,6 +468,12 @@ func (n *OpenBazaarNode) CloseDispute(orderID string, buyerPercentage, vendorPer
}
preferredContract := dispute.ResolutionPaymentContract(payDivision)

// TODO: Remove once broken contracts are migrated
if _, err := repo.NewCurrencyCode(preferredContract.BuyerOrder.Payment.Coin); err != nil {
log.Warningf("missing contract BuyerOrder.Payment.Coin on order (%s)", orderID)
preferredContract.BuyerOrder.Payment.Coin = paymentCoinHint.String()
}

var d = new(pb.DisputeResolution)

// Add timestamp
Expand Down

0 comments on commit 30962dd

Please sign in to comment.