Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use plan phase duration and index instead of start after #1996

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,840 changes: 718 additions & 1,122 deletions api/api.gen.go

Large diffs are not rendered by default.

4,754 changes: 1,747 additions & 3,007 deletions api/client/go/client.gen.go

Large diffs are not rendered by default.

712 changes: 66 additions & 646 deletions api/openapi.cloud.yaml

Large diffs are not rendered by default.

707 changes: 66 additions & 641 deletions api/openapi.yaml

Large diffs are not rendered by default.

4 changes: 0 additions & 4 deletions api/spec/src/cloud/main.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -210,10 +210,6 @@ namespace OpenMeterCloud.ProductCatalog {
@tag("Plan (Experimental)")
interface PlansById extends OpenMeter.ProductCatalog.Plans {}

@route("/api/v1/plans/{planId}/phases")
@tag("Plan (Experimental)")
interface PlanByIdPhases extends OpenMeter.ProductCatalog.PlanPhases {}

@route("/api/v1/subscriptions")
@tag("Subscriptions")
interface Subscriptions extends OpenMeter.ProductCatalog.Subscriptions {}
Expand Down
7 changes: 3 additions & 4 deletions api/spec/src/productcatalog/plan.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,13 @@ model PlanPhase {
rateCards: RateCard[];

/**
* The time after which the plan starts compared to subscription start
* The duration of the phase.
*/
@visibility("read", "create", "update")
@summary("Start after")
@summary("Duration")
@encode(DurationKnownEncoding.ISO8601)
@example(duration.fromISO("P1Y1D"))
startAfter: duration | null;
duration: duration | null;

/**
* The discounts on the plan.
Expand Down Expand Up @@ -254,5 +254,4 @@ enum PlanOrderBy {
@friendlyName("PhasesOrderBy")
enum PhasesOrderBy {
key: "key",
startAfter: "start_after",
}
97 changes: 0 additions & 97 deletions api/spec/src/productcatalog/routes.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -131,103 +131,6 @@ interface Plans {
archive(
@path planId: ULID,
): Plan | OpenMeter.NotFoundError | OpenMeter.CommonErrors;

/**
* Create a new draft version from plan.
* It returns error if there is already a plan in draft or planId does not reference the latest published version.
*/
@post
@route("/{planIdOrKey}/next")
@operationId("nextPlan")
@summary("New draft plan")
@extension("x-internal", true)
next(
@extension("x-go-type", "string")
@path
planIdOrKey: PlanIdOrKey,
): {
@statusCode _: 201;
@body body: Plan;
} | OpenMeter.NotFoundError | OpenMeter.CommonErrors;
}

@route("/api/v1/plans/{planId}/phases")
@tag("Plan (Experimental)")
interface PlanPhases {
/**
* List all phases in plan.
*/
@get
@operationId("listPlanPhases")
@summary("List phases in plan")
@extension("x-internal", true)
list(
@path planId: ULID,

/**
* Filter by phase.key attribute
*/
@query(#{ explode: true })
key?: Key[],

...OpenMeter.QueryPagination,
...OpenMeter.QueryOrdering<PhasesOrderBy>,
): OpenMeter.PaginatedResponse<PlanPhase> | OpenMeter.CommonErrors;

/**
* Create new phase in plan.
*/
@post
@operationId("createPlanPhase")
@summary("Create new phase in plan")
@extension("x-internal", true)
create(
@path planId: ULID,
@body request: TypeSpec.Rest.Resource.ResourceCreateModel<PlanPhase>,
): {
@statusCode _: 201;
@bodyRoot body: PlanPhase;
} | OpenMeter.CommonErrors;

/**
* Update phase in plan.
*/
@put
@operationId("updatePlanPhase")
@summary("Update phase in plan")
@extension("x-internal", true)
update(
@path planId: ULID,
@path planPhaseKey: Key,

@body
request: TypeSpec.Rest.Resource.ResourceCreateOrUpdateModel<PlanPhase>,
): PlanPhase | OpenMeter.NotFoundError | OpenMeter.CommonErrors;

/**
* Get phase in plan.
*/
@get
@operationId("getPlanPhase")
@summary("Get phase for plan")
@extension("x-internal", true)
get(
@path planId: ULID,
@path planPhaseKey: Key,
): PlanPhase | OpenMeter.NotFoundError | OpenMeter.CommonErrors;

/**
* Delete phase in plan.
*
* Once a phase is deleted it cannot be undeleted.
*/
@delete
@operationId("deletePlanPhase")
@summary("Delete phase for plan")
@extension("x-internal", true)
delete(@path planId: ULID, @path planPhaseKey: Key): {
@statusCode _: 204;
} | OpenMeter.NotFoundError | OpenMeter.CommonErrors;
}

@route("/api/v1/subscriptions")
Expand Down
27 changes: 19 additions & 8 deletions e2e/productcatalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,14 @@ func TestPlan(t *testing.T) {
Name: "Test Plan Phase 1",
Key: "test_plan_phase_1",
Description: lo.ToPtr("Test Plan Phase 1 Description"),
StartAfter: nil,
Duration: lo.ToPtr("P2M"),
RateCards: []api.RateCard{p1RC1, p1RC2},
},
{
Name: "Test Plan Phase 2",
Key: "test_plan_phase_2",
Description: lo.ToPtr("Test Plan Phase 1 Description"),
StartAfter: lo.ToPtr("P2M"),
Duration: nil,
RateCards: []api.RateCard{p2RC1, p2RC2},
},
},
Expand Down Expand Up @@ -374,17 +374,28 @@ func TestPlan(t *testing.T) {
t.Run("Should create and publish a new version of the plan", func(t *testing.T) {
require.NotNil(t, planId)

newPhases := []api.PlanPhase{
planCreate.Phases[0],
{
Name: planCreate.Phases[1].Name,
Key: planCreate.Phases[1].Key,
Duration: lo.ToPtr("P7M"),
RateCards: planCreate.Phases[1].RateCards,
},
{
Name: "Test Plan Phase 3",
Key: "test_plan_phase_3",
Duration: nil,
RateCards: []api.RateCard{p2RC1},
},
}

planAPIRes, err := client.CreatePlanWithResponse(ctx, api.CreatePlanJSONRequestBody{
Name: "Test Plan New Version",
Key: PlanKey,
Currency: api.CurrencyCode("USD"),
// Let's add a new phase
Phases: append(planCreate.Phases, api.PlanPhase{
Name: "Test Plan Phase 3",
Key: "test_plan_phase_3",
StartAfter: lo.ToPtr("P9M"),
RateCards: []api.RateCard{p2RC1},
}),
Phases: newPhases,
})

require.Nil(t, err)
Expand Down
31 changes: 16 additions & 15 deletions openmeter/billing/worker/subscription/scanario_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ import (
planadapter "github.com/openmeterio/openmeter/openmeter/productcatalog/plan/adapter"
planservice "github.com/openmeterio/openmeter/openmeter/productcatalog/plan/service"
productcatalogsubscription "github.com/openmeterio/openmeter/openmeter/productcatalog/subscription"
"github.com/openmeterio/openmeter/openmeter/productcatalog/subscription/testutils"
subscriptiontestutils "github.com/openmeterio/openmeter/openmeter/productcatalog/subscription/testutils"
"github.com/openmeterio/openmeter/openmeter/subscription"
subscriptionentitlementadatapter "github.com/openmeterio/openmeter/openmeter/subscription/adapters/entitlement"
subscriptionrepo "github.com/openmeterio/openmeter/openmeter/subscription/repo"
subscriptionservice "github.com/openmeterio/openmeter/openmeter/subscription/service"
"github.com/openmeterio/openmeter/openmeter/testutils"
"github.com/openmeterio/openmeter/openmeter/watermill/eventbus"
"github.com/openmeterio/openmeter/pkg/clock"
"github.com/openmeterio/openmeter/pkg/currencyx"
Expand All @@ -47,7 +48,7 @@ type SubscriptionHandlerTestSuite struct {

PlanService plan.Service
SubscriptionService subscription.Service
SubscrpiptionPlanAdapter testutils.PlanSubscriptionAdapter
SubscrpiptionPlanAdapter subscriptiontestutils.PlanSubscriptionAdapter
SubscriptionWorkflowService subscription.WorkflowService

Handler *Handler
Expand Down Expand Up @@ -92,7 +93,7 @@ func (s *SubscriptionHandlerTestSuite) SetupSuite() {
Publisher: eventbus.NewMock(s.T()),
})

s.SubscrpiptionPlanAdapter = testutils.NewPlanSubscriptionAdapter(testutils.PlanSubscriptionAdapterConfig{
s.SubscrpiptionPlanAdapter = subscriptiontestutils.NewPlanSubscriptionAdapter(subscriptiontestutils.PlanSubscriptionAdapterConfig{
PlanService: planService,
Logger: slog.Default(),
})
Expand Down Expand Up @@ -249,9 +250,9 @@ func (s *SubscriptionHandlerTestSuite) TestSubscriptionHappyPath() {
Phases: []productcatalog.Phase{
{
PhaseMeta: productcatalog.PhaseMeta{
Name: "free trial",
Key: "free-trial",
StartAfter: datex.MustParse(s.T(), "P0D"),
Name: "free trial",
Key: "free-trial",
Duration: lo.ToPtr(testutils.GetISODuration(s.T(), "P1M")),
},
// TODO[OM-1031]: let's add discount handling (as this could be a 100% discount for the first month)
RateCards: productcatalog.RateCards{
Expand All @@ -267,9 +268,9 @@ func (s *SubscriptionHandlerTestSuite) TestSubscriptionHappyPath() {
},
{
PhaseMeta: productcatalog.PhaseMeta{
Name: "discounted phase",
Key: "discounted-phase",
StartAfter: datex.MustParse(s.T(), "P1M"),
Name: "discounted phase",
Key: "discounted-phase",
Duration: lo.ToPtr(testutils.GetISODuration(s.T(), "P2M")),
},
// TODO[OM-1031]: 50% discount
RateCards: productcatalog.RateCards{
Expand All @@ -288,9 +289,9 @@ func (s *SubscriptionHandlerTestSuite) TestSubscriptionHappyPath() {
},
{
PhaseMeta: productcatalog.PhaseMeta{
Name: "final phase",
Key: "final-phase",
StartAfter: datex.MustParse(s.T(), "P3M"),
Name: "final phase",
Key: "final-phase",
Duration: nil,
},
RateCards: productcatalog.RateCards{
&productcatalog.UsageBasedRateCard{
Expand Down Expand Up @@ -604,9 +605,9 @@ func (s *SubscriptionHandlerTestSuite) TestInArrearsProrating() {
Phases: []productcatalog.Phase{
{
PhaseMeta: productcatalog.PhaseMeta{
Name: "first-phase",
Key: "first-phase",
StartAfter: datex.MustParse(s.T(), "P0D"),
Name: "first-phase",
Key: "first-phase",
Duration: nil,
},
RateCards: productcatalog.RateCards{
&productcatalog.UsageBasedRateCard{
Expand Down
12 changes: 9 additions & 3 deletions openmeter/ent/db/migrate/schema.go

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

Loading
Loading