@@ -3,7 +3,6 @@ package subscriptionentitlement
33import (
44 "context"
55 "fmt"
6- "time"
76
87 "github.com/samber/lo"
98
@@ -13,6 +12,7 @@ import (
1312 "github.com/openmeterio/openmeter/pkg/framework/transaction"
1413 "github.com/openmeterio/openmeter/pkg/models"
1514 "github.com/openmeterio/openmeter/pkg/pagination"
15+ "github.com/openmeterio/openmeter/pkg/slicesx"
1616)
1717
1818type EntitlementSubscriptionAdapter struct {
@@ -105,8 +105,8 @@ func (a *EntitlementSubscriptionAdapter) GetByItemID(ctx context.Context, id mod
105105 }, nil
106106}
107107
108- func (a * EntitlementSubscriptionAdapter ) GetForSubscriptionAt (ctx context.Context , subscriptionID models. NamespacedID , at time. Time ) ([]subscription.SubscriptionEntitlement , error ) {
109- items , err := a .itemRepo .GetForSubscriptionAt (ctx , subscriptionID , at )
108+ func (a * EntitlementSubscriptionAdapter ) GetForSubscriptionAt (ctx context.Context , input subscription. GetForSubscriptionAtInput ) ([]subscription.SubscriptionEntitlement , error ) {
109+ items , err := a .itemRepo .GetForSubscriptionAt (ctx , input )
110110 if err != nil {
111111 return nil , err
112112 }
@@ -118,7 +118,7 @@ func (a *EntitlementSubscriptionAdapter) GetForSubscriptionAt(ctx context.Contex
118118 if len (items ) > 0 {
119119 ents , err = a .entitlementConnector .ListEntitlements (ctx , entitlement.ListEntitlementsParams {
120120 IDs : lo .Map (items , func (s subscription.SubscriptionItem , _ int ) string { return * s .EntitlementID }),
121- Namespaces : []string {subscriptionID .Namespace },
121+ Namespaces : []string {input .Namespace },
122122 Page : pagination.Page {}, // zero value so all entitlements are fetched
123123 })
124124 if err != nil {
@@ -148,6 +148,42 @@ func (a *EntitlementSubscriptionAdapter) GetForSubscriptionAt(ctx context.Contex
148148 return subEnts , nil
149149}
150150
151+ func (a * EntitlementSubscriptionAdapter ) GetForSubscriptionsAt (ctx context.Context , input []subscription.GetForSubscriptionAtInput ) ([]subscription.SubscriptionEntitlement , error ) {
152+ items , err := a .itemRepo .GetForSubscriptionsAt (ctx , input )
153+ if err != nil {
154+ return nil , err
155+ }
156+
157+ items = lo .Filter (items , func (s subscription.SubscriptionItem , _ int ) bool { return s .EntitlementID != nil })
158+
159+ if len (items ) == 0 {
160+ return nil , nil
161+ }
162+
163+ ents , err := a .entitlementConnector .ListEntitlements (ctx , entitlement.ListEntitlementsParams {
164+ IDs : lo .Map (items , func (s subscription.SubscriptionItem , _ int ) string { return * s .EntitlementID }),
165+ Namespaces : lo .Uniq (lo .Map (input , func (s subscription.GetForSubscriptionAtInput , _ int ) string { return s .Namespace })),
166+ Page : pagination.Page {}, // zero value so all entitlements are fetched
167+ })
168+ if err != nil {
169+ return nil , err
170+ }
171+
172+ return slicesx .MapWithErr (ents .Items , func (ent entitlement.Entitlement ) (subscription.SubscriptionEntitlement , error ) {
173+ if ent .ActiveFrom == nil {
174+ return subscription.SubscriptionEntitlement {}, fmt .Errorf ("entitlement active from is nil, entitlement doesn't have cadence" )
175+ }
176+
177+ return subscription.SubscriptionEntitlement {
178+ Entitlement : ent ,
179+ Cadence : models.CadencedModel {
180+ ActiveFrom : * ent .ActiveFrom ,
181+ ActiveTo : ent .ActiveTo ,
182+ },
183+ }, nil
184+ })
185+ }
186+
151187func (a * EntitlementSubscriptionAdapter ) DeleteByItemID (ctx context.Context , id models.NamespacedID ) error {
152188 item , err := a .itemRepo .GetByID (ctx , id )
153189 if err != nil {
0 commit comments