| package addondiff |
|
|
| import ( |
| "github.com/samber/lo" |
|
|
| "github.com/openmeterio/openmeter/openmeter/productcatalog" |
| "github.com/openmeterio/openmeter/openmeter/subscription" |
| subscriptionaddon "github.com/openmeterio/openmeter/openmeter/subscription/addon" |
| ) |
|
|
| |
| func GetAffectedItemIDs(view subscription.SubscriptionView, addon subscriptionaddon.SubscriptionAddon) map[string][]string { |
| affected := map[string][]string{} |
|
|
| |
|
|
| instances := addon.GetInstances() |
|
|
| if len(instances) == 0 { |
| return affected |
| } |
|
|
| for _, p := range view.Phases { |
| for _, items := range p.ItemsByKey { |
| for _, item := range items { |
| for _, inst := range instances { |
| if inst.Quantity == 0 { |
| continue |
| } |
|
|
| itemPer := item.SubscriptionItem.CadencedModel.AsPeriod() |
| instPer := inst.CadencedModel.AsPeriod() |
|
|
| |
| if instPer.Intersection(itemPer) == nil { |
| continue |
| } |
|
|
| for _, rc := range inst.RateCards { |
| rcKey := rc.AddonRateCard.RateCard.Key() |
|
|
| if _, ok := affected[rcKey]; !ok { |
| affected[rcKey] = []string{} |
| } |
|
|
| if productcatalog.AddonRateCardMatcherForAGivenPlanRateCard(item.SubscriptionItem.RateCard)(rc.AddonRateCard.RateCard) { |
| affected[rcKey] = append(affected[rcKey], item.SubscriptionItem.ID) |
| } |
| } |
| } |
| } |
| } |
| } |
|
|
| |
| return lo.MapEntries(affected, func(key string, value []string) (string, []string) { |
| return key, lo.Uniq(value) |
| }) |
| } |
|
|