Skip to content

Commit

Permalink
Merge pull request #1799 from openmeterio/feat/plan-api-impl
Browse files Browse the repository at this point in the history
feat: add service/repo interfaces for Plan API
  • Loading branch information
chrisgacsal authored Nov 7, 2024
2 parents 3f4464b + e62e490 commit 287f794
Show file tree
Hide file tree
Showing 11 changed files with 867 additions and 168 deletions.
14 changes: 14 additions & 0 deletions openmeter/productcatalog/plan/discount.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type discounter interface {
Validator

Type() DiscountType
RateCardKeys() []string
AsPercentage() (PercentageDiscount, error)
FromPercentage(PercentageDiscount)
}
Expand All @@ -43,6 +44,15 @@ type Discount struct {
percentage *PercentageDiscount
}

func (d *Discount) RateCardKeys() []string {
switch d.t {
case PercentageDiscountType:
return d.percentage.RateCards
default:
return nil
}
}

func (d *Discount) MarshalJSON() ([]byte, error) {
var b []byte
var err error
Expand Down Expand Up @@ -137,6 +147,10 @@ type PercentageDiscount struct {

// Percentage defines percentage of the discount.
Percentage decimal.Decimal `json:"percentage"`

// RateCards is the list of specific RateCard Keys the discount is applied to.
// If not provided the discount applies to all RateCards in Phase.
RateCards []string `json:"rateCards,omitempty"`
}

func (f PercentageDiscount) Validate() error {
Expand Down
18 changes: 18 additions & 0 deletions openmeter/productcatalog/plan/entitlement.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,24 @@ func (e *EntitlementTemplate) FromBoolean(t BooleanEntitlementTemplate) {
e.t = entitlement.EntitlementTypeBoolean
}

func NewEntitlementTemplateFrom[T MeteredEntitlementTemplate | StaticEntitlementTemplate | BooleanEntitlementTemplate](c T) EntitlementTemplate {
r := &EntitlementTemplate{}

switch any(c).(type) {
case MeteredEntitlementTemplate:
e := any(c).(MeteredEntitlementTemplate)
r.FromMetered(e)
case StaticEntitlementTemplate:
e := any(c).(StaticEntitlementTemplate)
r.FromStatic(e)
case BooleanEntitlementTemplate:
e := any(c).(BooleanEntitlementTemplate)
r.FromBoolean(e)
}

return *r
}

type EntitlementTemplateMeta struct {
// Type defines the type of the entitlement.Entitlement.
Type entitlement.EntitlementType `json:"type"`
Expand Down
16 changes: 11 additions & 5 deletions openmeter/productcatalog/plan/phase.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (p Phase) Validate() error {
var errs []error

if p.StartAfter.IsNegative() {
errs = append(errs, fmt.Errorf("startAfter must not be negative"))
errs = append(errs, fmt.Errorf("the StartAfter period must not be negative"))
}

// Check for
Expand All @@ -54,23 +54,29 @@ func (p Phase) Validate() error {
rateCardKeys := make(map[string]RateCard)
for _, rateCard := range p.RateCards {
if _, ok := rateCardKeys[rateCard.Key()]; ok {
errs = append(errs, fmt.Errorf("duplicated rate card: %s", rateCard.Key()))
errs = append(errs, fmt.Errorf("duplicated RateCard: %s", rateCard.Key()))
} else {
rateCardKeys[rateCard.Key()] = rateCard
}

if rateCard.Namespace() != p.Namespace {
errs = append(errs, fmt.Errorf("invalid rate card %s: namespace mismatch %s", rateCard.Key(), rateCard.Namespace()))
errs = append(errs, fmt.Errorf("invalid RateCard: namespace mismatch %s", rateCard.Namespace()))
}

if err := rateCard.Validate(); err != nil {
errs = append(errs, fmt.Errorf("invalid rate card %s: %w", rateCard.Key(), err))
errs = append(errs, fmt.Errorf("invalid RateCard: %w", err))
}
}

for _, discount := range p.Discounts {
if err := discount.Validate(); err != nil {
errs = append(errs, fmt.Errorf("invalid discount: %w", err))
errs = append(errs, fmt.Errorf("invalid Discount: %w", err))
}

for _, key := range discount.RateCardKeys() {
if _, ok := rateCardKeys[key]; !ok {
errs = append(errs, fmt.Errorf("invalid Discount: unknown RateCard: %s", key))
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions openmeter/productcatalog/plan/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ func (p Plan) Validate() error {
var _ Validator = (*EffectivePeriod)(nil)

type EffectivePeriod struct {
// EffectiveFrom
// EffectiveFrom defines the time from the Plan becomes active.
EffectiveFrom *time.Time `json:"effectiveFrom,omitempty"`

// EffectiveTo
// EffectiveTO defines the time from the Plan becomes archived.
EffectiveTo *time.Time `json:"effectiveTo,omitempty"`
}

Expand Down
Loading

0 comments on commit 287f794

Please sign in to comment.