Skip to content

Commit

Permalink
feat(cust): add EP to check customer entitlement value
Browse files Browse the repository at this point in the history
  • Loading branch information
GAlexIHU committed Jan 21, 2025
1 parent acc207f commit d4e2087
Show file tree
Hide file tree
Showing 25 changed files with 2,821 additions and 2,148 deletions.
1,573 changes: 820 additions & 753 deletions api/api.gen.go

Large diffs are not rendered by default.

908 changes: 556 additions & 352 deletions api/client/go/client.gen.go

Large diffs are not rendered by default.

1,018 changes: 550 additions & 468 deletions api/openapi.cloud.yaml

Large diffs are not rendered by default.

1,026 changes: 555 additions & 471 deletions api/openapi.yaml

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions api/spec/src/customer.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ interface Customers {
deleteAppData(@path customerId: ULID, @path appId: ULID): {
@statusCode _: 204;
} | NotFoundError | CommonErrors;

/**
* Checks customer access to a given feature (by key). All entitlement types share the hasAccess property in their value response, but multiple other properties are returned based on the entitlement type.
*/
@get
@operationId("getCustomerEntitlementValue")
@route("/{customerId}/entitlements/{featureKey}/value")
@summary("Get entitlement value")
getCustomerEntitlementValue(
@path customerId: string,
@path featureKey: string,
@query(#{ explode: true }) time?: DateTime,
): Entitlements.EntitlementValue | OpenMeter.CommonErrors | OpenMeter.NotFoundError;
}

/**
Expand Down
1 change: 0 additions & 1 deletion api/spec/src/entitlements/subjects.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ using TypeSpec.Http;
using TypeSpec.Rest;
using TypeSpec.OpenAPI;

// TODO: does this have to be in a separate namespace?
namespace OpenMeter.Entitlements;

@route("/api/v1/subjects/{subjectIdOrKey}/entitlements")
Expand Down
6 changes: 4 additions & 2 deletions app/common/customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import (
customeradapter "github.com/openmeterio/openmeter/openmeter/customer/adapter"
customerservice "github.com/openmeterio/openmeter/openmeter/customer/service"
entdb "github.com/openmeterio/openmeter/openmeter/ent/db"
"github.com/openmeterio/openmeter/openmeter/registry"
)

var Customer = wire.NewSet(
NewCustomerService,
)

func NewCustomerService(logger *slog.Logger, db *entdb.Client) (customer.Service, error) {
func NewCustomerService(logger *slog.Logger, db *entdb.Client, entRegistry *registry.Entitlement) (customer.Service, error) {
customerAdapter, err := customeradapter.New(customeradapter.Config{
Client: db,
Logger: logger.WithGroup("customer.postgres"),
Expand All @@ -26,6 +27,7 @@ func NewCustomerService(logger *slog.Logger, db *entdb.Client) (customer.Service
}

return customerservice.New(customerservice.Config{
Adapter: customerAdapter,
Adapter: customerAdapter,
EntitlementConnector: entRegistry.Entitlement,
})
}
26 changes: 13 additions & 13 deletions cmd/billing-worker/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

73 changes: 40 additions & 33 deletions cmd/jobs/billing/advance/advance.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
billingadapter "github.com/openmeterio/openmeter/openmeter/billing/adapter"
billingservice "github.com/openmeterio/openmeter/openmeter/billing/service"
billingworkerautoadvance "github.com/openmeterio/openmeter/openmeter/billing/worker/advance"
registrybuilder "github.com/openmeterio/openmeter/openmeter/registry/builder"
"github.com/openmeterio/openmeter/openmeter/watermill/driver/kafka"
"github.com/openmeterio/openmeter/pkg/framework/entutils/entdriver"
"github.com/openmeterio/openmeter/pkg/framework/pgdriver"
Expand Down Expand Up @@ -57,40 +58,8 @@ func NewAutoAdvancer(ctx context.Context, conf appconfig.Configuration, logger *
// Initialize Ent driver
entPostgresDriver := entdriver.NewEntPostgresDriver(postgresDriver.DB())

customerService, err := common.NewCustomerService(logger, entPostgresDriver.Client())
if err != nil {
return nil, fmt.Errorf("failed to initialize customer service: %w", err)
}

secretService, err := common.NewUnsafeSecretService(logger, entPostgresDriver.Client())
if err != nil {
return nil, fmt.Errorf("failed to initialize secret service: %w", err)
}

appService, err := common.NewAppService(logger, entPostgresDriver.Client(), conf.Apps)
if err != nil {
return nil, fmt.Errorf("failed to initialize app service: %w", err)
}

_, err = common.NewAppStripeService(logger, entPostgresDriver.Client(), conf.Apps, appService, customerService, secretService)
if err != nil {
return nil, fmt.Errorf("failed to initialize stripe app service: %w", err)
}

namespaceManager, err := common.NewNamespaceManager(nil, conf.Namespace)
if err != nil {
return nil, fmt.Errorf("failed to initialize namespace manager: %w", err)
}

_, err = common.NewAppSandboxProvisioner(ctx, logger, conf.Apps, appService, namespaceManager)
if err != nil {
return nil, fmt.Errorf("failed to initialize sandbox app provisioner: %w", err)
}

meterRepository := common.NewInMemoryRepository(conf.Meters)

featureService := common.NewFeatureConnector(logger, entPostgresDriver.Client(), meterRepository)

clickhouseConn, err := common.NewClickHouse(conf.Aggregation.ClickHouse)
if err != nil {
return nil, fmt.Errorf("failed to initialize clickhouse connection: %w", err)
Expand Down Expand Up @@ -129,6 +98,44 @@ func NewAutoAdvancer(ctx context.Context, conf appconfig.Configuration, logger *
return nil, fmt.Errorf("failed to initialize event bus publisher: %w", err)
}

entitlementRegistry := registrybuilder.GetEntitlementRegistry(registrybuilder.EntitlementOptions{
DatabaseClient: entPostgresDriver.Client(),
StreamingConnector: streamingConnector,
Logger: logger,
MeterRepository: meterRepository,
Publisher: ebPublisher,
})

customerService, err := common.NewCustomerService(logger, entPostgresDriver.Client(), entitlementRegistry)
if err != nil {
return nil, fmt.Errorf("failed to initialize customer service: %w", err)
}

secretService, err := common.NewUnsafeSecretService(logger, entPostgresDriver.Client())
if err != nil {
return nil, fmt.Errorf("failed to initialize secret service: %w", err)
}

appService, err := common.NewAppService(logger, entPostgresDriver.Client(), conf.Apps)
if err != nil {
return nil, fmt.Errorf("failed to initialize app service: %w", err)
}

_, err = common.NewAppStripeService(logger, entPostgresDriver.Client(), conf.Apps, appService, customerService, secretService)
if err != nil {
return nil, fmt.Errorf("failed to initialize stripe app service: %w", err)
}

namespaceManager, err := common.NewNamespaceManager(nil, conf.Namespace)
if err != nil {
return nil, fmt.Errorf("failed to initialize namespace manager: %w", err)
}

_, err = common.NewAppSandboxProvisioner(ctx, logger, conf.Apps, appService, namespaceManager)
if err != nil {
return nil, fmt.Errorf("failed to initialize sandbox app provisioner: %w", err)
}

billingAdapter, err := billingadapter.New(billingadapter.Config{
Client: entPostgresDriver.Client(),
Logger: logger,
Expand All @@ -142,7 +149,7 @@ func NewAutoAdvancer(ctx context.Context, conf appconfig.Configuration, logger *
CustomerService: customerService,
AppService: appService,
Logger: logger,
FeatureService: featureService,
FeatureService: entitlementRegistry.Feature,
MeterRepo: meterRepository,
StreamingConnector: streamingConnector,
Publisher: ebPublisher,
Expand Down
Loading

0 comments on commit d4e2087

Please sign in to comment.