Skip to content

Commit

Permalink
feat(customer): add filters (#1778)
Browse files Browse the repository at this point in the history
  • Loading branch information
hekike authored Nov 3, 2024
1 parent 273944f commit f9abcc9
Show file tree
Hide file tree
Showing 13 changed files with 1,047 additions and 708 deletions.
1,446 changes: 766 additions & 680 deletions api/api.gen.go

Large diffs are not rendered by default.

74 changes: 72 additions & 2 deletions api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1898,7 +1898,12 @@ paths:
operationId: listCustomers
description: List customers.
parameters:
- $ref: '#/components/parameters/queryCustomerList'
- $ref: '#/components/parameters/CustomerOrderByOrdering.order'
- $ref: '#/components/parameters/CustomerOrderByOrdering.orderBy'
- $ref: '#/components/parameters/queryCustomerList.includeDeleted'
- $ref: '#/components/parameters/queryCustomerList.name'
- $ref: '#/components/parameters/queryCustomerList.primaryEmail'
- $ref: '#/components/parameters/queryCustomerList.subject'
- $ref: '#/components/parameters/PaginatedQuery.page'
- $ref: '#/components/parameters/PaginatedQuery.pageSize'
responses:
Expand Down Expand Up @@ -7396,13 +7401,31 @@ components:
$ref: '#/components/schemas/BillingInvoiceStatus'
title: Filter by the invoice status
explode: false
CustomerOrderByOrdering.order:
name: order
in: query
required: false
description: The order direction.
schema:
$ref: '#/components/schemas/SortOrder'
default: ASC
explode: false
CustomerOrderByOrdering.orderBy:
name: orderBy
in: query
required: false
description: The order by field.
schema:
$ref: '#/components/schemas/CustomerOrderBy'
explode: false
CustomerOverrideOrderByOrdering.order:
name: order
in: query
required: false
description: The order direction.
schema:
$ref: '#/components/schemas/SortOrder'
default: ASC
explode: false
CustomerOverrideOrderByOrdering.orderBy:
name: orderBy
Expand All @@ -7419,6 +7442,7 @@ components:
description: The order direction.
schema:
$ref: '#/components/schemas/SortOrder'
default: ASC
explode: false
EntitlementOrderByOrdering.orderBy:
name: orderBy
Expand All @@ -7435,6 +7459,7 @@ components:
description: The order direction.
schema:
$ref: '#/components/schemas/SortOrder'
default: ASC
explode: false
FeatureOrderByOrdering.orderBy:
name: orderBy
Expand All @@ -7451,6 +7476,7 @@ components:
description: The order direction.
schema:
$ref: '#/components/schemas/SortOrder'
default: ASC
explode: false
GrantOrderByOrdering.orderBy:
name: orderBy
Expand All @@ -7467,6 +7493,7 @@ components:
description: The order direction.
schema:
$ref: '#/components/schemas/SortOrder'
default: ASC
explode: false
InvoiceOrderByOrdering.orderBy:
name: orderBy
Expand Down Expand Up @@ -7595,6 +7622,7 @@ components:
description: The order direction.
schema:
$ref: '#/components/schemas/SortOrder'
default: ASC
explode: false
NotificationChannelOrderByOrdering.orderBy:
name: orderBy
Expand All @@ -7611,6 +7639,7 @@ components:
description: The order direction.
schema:
$ref: '#/components/schemas/SortOrder'
default: ASC
explode: false
NotificationEventOrderByOrdering.orderBy:
name: orderBy
Expand All @@ -7627,6 +7656,7 @@ components:
description: The order direction.
schema:
$ref: '#/components/schemas/SortOrder'
default: ASC
explode: false
NotificationRuleOrderByOrdering.orderBy:
name: orderBy
Expand Down Expand Up @@ -7742,6 +7772,7 @@ components:
description: The order direction.
schema:
$ref: '#/components/schemas/SortOrder'
default: ASC
explode: false
PhasesOrderByOrdering.orderBy:
name: orderBy
Expand All @@ -7758,6 +7789,7 @@ components:
description: The order direction.
schema:
$ref: '#/components/schemas/SortOrder'
default: ASC
explode: false
PlanOrderByOrdering.orderBy:
name: orderBy
Expand All @@ -7774,6 +7806,7 @@ components:
description: The order direction.
schema:
$ref: '#/components/schemas/SortOrder'
default: ASC
explode: false
ProfileOrderByOrdering.orderBy:
name: orderBy
Expand All @@ -7783,7 +7816,7 @@ components:
schema:
$ref: '#/components/schemas/BillingProfileOrderBy'
explode: false
queryCustomerList:
queryCustomerList.includeDeleted:
name: includeDeleted
in: query
required: false
Expand All @@ -7792,6 +7825,36 @@ components:
type: boolean
default: false
explode: false
queryCustomerList.name:
name: name
in: query
required: false
description: |-
Filter customers by name.
Case-insensitive partial match.
schema:
type: string
explode: false
queryCustomerList.primaryEmail:
name: primaryEmail
in: query
required: false
description: |-
Filter customers by primary email.
Case-insensitive partial match.
schema:
type: string
explode: false
queryCustomerList.subject:
name: subject
in: query
required: false
description: |-
Filter customers by usage attribution subject.
Case-insensitive partial match.
schema:
type: string
explode: false
schemas:
Address:
type: object
Expand Down Expand Up @@ -10288,6 +10351,13 @@ components:
maxItems: 1000
description: The items in the page.
description: A page of results.
CustomerOrderBy:
type: string
enum:
- id
- name
- createdAt
description: Order by options for customers.
CustomerOverridePaginatedResponse:
type: object
required:
Expand Down
40 changes: 40 additions & 0 deletions api/spec/src/customer.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,51 @@ interface Customers {
*/
@friendlyName("queryCustomerList")
model ListCustomersParams extends PaginatedQuery {
/**
* Order customers by.
* @TODO: set default value to CustomerOrderBy.name
*/
...OpenMeter.QueryOrdering<CustomerOrderBy>;

/**
* Include deleted customers.
*/
@query
includeDeleted?: boolean = false;

/**
* Filter customers by name.
* Case-insensitive partial match.
*/
@query
@example("ACME")
name?: string;

/**
* Filter customers by primary email.
* Case-insensitive partial match.
*/
@query
@example("[email protected]")
primaryEmail?: string;

/**
* Filter customers by usage attribution subject.
* Case-insensitive partial match.
*/
@query
@example("my_subject_key")
subject?: string;
}

/**
* Order by options for customers.
*/
@friendlyName("CustomerOrderBy")
enum CustomerOrderBy {
id: "id",
name: "name",
createdAt: "createdAt",
}

/**
Expand Down
2 changes: 1 addition & 1 deletion api/spec/src/query.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ model QueryOrdering<T> {
*/
@query
@example(SortOrder.ASC)
order?: SortOrder;
order?: SortOrder = SortOrder.ASC;

/**
* The order by field.
Expand Down
46 changes: 32 additions & 14 deletions openmeter/customer/adapter/customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/samber/lo"

"github.com/openmeterio/openmeter/api"
appobserver "github.com/openmeterio/openmeter/openmeter/app/observer"
customerentity "github.com/openmeterio/openmeter/openmeter/customer/entity"
entdb "github.com/openmeterio/openmeter/openmeter/ent/db"
Expand All @@ -14,6 +15,7 @@ import (
"github.com/openmeterio/openmeter/pkg/clock"
"github.com/openmeterio/openmeter/pkg/framework/entutils"
"github.com/openmeterio/openmeter/pkg/pagination"
"github.com/openmeterio/openmeter/pkg/sortx"
)

// Register registers a new observer
Expand Down Expand Up @@ -55,6 +57,7 @@ func (a *adapter) ListCustomers(ctx context.Context, input customerentity.ListCu
}
}

// Build the database query
query := repo.db.Customer.
Query().
WithSubjects().
Expand All @@ -65,22 +68,37 @@ func (a *adapter) ListCustomers(ctx context.Context, input customerentity.ListCu
query = query.Where(customerdb.DeletedAtIsNil())
}

// order := entutils.GetOrdering(sortx.OrderDefault)
// if !params.Order.IsDefaultValue() {
// order = entutils.GetOrdering(params.Order)
// }
// Filters
if input.Name != nil {
query = query.Where(customerdb.NameContainsFold(*input.Name))
}

if input.PrimaryEmail != nil {
query = query.Where(customerdb.PrimaryEmailContainsFold(*input.PrimaryEmail))
}

if input.Subject != nil {
query = query.Where(customerdb.HasSubjectsWith(customersubjectsdb.SubjectKeyContainsFold(*input.Subject)))
}

// Order
order := entutils.GetOrdering(sortx.OrderDefault)
if !input.Order.IsDefaultValue() {
order = entutils.GetOrdering(input.Order)
}

// switch params.OrderBy {
// case customer.CustomerOrderByCreatedAt:
// query = query.Order(customerdb.ByCreatedAt(order...))
// case customer.CustomerOrderByUpdatedAt:
// query = query.Order(customerdb.ByUpdatedAt(order...))
// case customer.CustomerOrderByID:
// fallthrough
// default:
// query = query.Order(customerdb.ByID(order...))
// }
switch input.OrderBy {
case api.CustomerOrderById:
query = query.Order(customerdb.ByID(order...))
case api.CustomerOrderByCreatedAt:
query = query.Order(customerdb.ByCreatedAt(order...))
case api.CustomerOrderByName:
fallthrough
default:
query = query.Order(customerdb.ByName(order...))
}

// Response
response := pagination.PagedResponse[customerentity.Customer]{
Page: input.Page,
}
Expand Down
10 changes: 10 additions & 0 deletions openmeter/customer/entity/customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/openmeterio/openmeter/pkg/currencyx"
"github.com/openmeterio/openmeter/pkg/models"
"github.com/openmeterio/openmeter/pkg/pagination"
"github.com/openmeterio/openmeter/pkg/sortx"
"github.com/openmeterio/openmeter/pkg/timezone"
)

Expand Down Expand Up @@ -115,6 +116,15 @@ type ListCustomersInput struct {
pagination.Page

IncludeDeleted bool

// Order
OrderBy api.CustomerOrderBy
Order sortx.Order

// Filters
Name *string
PrimaryEmail *string
Subject *string
}

// CreateCustomerInput represents the input for the CreateCustomer method
Expand Down
21 changes: 17 additions & 4 deletions openmeter/customer/httpdriver/customer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (
"github.com/openmeterio/openmeter/api"
"github.com/openmeterio/openmeter/openmeter/customer"
customerentity "github.com/openmeterio/openmeter/openmeter/customer/entity"
"github.com/openmeterio/openmeter/pkg/defaultx"
"github.com/openmeterio/openmeter/pkg/framework/commonhttp"
"github.com/openmeterio/openmeter/pkg/framework/transport/httptransport"
"github.com/openmeterio/openmeter/pkg/pagination"
"github.com/openmeterio/openmeter/pkg/sortx"
)

type (
Expand All @@ -32,14 +34,25 @@ func (h *handler) ListCustomers() ListCustomersHandler {
}

req := ListCustomersRequest{
Namespace: ns,
IncludeDeleted: lo.FromPtrOr(params.IncludeDeleted, customer.IncludeDeleted),
// OrderBy: defaultx.WithDefault(params.OrderBy, api.ListCustomersParamsOrderById),
// Order: sortx.Order(defaultx.WithDefault(params.Order, api.ListCustomersParamsOrderSortOrderASC)),
Namespace: ns,

// Pagination
Page: pagination.Page{
PageSize: lo.FromPtrOr(params.PageSize, customer.DefaultPageSize),
PageNumber: lo.FromPtrOr(params.Page, customer.DefaultPageNumber),
},

// Order
OrderBy: defaultx.WithDefault(params.OrderBy, api.CustomerOrderByName),
Order: sortx.Order(defaultx.WithDefault(params.Order, api.SortOrderASC)),

// Filters
Name: params.Name,
PrimaryEmail: params.PrimaryEmail,
Subject: params.Subject,

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

return req, nil
Expand Down
Loading

0 comments on commit f9abcc9

Please sign in to comment.