Skip to content

Commit

Permalink
feat(customer): add filtering by subscription plan key
Browse files Browse the repository at this point in the history
  • Loading branch information
GAlexIHU committed Jan 17, 2025
1 parent 85aa09f commit 80aec98
Show file tree
Hide file tree
Showing 9 changed files with 1,543 additions and 1,423 deletions.
1,428 changes: 721 additions & 707 deletions api/api.gen.go

Large diffs are not rendered by default.

1,454 changes: 738 additions & 716 deletions api/client/go/client.gen.go

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions api/openapi.cloud.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2056,6 +2056,7 @@ paths:
- $ref: '#/components/parameters/queryCustomerList.name'
- $ref: '#/components/parameters/queryCustomerList.primaryEmail'
- $ref: '#/components/parameters/queryCustomerList.subject'
- $ref: '#/components/parameters/queryCustomerList.planKey'
responses:
'200':
description: The request has succeeded.
Expand Down Expand Up @@ -7871,6 +7872,14 @@ components:
schema:
type: string
explode: false
queryCustomerList.planKey:
name: planKey
in: query
required: false
description: Filter customers by the plan key of their susbcription.
schema:
type: string
explode: false
queryCustomerList.primaryEmail:
name: primaryEmail
in: query
Expand Down
9 changes: 9 additions & 0 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2055,6 +2055,7 @@ paths:
- $ref: '#/components/parameters/queryCustomerList.name'
- $ref: '#/components/parameters/queryCustomerList.primaryEmail'
- $ref: '#/components/parameters/queryCustomerList.subject'
- $ref: '#/components/parameters/queryCustomerList.planKey'
responses:
'200':
description: The request has succeeded.
Expand Down Expand Up @@ -7467,6 +7468,14 @@ components:
schema:
type: string
explode: false
queryCustomerList.planKey:
name: planKey
in: query
required: false
description: Filter customers by the plan key of their susbcription.
schema:
type: string
explode: false
queryCustomerList.primaryEmail:
name: primaryEmail
in: query
Expand Down
7 changes: 7 additions & 0 deletions api/spec/src/customer.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,13 @@ model ListCustomersParams {
@query
@example("my_subject_key")
subject?: string;

/**
* Filter customers by the plan key of their susbcription.
*/
@query
@example("pro_plan")
planKey?: string;
}

/**
Expand Down
52 changes: 52 additions & 0 deletions e2e/productcatalog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,4 +474,56 @@ func TestPlan(t *testing.T) {

require.Equal(t, 2, len(planCreate.Phases))
})

t.Run("Should list customers of a given plan", func(t *testing.T) {
// Let's make sure our customer is there
require.NotNil(t, customer1)
require.NotEmpty(t, migratedSubscriptionId)

// Let's create a 3rd customer that doesnt have a subscription
// Let's set up two customers
customerAPIRes, err := client.CreateCustomerWithResponse(ctx, api.CreateCustomerJSONRequestBody{
Name: "Test Customer 3",
Currency: lo.ToPtr(api.CurrencyCode("USD")),
Description: lo.ToPtr("Test Customer Description"),
PrimaryEmail: lo.ToPtr("[email protected]"),
BillingAddress: &api.Address{
City: lo.ToPtr("City"),
Country: lo.ToPtr("US"),
Line1: lo.ToPtr("Line 1"),
Line2: lo.ToPtr("Line 2"),
State: lo.ToPtr("State"),
PhoneNumber: lo.ToPtr("1234567890"),
PostalCode: lo.ToPtr("12345"),
},
UsageAttribution: api.CustomerUsageAttribution{
SubjectKeys: []string{"test_customer_subject_3"},
},
})
require.Nil(t, err)
require.Equal(t, 201, customerAPIRes.StatusCode(), "received the following body: %s", customerAPIRes.Body)

// Let's make sure both customers do exist!
apiRes, err := client.ListCustomersWithResponse(ctx, &api.ListCustomersParams{})
require.Nil(t, err)

assert.Equal(t, 200, apiRes.StatusCode(), "received the following body: %s", apiRes.Body)
require.NotNil(t, apiRes.JSON200)
require.NotNil(t, apiRes.JSON200.Items)
require.Equal(t, 3, len(apiRes.JSON200.Items))

// Now let's check the filtering works
apiRes, err = client.ListCustomersWithResponse(ctx, &api.ListCustomersParams{
PlanKey: lo.ToPtr(PlanKey),
})
require.Nil(t, err)

assert.Equal(t, 200, apiRes.StatusCode(), "received the following body: %s", apiRes.Body)
require.NotNil(t, apiRes.JSON200)
require.NotNil(t, apiRes.JSON200.Items)

// Only customer 1 is returned
require.Equal(t, 1, len(apiRes.JSON200.Items))
require.Equal(t, *customer1.Id, *apiRes.JSON200.Items[0].Id)
})
}
5 changes: 5 additions & 0 deletions openmeter/customer/adapter/customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
entdb "github.com/openmeterio/openmeter/openmeter/ent/db"
customerdb "github.com/openmeterio/openmeter/openmeter/ent/db/customer"
customersubjectsdb "github.com/openmeterio/openmeter/openmeter/ent/db/customersubjects"
plandb "github.com/openmeterio/openmeter/openmeter/ent/db/plan"
subscriptiondb "github.com/openmeterio/openmeter/openmeter/ent/db/subscription"
"github.com/openmeterio/openmeter/pkg/clock"
"github.com/openmeterio/openmeter/pkg/framework/entutils"
Expand Down Expand Up @@ -61,6 +62,10 @@ func (a *adapter) ListCustomers(ctx context.Context, input customerentity.ListCu
query = query.Where(customerdb.HasSubjectsWith(customersubjectsdb.SubjectKeyContainsFold(*input.Subject)))
}

if input.PlanKey != nil {
query = query.Where(customerdb.HasSubscriptionWith(subscriptiondb.HasPlanWith(plandb.Key(*input.PlanKey))))
}

// Order
order := entutils.GetOrdering(sortx.OrderDefault)
if !input.Order.IsDefaultValue() {
Expand Down
1 change: 1 addition & 0 deletions openmeter/customer/entity/customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ type ListCustomersInput struct {
Name *string
PrimaryEmail *string
Subject *string
PlanKey *string
}

// CreateCustomerInput represents the input for the CreateCustomer method
Expand Down
1 change: 1 addition & 0 deletions openmeter/customer/httpdriver/customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (h *handler) ListCustomers() ListCustomersHandler {
Name: params.Name,
PrimaryEmail: params.PrimaryEmail,
Subject: params.Subject,
PlanKey: params.PlanKey,

// Modifiers
IncludeDeleted: lo.FromPtrOr(params.IncludeDeleted, customer.IncludeDeleted),
Expand Down

0 comments on commit 80aec98

Please sign in to comment.