Created
February 27, 2018 16:32
-
-
Save alexandrebini/714fcd2bed0af64dee3939b9158d4b70 to your computer and use it in GitHub Desktop.
product_policies.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package research | |
import "fmt" | |
// ProductPolicies structure | |
type ProductPolicies []func(product Product) (Product, error) | |
const maxBSR = 200000 | |
// CreateProductPolicies defines all the product policies | |
func CreateProductPolicies(estimatedSale *EstimatedSale) *ProductPolicies { | |
return &ProductPolicies{ | |
calculateFbaNetValuePolicy, | |
calculateSizePolicy, | |
func(p Product) (Product, error) { return estimateSalesPolicy(p, estimatedSale) }, | |
maximumBSRPolicy, | |
containsErrorPolicy, | |
} | |
} | |
// Apply product policies | |
func (policies ProductPolicies) Apply(product Product) Product { | |
for _, apply := range policies { | |
p, err := apply(product) | |
if err != nil { | |
p.Delete() | |
return p | |
} | |
product = p | |
} | |
product.Keep() | |
return product | |
} | |
func calculateFbaNetValuePolicy(p Product) (Product, error) { | |
if p.ListingPrice == nil || p.TotalFeesEstimate == nil { | |
return p, nil | |
} | |
value := *p.ListingPrice - *p.TotalFeesEstimate | |
p.FbaNetValue = &value | |
return p, nil | |
} | |
func calculateSizePolicy(p Product) (Product, error) { | |
if p.hasDimensions() { | |
if packages, ok := mailPackages[*p.Marketplace]; ok { | |
for _, size := range packages { | |
if size.fits(p) { | |
p.SizeID = &size.id | |
return p, nil | |
} | |
} | |
} | |
} | |
p.SizeID = nil | |
return p, nil | |
} | |
func estimateSalesPolicy(p Product, estimatedSale *EstimatedSale) (Product, error) { | |
if p.Bsr == nil || estimatedSale == nil || estimatedSale.Units == nil { | |
return p, nil | |
} | |
p.EstimatedUnits = estimatedSale.Units | |
if p.ListingPrice == nil { | |
return p, nil | |
} | |
sales := float32(*p.EstimatedUnits) * *p.ListingPrice | |
p.EstimatedSales = &sales | |
return p, nil | |
} | |
func maximumBSRPolicy(p Product) (Product, error) { | |
if p.Bsr != nil && *p.Bsr > maxBSR { | |
return p, fmt.Errorf("Product BSR (%d) beyond the maximum (%d)", p.Bsr, maxBSR) | |
} | |
return p, nil | |
} | |
func containsErrorPolicy(p Product) (Product, error) { | |
if p.Error != nil { | |
return p, fmt.Errorf(*p.Error) | |
} | |
return p, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment