Skip to content

Commit

Permalink
Expose methods for checking if a meter has entitlement entities assoc…
Browse files Browse the repository at this point in the history
…iated (#1271)

* feat: implement repo call

* feat(productcatalog): implement repo call
  • Loading branch information
GAlexIHU authored Jul 29, 2024
1 parent 3e28f96 commit d03ec4a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
15 changes: 15 additions & 0 deletions internal/entitlement/postgresadapter/entitlement.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()

Expand Down
2 changes: 2 additions & 0 deletions internal/entitlement/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions internal/productcatalog/feature_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions internal/productcatalog/postgresadapter/feature.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit d03ec4a

Please sign in to comment.