diff --git a/openmeter/productcatalog/subscription/http/get.go b/openmeter/productcatalog/subscription/http/get.go index bb5ed5695..e5e36f5e7 100644 --- a/openmeter/productcatalog/subscription/http/get.go +++ b/openmeter/productcatalog/subscription/http/get.go @@ -52,7 +52,7 @@ func (h *handler) GetSubscription() GetSubscriptionHandler { return def, err } - return MapSubscriptionViewToAPI(view.WithoutItemHistory()) + return MapSubscriptionViewToAPI(view.WithOnlyActivesInCurrentPhase()) }, commonhttp.JSONResponseEncoderWithStatus[GetSubscriptionResponse](http.StatusOK), httptransport.AppendOptions( diff --git a/openmeter/subscription/subscriptionview.go b/openmeter/subscription/subscriptionview.go index 0abf91fbf..a2f92b466 100644 --- a/openmeter/subscription/subscriptionview.go +++ b/openmeter/subscription/subscriptionview.go @@ -11,6 +11,7 @@ import ( customerentity "github.com/openmeterio/openmeter/openmeter/customer/entity" "github.com/openmeterio/openmeter/openmeter/entitlement" meteredentitlement "github.com/openmeterio/openmeter/openmeter/entitlement/metered" + "github.com/openmeterio/openmeter/pkg/clock" "github.com/openmeterio/openmeter/pkg/convert" "github.com/openmeterio/openmeter/pkg/datex" "github.com/openmeterio/openmeter/pkg/models" @@ -57,6 +58,7 @@ func (s *SubscriptionView) Validate(includePhases bool) error { return nil } +// WithoutItemHistory returns a copy of the SubscriptionView with only the last item present for each key. func (v SubscriptionView) WithoutItemHistory() SubscriptionView { v2 := v for i, phase := range v.Phases { @@ -73,6 +75,33 @@ func (v SubscriptionView) WithoutItemHistory() SubscriptionView { return v2 } +func (v SubscriptionView) WithOnlyActivesInCurrentPhase() SubscriptionView { + v2 := v.WithoutItemHistory() + + // If there is a current phase, lets remove all inactive items from that + current, ok := v.Spec.GetCurrentPhaseAt(clock.Now()) + if ok { + for i, phase := range v.Phases { + if phase.SubscriptionPhase.Key == current.PhaseKey { + phase2 := phase + phase2.ItemsByKey = make(map[string][]SubscriptionItemView) + + for key, items := range phase.ItemsByKey { + for _, item := range items { + if item.SubscriptionItem.IsActiveAt(clock.Now()) { + phase2.ItemsByKey[key] = append(phase2.ItemsByKey[key], item) + } + } + } + + v2.Phases[i] = phase2 + } + } + } + + return v2 +} + type SubscriptionPhaseView struct { SubscriptionPhase SubscriptionPhase `json:"subscriptionPhase"` Spec SubscriptionPhaseSpec `json:"spec"`