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

Commit 0361d31

Browse files
authored
Merge pull request #2073 from OpenBazaar/brian.addBulkUpdatePrices
Adjust listing prices by percentage bulk
2 parents 755e025 + efe7a09 commit 0361d31

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

api/endpoints.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,8 @@ func post(i *jsonAPIHandler, path string, w http.ResponseWriter, r *http.Request
117117
i.POSTResendOrderMessage(w, r)
118118
case strings.HasPrefix(path, "/ob/hashmessage"):
119119
i.POSTHashMessage(w, r)
120+
case strings.HasPrefix(path, "/ob/bulkupdateprices"):
121+
i.POSTBulkUpdatePrices(w, r)
120122
default:
121123
ErrorResponse(w, http.StatusNotFound, "Not Found")
122124
}

api/jsonapi.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4116,6 +4116,35 @@ func (i *jsonAPIHandler) GETPeerInfo(w http.ResponseWriter, r *http.Request) {
41164116
SanitizedResponse(w, string(out))
41174117
}
41184118

4119+
// Enable bulk updating prices for your listings by percentage
4120+
func (i *jsonAPIHandler) POSTBulkUpdatePrices(w http.ResponseWriter, r *http.Request) {
4121+
type BulkUpdatePriceRequest struct {
4122+
Percentage float64 `json:"percentage"`
4123+
}
4124+
4125+
var bulkUpdate BulkUpdatePriceRequest
4126+
err := json.NewDecoder(r.Body).Decode(&bulkUpdate)
4127+
if err != nil {
4128+
http.Error(w, err.Error(), 400)
4129+
return
4130+
}
4131+
4132+
// Check for bad input
4133+
if bulkUpdate.Percentage == 0 {
4134+
SanitizedResponse(w, `{"success": "true"}`)
4135+
return
4136+
}
4137+
4138+
log.Infof("Updating all listing prices by %v percent\n", bulkUpdate.Percentage)
4139+
err = i.node.SetPriceOnListings(bulkUpdate.Percentage)
4140+
if err != nil {
4141+
http.Error(w, err.Error(), 400)
4142+
return
4143+
}
4144+
4145+
SanitizedResponse(w, `{"success": "true"}`)
4146+
}
4147+
41194148
func (i *jsonAPIHandler) POSTBulkUpdateCurrency(w http.ResponseWriter, r *http.Request) {
41204149
// Retrieve attribute and values to update
41214150
type BulkUpdateRequest struct {

core/listings.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,51 @@ func (n *OpenBazaarNode) GetListingFromSlug(slug string) (*pb.SignedListing, err
651651
return sl, nil
652652
}
653653

654+
func (n *OpenBazaarNode) SetPriceOnListings(percentage float64) error {
655+
absPath, err := filepath.Abs(path.Join(n.RepoPath, "root", "listings"))
656+
if err != nil {
657+
return err
658+
}
659+
walkpath := func(p string, f os.FileInfo, err error) error {
660+
if !f.IsDir() && filepath.Ext(p) == ".json" {
661+
signedProto, err := GetSignedListingFromPath(p)
662+
if err != nil {
663+
return err
664+
}
665+
666+
oldSL := repo.NewSignedListingFromProtobuf(signedProto)
667+
l := oldSL.GetListing()
668+
669+
err = l.SetPrices(percentage)
670+
if err != nil {
671+
return err
672+
}
673+
674+
lb, err := l.MarshalJSON()
675+
if err != nil {
676+
return fmt.Errorf("marshaling signed listing (%s): %s", l.GetSlug(), err.Error())
677+
}
678+
err = n.UpdateListing(lb, false)
679+
if err != nil {
680+
return err
681+
}
682+
}
683+
684+
return nil
685+
}
686+
687+
err = filepath.Walk(absPath, walkpath)
688+
if err != nil {
689+
return err
690+
}
691+
692+
err = n.SeedNode()
693+
if err != nil {
694+
return err
695+
}
696+
return nil
697+
}
698+
654699
// SetCurrencyOnListings - set currencies accepted for a listing
655700
func (n *OpenBazaarNode) SetCurrencyOnListings(currencies []string) error {
656701
absPath, err := filepath.Abs(path.Join(n.RepoPath, "root", "listings"))

repo/listing.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,31 @@ func (l *Listing) GetAcceptedCurrencies() []string {
502502
return l.listingProto.Metadata.AcceptedCurrencies
503503
}
504504

505+
// SetPrices
506+
func (l *Listing) SetPrices(percentage float64) error {
507+
508+
currentPrice, ok := new(big.Float).SetString(l.listingProto.Item.BigPrice)
509+
if !ok {
510+
return nil
511+
}
512+
513+
multiple := percentage/100 + 1
514+
floatFactor := new(big.Float).SetFloat64(multiple)
515+
516+
newPrice := new(big.Float).Mul(currentPrice, floatFactor)
517+
newPriceInt, _ := newPrice.Int(nil)
518+
519+
// Check if new price is negative
520+
zeroInt, _ := new(big.Int).SetString("0", 10)
521+
if newPriceInt.Cmp(zeroInt) == -1 {
522+
l.listingProto.Item.BigPrice = "1"
523+
} else {
524+
l.listingProto.Item.BigPrice = newPriceInt.String()
525+
}
526+
527+
return nil
528+
}
529+
505530
// SetAcceptedCurrencies the listing's accepted currency codes. Assumes the node
506531
// serving the listing has already validated the wallet supports the currencies.
507532
func (l *Listing) SetAcceptedCurrencies(codes ...string) error {

0 commit comments

Comments
 (0)