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

Commit 6e15486

Browse files
committed
Adjust listing prices by percentage bulk
1 parent 755e025 commit 6e15486

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,48 @@ 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+
l.SetPrices(percentage)
670+
671+
lb, err := l.MarshalJSON()
672+
if err != nil {
673+
return fmt.Errorf("marshaling signed listing (%s): %s", l.GetSlug(), err.Error())
674+
}
675+
err = n.UpdateListing(lb, false)
676+
if err != nil {
677+
return err
678+
}
679+
}
680+
681+
return nil
682+
}
683+
684+
err = filepath.Walk(absPath, walkpath)
685+
if err != nil {
686+
return err
687+
}
688+
689+
err = n.SeedNode()
690+
if err != nil {
691+
return err
692+
}
693+
return nil
694+
}
695+
654696
// SetCurrencyOnListings - set currencies accepted for a listing
655697
func (n *OpenBazaarNode) SetCurrencyOnListings(currencies []string) error {
656698
absPath, err := filepath.Abs(path.Join(n.RepoPath, "root", "listings"))

repo/listing.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,25 @@ 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+
l.listingProto.Item.BigPrice = newPriceInt.String()
520+
521+
return nil
522+
}
523+
505524
// SetAcceptedCurrencies the listing's accepted currency codes. Assumes the node
506525
// serving the listing has already validated the wallet supports the currencies.
507526
func (l *Listing) SetAcceptedCurrencies(codes ...string) error {

0 commit comments

Comments
 (0)