From 875f8324e9e8ef1ec89802f72299f585a2533860 Mon Sep 17 00:00:00 2001 From: Alex Goth Date: Tue, 10 Dec 2024 19:28:20 +0100 Subject: [PATCH] fix(plan): expose BillingCadence for all RateCards --- openmeter/productcatalog/ratecard.go | 9 +++++++++ openmeter/productcatalog/subscription/plan.go | 17 +---------------- 2 files changed, 10 insertions(+), 16 deletions(-) diff --git a/openmeter/productcatalog/ratecard.go b/openmeter/productcatalog/ratecard.go index 7085afd4a..57162e167 100644 --- a/openmeter/productcatalog/ratecard.go +++ b/openmeter/productcatalog/ratecard.go @@ -34,6 +34,7 @@ type RateCard interface { Feature() *feature.Feature Key() string Merge(RateCard) error + GetBillingCadence() *datex.Period } type RateCardSerde struct { @@ -157,6 +158,10 @@ type FlatFeeRateCard struct { BillingCadence *datex.Period `json:"billingCadence"` } +func (r *FlatFeeRateCard) GetBillingCadence() *datex.Period { + return r.BillingCadence +} + func (r *FlatFeeRateCard) Merge(v RateCard) error { if r.Type() != v.Type() { return errors.New("type mismatch") @@ -240,6 +245,10 @@ type UsageBasedRateCard struct { BillingCadence datex.Period `json:"billingCadence"` } +func (r *UsageBasedRateCard) GetBillingCadence() *datex.Period { + return &r.BillingCadence +} + func (r *UsageBasedRateCard) Merge(v RateCard) error { if r.Type() != v.Type() { return errors.New("type mismatch") diff --git a/openmeter/productcatalog/subscription/plan.go b/openmeter/productcatalog/subscription/plan.go index dcfc93d04..a04c034f7 100644 --- a/openmeter/productcatalog/subscription/plan.go +++ b/openmeter/productcatalog/subscription/plan.go @@ -4,7 +4,6 @@ import ( "github.com/openmeterio/openmeter/openmeter/productcatalog" "github.com/openmeterio/openmeter/openmeter/subscription" "github.com/openmeterio/openmeter/pkg/currencyx" - "github.com/openmeterio/openmeter/pkg/datex" "github.com/openmeterio/openmeter/pkg/models" ) @@ -98,20 +97,6 @@ func (r *RateCard) ToCreateSubscriptionItemPlanInput() subscription.CreateSubscr fk = &m.Feature.Key } - var cadence *datex.Period - - // FIXME: BillingCadence could be a method on RateCard - switch r.RateCard.Type() { - case productcatalog.FlatFeeRateCardType: - if rc, ok := r.RateCard.(*productcatalog.FlatFeeRateCard); ok { - cadence = rc.BillingCadence - } - case productcatalog.UsageBasedRateCardType: - if rc, ok := r.RateCard.(*productcatalog.UsageBasedRateCard); ok { - cadence = &rc.BillingCadence - } - } - return subscription.CreateSubscriptionItemPlanInput{ PhaseKey: r.PhaseKey, ItemKey: r.Key(), @@ -122,7 +107,7 @@ func (r *RateCard) ToCreateSubscriptionItemPlanInput() subscription.CreateSubscr EntitlementTemplate: m.EntitlementTemplate, TaxConfig: m.TaxConfig, Price: m.Price, - BillingCadence: cadence, + BillingCadence: r.GetBillingCadence(), }, } }