diff --git a/internal/entitlement/postgresadapter/entitlement.go b/internal/entitlement/postgresadapter/entitlement.go index c55ee117c..9df01ba6d 100644 --- a/internal/entitlement/postgresadapter/entitlement.go +++ b/internal/entitlement/postgresadapter/entitlement.go @@ -9,6 +9,7 @@ import ( "github.com/openmeterio/openmeter/internal/ent/db" db_entitlement "github.com/openmeterio/openmeter/internal/ent/db/entitlement" + db_feature "github.com/openmeterio/openmeter/internal/ent/db/feature" "github.com/openmeterio/openmeter/internal/ent/db/predicate" db_usagereset "github.com/openmeterio/openmeter/internal/ent/db/usagereset" "github.com/openmeterio/openmeter/internal/entitlement" @@ -144,6 +145,20 @@ func (a *entitlementDBAdapter) GetEntitlementsOfSubject(ctx context.Context, nam return result, nil } +func (a *entitlementDBAdapter) HasEntitlementForMeter(ctx context.Context, namespace string, meterSlug string) (bool, error) { + exists, err := a.db.Entitlement.Query(). + Where( + db_entitlement.Namespace(namespace), + db_entitlement.HasFeatureWith(db_feature.MeterSlugEQ(meterSlug)), + ). + Exist(ctx) + if err != nil { + return false, err + } + + return exists, nil +} + func (a *entitlementDBAdapter) ListEntitlements(ctx context.Context, params entitlement.ListEntitlementsParams) (pagination.PagedResponse[entitlement.Entitlement], error) { query := a.db.Entitlement.Query() diff --git a/internal/entitlement/repository.go b/internal/entitlement/repository.go index 31e12682b..fb028862c 100644 --- a/internal/entitlement/repository.go +++ b/internal/entitlement/repository.go @@ -25,6 +25,8 @@ type EntitlementRepo interface { ListEntitlements(ctx context.Context, params ListEntitlementsParams) (pagination.PagedResponse[Entitlement], error) + HasEntitlementForMeter(ctx context.Context, namespace string, meterSlug string) (bool, error) + // FIXME: This is a terrbile hack LockEntitlementForTx(ctx context.Context, entitlementID models.NamespacedID) error diff --git a/internal/productcatalog/feature_connector.go b/internal/productcatalog/feature_connector.go index 968fa5b72..6e7c39439 100644 --- a/internal/productcatalog/feature_connector.go +++ b/internal/productcatalog/feature_connector.go @@ -74,6 +74,7 @@ type FeatureRepo interface { CreateFeature(ctx context.Context, feature CreateFeatureInputs) (Feature, error) ArchiveFeature(ctx context.Context, featureID models.NamespacedID) error ListFeatures(ctx context.Context, params ListFeaturesParams) (pagination.PagedResponse[Feature], error) + HasActiveFeatureForMeter(ctx context.Context, namespace string, meterSlug string) (bool, error) GetByIdOrKey(ctx context.Context, namespace string, idOrKey string, includeArchived bool) (*Feature, error) entutils.TxCreator diff --git a/internal/productcatalog/postgresadapter/feature.go b/internal/productcatalog/postgresadapter/feature.go index cc1b97c44..5ab8161c3 100644 --- a/internal/productcatalog/postgresadapter/feature.go +++ b/internal/productcatalog/postgresadapter/feature.go @@ -91,6 +91,19 @@ func (c *featureDBAdapter) ArchiveFeature(ctx context.Context, featureID models. return nil } +func (c *featureDBAdapter) HasActiveFeatureForMeter(ctx context.Context, namespace string, meterSlug string) (bool, error) { + exists, err := c.db.Feature.Query(). + Where(db_feature.Namespace(namespace)). + Where(db_feature.MeterSlug(meterSlug)). + Where(db_feature.Or(db_feature.ArchivedAtIsNil(), db_feature.ArchivedAtGT(clock.Now()))). + Exist(ctx) + if err != nil { + return false, err + } + + return exists, nil +} + func (c *featureDBAdapter) ListFeatures(ctx context.Context, params productcatalog.ListFeaturesParams) (pagination.PagedResponse[productcatalog.Feature], error) { query := c.db.Feature.Query(). Where(db_feature.Namespace(params.Namespace))