Skip to content

Commit a6d3ef8

Browse files
committed
feat(customer): get by usage attribution lookup by customer key and id
1 parent 32cc612 commit a6d3ef8

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

openmeter/customer/adapter/customer.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,15 @@ func (a *adapter) GetCustomerByUsageAttribution(ctx context.Context, input custo
392392

393393
query := a.db.Customer.Query().
394394
Where(customerdb.Namespace(input.Namespace)).
395-
Where(customerdb.HasSubjectsWith(customersubjectsdb.SubjectKey(input.SubjectKey))).
395+
Where(
396+
customerdb.Or(
397+
// We lookup the customer by subject key in the subjects table
398+
customerdb.HasSubjectsWith(customersubjectsdb.SubjectKey(input.SubjectKey)),
399+
// Or we lookup the customer by ID or key
400+
customerdb.ID(input.SubjectKey),
401+
customerdb.Key(input.SubjectKey),
402+
),
403+
).
396404
Where(customerdb.DeletedAtIsNil())
397405
query = withSubjects(query)
398406
query = withActiveSubscription(query)

test/customer/customer.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,7 @@ func (s *CustomerHandlerTestSuite) TestGetByUsageAttribution(ctx context.Context
623623
Namespace: s.namespace,
624624
CustomerMutate: customer.CustomerMutate{
625625
Name: TestName,
626+
Key: lo.ToPtr(TestKey),
626627
UsageAttribution: customer.CustomerUsageAttribution{
627628
SubjectKeys: TestSubjectKeys,
628629
},
@@ -642,8 +643,28 @@ func (s *CustomerHandlerTestSuite) TestGetByUsageAttribution(ctx context.Context
642643
require.NotNil(t, cus, "Customer must not be nil")
643644
require.Equal(t, s.namespace, cus.Namespace, "Customer namespace must match")
644645
require.Equal(t, createdCustomer.ID, cus.ID, "Customer ID must match")
645-
require.Equal(t, TestName, cus.Name, "Customer name must match")
646-
require.Equal(t, TestSubjectKeys, cus.UsageAttribution.SubjectKeys, "Customer usage attribution subject keys must match")
646+
647+
// Get the customer by key
648+
cus, err = service.GetCustomerByUsageAttribution(ctx, customer.GetCustomerByUsageAttributionInput{
649+
Namespace: s.namespace,
650+
SubjectKey: TestKey,
651+
})
652+
653+
require.NoError(t, err, "Fetching customer must not return error")
654+
require.NotNil(t, cus, "Customer must not be nil")
655+
require.Equal(t, s.namespace, cus.Namespace, "Customer namespace must match")
656+
require.Equal(t, createdCustomer.ID, cus.ID, "Customer ID must match")
657+
658+
// Get the customer by key
659+
cus, err = service.GetCustomerByUsageAttribution(ctx, customer.GetCustomerByUsageAttributionInput{
660+
Namespace: s.namespace,
661+
SubjectKey: TestKey,
662+
})
663+
664+
require.NoError(t, err, "Fetching customer must not return error")
665+
require.NotNil(t, cus, "Customer must not be nil")
666+
require.Equal(t, s.namespace, cus.Namespace, "Customer namespace must match")
667+
require.Equal(t, createdCustomer.ID, cus.ID, "Customer ID must match")
647668

648669
// Get the customer by usage attribution with a non-existent subject key
649670
_, err = service.GetCustomerByUsageAttribution(ctx, customer.GetCustomerByUsageAttributionInput{

0 commit comments

Comments
 (0)