add event base intelligency
This commit is contained in:
@@ -3,6 +3,7 @@ package pricing
|
||||
import (
|
||||
"time"
|
||||
|
||||
"cloud.o-forge.io/core/oc-lib/models/booking"
|
||||
"cloud.o-forge.io/core/oc-lib/tools"
|
||||
)
|
||||
|
||||
@@ -11,6 +12,9 @@ type PricedItemITF interface {
|
||||
GetType() tools.DataType
|
||||
IsPurchasable() bool
|
||||
IsBooked() bool
|
||||
GetQuantity() int
|
||||
AddQuantity(amount int)
|
||||
GetBookingMode() booking.BookingMode
|
||||
GetCreatorID() string
|
||||
SelectPricing() PricingProfileITF
|
||||
GetLocationStart() *time.Time
|
||||
@@ -18,5 +22,5 @@ type PricedItemITF interface {
|
||||
SetLocationEnd(end time.Time)
|
||||
GetLocationEnd() *time.Time
|
||||
GetExplicitDurationInS() float64
|
||||
GetPrice() (float64, error)
|
||||
GetPriceHT() (float64, error)
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ type PricingProfileITF interface {
|
||||
IsPurchasable() bool
|
||||
GetPurchase() BuyingStrategy
|
||||
GetOverrideStrategyValue() int
|
||||
GetPrice(quantity float64, val float64, start time.Time, end time.Time, params ...string) (float64, error)
|
||||
GetPriceHT(quantity float64, val float64, start time.Time, end time.Time, variation []*PricingVariation, params ...string) (float64, error)
|
||||
}
|
||||
|
||||
type RefundType int
|
||||
@@ -34,10 +34,37 @@ type AccessPricingProfile[T Strategy] struct { // only use for acces such as : D
|
||||
RefundRatio int32 `json:"refund_ratio" bson:"refund_ratio" default:"0"` // RefundRatio is the refund ratio if missing
|
||||
}
|
||||
|
||||
func (a AccessPricingProfile[T]) IsBooked() bool {
|
||||
return a.Pricing.BuyingStrategy == SUBSCRIPTION
|
||||
}
|
||||
|
||||
func (a AccessPricingProfile[T]) IsPurchasable() bool {
|
||||
return a.Pricing.BuyingStrategy == PERMANENT
|
||||
}
|
||||
|
||||
func (a AccessPricingProfile[T]) GetPurchase() BuyingStrategy {
|
||||
return a.Pricing.BuyingStrategy
|
||||
}
|
||||
|
||||
func (a AccessPricingProfile[T]) GetPriceHT(quantity float64, val float64, start time.Time, end time.Time, variations []*PricingVariation, params ...string) (float64, error) {
|
||||
return a.Pricing.GetPriceHT(quantity, val, start, &end, variations)
|
||||
}
|
||||
|
||||
func (b *AccessPricingProfile[T]) GetOverrideStrategyValue() int {
|
||||
return -1
|
||||
}
|
||||
|
||||
func GetDefaultPricingProfile() PricingProfileITF {
|
||||
return &AccessPricingProfile[TimePricingStrategy]{
|
||||
Pricing: PricingStrategy[TimePricingStrategy]{
|
||||
Price: 0,
|
||||
Currency: "EUR",
|
||||
BuyingStrategy: PERMANENT,
|
||||
TimePricingStrategy: ONCE,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
type ExploitPrivilegeStrategy int
|
||||
|
||||
const (
|
||||
|
||||
@@ -163,11 +163,37 @@ type PricingStrategy[T Strategy] struct {
|
||||
OverrideStrategy T `json:"override_strategy" bson:"override_strategy" default:"-1"` // Modulation is the modulation of the pricing
|
||||
}
|
||||
|
||||
func (p PricingStrategy[T]) GetPrice(amountOfData float64, bookingTimeDuration float64, start time.Time, end *time.Time) (float64, error) {
|
||||
func (p PricingStrategy[T]) GetPriceHT(amountOfData float64, bookingTimeDuration float64, start time.Time, end *time.Time, variations []*PricingVariation) (float64, error) {
|
||||
if p.BuyingStrategy == SUBSCRIPTION {
|
||||
return BookingEstimation(p.GetTimePricingStrategy(), p.Price*float64(amountOfData), bookingTimeDuration, start, end)
|
||||
} else if p.BuyingStrategy == PERMANENT {
|
||||
price, err := BookingEstimation(p.GetTimePricingStrategy(), p.Price*float64(amountOfData), bookingTimeDuration, start, end)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if variations != nil {
|
||||
for _, v := range variations {
|
||||
price = v.GetPriceHT(price)
|
||||
}
|
||||
return price, nil
|
||||
}
|
||||
|
||||
return p.Price, nil
|
||||
|
||||
} else if p.BuyingStrategy == PERMANENT {
|
||||
if variations != nil {
|
||||
price := p.Price
|
||||
for _, v := range variations {
|
||||
price = v.GetPriceHT(price)
|
||||
}
|
||||
return price, nil
|
||||
}
|
||||
return p.Price, nil
|
||||
}
|
||||
if variations != nil {
|
||||
price := p.Price
|
||||
for _, v := range variations {
|
||||
price = v.GetPriceHT(price)
|
||||
}
|
||||
return price, nil
|
||||
}
|
||||
return p.Price * float64(amountOfData), nil
|
||||
}
|
||||
@@ -183,3 +209,18 @@ func (p PricingStrategy[T]) GetTimePricingStrategy() TimePricingStrategy {
|
||||
func (p PricingStrategy[T]) GetOverrideStrategy() T {
|
||||
return p.OverrideStrategy
|
||||
}
|
||||
|
||||
type PricingVariation struct {
|
||||
Inflate bool `json:"inflate" bson:"price"` // Price is the Price of the pricing
|
||||
Percentage float64 `json:"percent" bson:"percent"` // Currency is the currency of the pricing // Modulation is the modulation of the pricing
|
||||
Priority int `json:"priority" bson:"priority"`
|
||||
}
|
||||
|
||||
func (pv *PricingVariation) GetPriceHT(priceHT float64) float64 {
|
||||
value := (priceHT * pv.Percentage) / 100
|
||||
if pv.Inflate {
|
||||
return priceHT + value
|
||||
} else {
|
||||
return priceHT - value
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,7 +100,7 @@ func TestPricingStrategy_Getters(t *testing.T) {
|
||||
assert.Equal(t, DummyStrategy(1), ps.GetOverrideStrategy())
|
||||
}
|
||||
|
||||
func TestPricingStrategy_GetPrice(t *testing.T) {
|
||||
func TestPricingStrategy_GetPriceHT(t *testing.T) {
|
||||
start := time.Now()
|
||||
end := start.Add(1 * time.Hour)
|
||||
|
||||
@@ -111,19 +111,19 @@ func TestPricingStrategy_GetPrice(t *testing.T) {
|
||||
TimePricingStrategy: pricing.PER_HOUR,
|
||||
}
|
||||
|
||||
p, err := ps.GetPrice(2, 3600, start, &end)
|
||||
p, err := ps.GetPriceHT(2, 3600, start, &end, nil)
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, p > 0)
|
||||
|
||||
// UNLIMITED case
|
||||
ps.BuyingStrategy = pricing.PERMANENT
|
||||
p, err = ps.GetPrice(10, 0, start, &end)
|
||||
p, err = ps.GetPriceHT(10, 0, start, &end, nil)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 5.0, p)
|
||||
|
||||
// PAY_PER_USE case
|
||||
//ps.BuyingStrategy = pricing.PAY_PER_USE
|
||||
p, err = ps.GetPrice(3, 0, start, &end)
|
||||
p, err = ps.GetPriceHT(3, 0, start, &end, nil)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 15.0, p)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user