-
Notifications
You must be signed in to change notification settings - Fork 166
feat(ledger): transaction listing API #4147
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ import ( | |
| "github.com/openmeterio/openmeter/openmeter/billing/charges/models/payment" | ||
| "github.com/openmeterio/openmeter/openmeter/billing/creditgrant" | ||
| "github.com/openmeterio/openmeter/openmeter/ledger" | ||
| "github.com/openmeterio/openmeter/openmeter/ledger/customerbalance" | ||
| "github.com/openmeterio/openmeter/openmeter/productcatalog" | ||
| "github.com/openmeterio/openmeter/pkg/currencyx" | ||
| "github.com/openmeterio/openmeter/pkg/models" | ||
|
|
@@ -307,3 +308,104 @@ func convertBalance(currency currencyx.Code, balance ledger.Balance) api.CreditB | |
| Pending: balance.Pending().String(), | ||
| } | ||
| } | ||
|
|
||
| func fromAPIBillingCreditTransactionType(filter *api.BillingCreditTransactionType) *customerbalance.CreditTransactionType { | ||
| if filter == nil { | ||
| return nil | ||
| } | ||
|
|
||
| var txType customerbalance.CreditTransactionType | ||
| switch *filter { | ||
| case api.BillingCreditTransactionTypeFunded: | ||
| txType = customerbalance.CreditTransactionTypeFunded | ||
| case api.BillingCreditTransactionTypeConsumed: | ||
| txType = customerbalance.CreditTransactionTypeConsumed | ||
| case api.BillingCreditTransactionTypeAdjusted: | ||
| txType = customerbalance.CreditTransactionTypeAdjusted | ||
| default: | ||
| return nil | ||
| } | ||
|
|
||
| return &txType | ||
| } | ||
|
|
||
| func toAPIBillingCreditTransactions(items []customerbalance.CreditTransaction) []api.BillingCreditTransaction { | ||
| out := make([]api.BillingCreditTransaction, 0, len(items)) | ||
|
|
||
| for _, item := range items { | ||
| out = append(out, toAPIBillingCreditTransaction(item)) | ||
| } | ||
|
|
||
| return out | ||
| } | ||
|
|
||
| func toAPIBillingCreditTransaction(tx customerbalance.CreditTransaction) api.BillingCreditTransaction { | ||
| apiTx := api.BillingCreditTransaction{ | ||
| Id: tx.ID.ID, | ||
| CreatedAt: &tx.CreatedAt, | ||
| BookedAt: tx.BookedAt, | ||
| Type: toAPIBillingCreditTransactionType(tx.Type), | ||
| Currency: api.BillingCurrencyCode(tx.Currency), | ||
| Amount: tx.Amount.String(), | ||
| Name: tx.Name, | ||
| AvailableBalance: struct { | ||
| After api.Numeric `json:"after"` | ||
| Before api.Numeric `json:"before"` | ||
| }{ | ||
| Before: tx.Balance.Before.String(), | ||
| After: tx.Balance.After.String(), | ||
| }, | ||
| } | ||
|
|
||
| labels := creditTransactionLabels(tx.Annotations) | ||
| if len(labels) > 0 { | ||
| apiLabels := api.Labels(labels) | ||
| apiTx.Labels = &apiLabels | ||
| } | ||
|
|
||
| return apiTx | ||
| } | ||
|
|
||
| func toAPIBillingCreditTransactionType(txType customerbalance.CreditTransactionType) api.BillingCreditTransactionType { | ||
| switch txType { | ||
| case customerbalance.CreditTransactionTypeFunded: | ||
| return api.BillingCreditTransactionTypeFunded | ||
| case customerbalance.CreditTransactionTypeConsumed: | ||
| return api.BillingCreditTransactionTypeConsumed | ||
| default: | ||
| return api.BillingCreditTransactionTypeAdjusted | ||
| } | ||
|
Comment on lines
+369
to
+377
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don’t coerce unknown transaction types to The 💡 Minimal safer mapping func toAPIBillingCreditTransactionType(txType customerbalance.CreditTransactionType) api.BillingCreditTransactionType {
switch txType {
case customerbalance.CreditTransactionTypeFunded:
return api.BillingCreditTransactionTypeFunded
case customerbalance.CreditTransactionTypeConsumed:
return api.BillingCreditTransactionTypeConsumed
+ case customerbalance.CreditTransactionTypeAdjusted:
+ return api.BillingCreditTransactionTypeAdjusted
default:
- return api.BillingCreditTransactionTypeAdjusted
+ return api.BillingCreditTransactionTypeNaked
}
}🤖 Prompt for AI Agents |
||
| } | ||
|
|
||
| func creditTransactionLabels(annotations models.Annotations) map[string]string { | ||
| labels := make(map[string]string) | ||
|
|
||
| setLabel := func(key, annotationKey string) { | ||
| value := stringAnnotation(annotations, annotationKey) | ||
| if value != "" { | ||
| labels[key] = value | ||
| } | ||
| } | ||
|
|
||
| setLabel("charge_id", ledger.AnnotationChargeID) | ||
| setLabel("subscription_id", ledger.AnnotationSubscriptionID) | ||
| setLabel("subscription_phase_id", ledger.AnnotationSubscriptionPhaseID) | ||
| setLabel("subscription_item_id", ledger.AnnotationSubscriptionItemID) | ||
| setLabel("feature_id", ledger.AnnotationFeatureID) | ||
|
|
||
| return labels | ||
| } | ||
|
|
||
| func stringAnnotation(annotations models.Annotations, key string) string { | ||
| raw, ok := annotations[key] | ||
| if !ok { | ||
| return "" | ||
| } | ||
|
|
||
| value, ok := raw.(string) | ||
| if !ok { | ||
| return "" | ||
| } | ||
|
|
||
| return value | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| package customerscredits | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "github.com/samber/lo" | ||
|
|
||
| api "github.com/openmeterio/openmeter/api/v3" | ||
| "github.com/openmeterio/openmeter/api/v3/apierrors" | ||
| "github.com/openmeterio/openmeter/api/v3/response" | ||
| "github.com/openmeterio/openmeter/openmeter/customer" | ||
| "github.com/openmeterio/openmeter/openmeter/ledger/customerbalance" | ||
| "github.com/openmeterio/openmeter/pkg/currencyx" | ||
| "github.com/openmeterio/openmeter/pkg/framework/commonhttp" | ||
| "github.com/openmeterio/openmeter/pkg/framework/transport/httptransport" | ||
| "github.com/openmeterio/openmeter/pkg/pagination" | ||
| ) | ||
|
|
||
| type ( | ||
| ListCreditTransactionsRequest = customerbalance.ListCreditTransactionsInput | ||
| ListCreditTransactionsResponse = response.PagePaginationResponse[api.BillingCreditTransaction] | ||
| ListCreditTransactionsParams struct { | ||
| CustomerID api.ULID | ||
| Params api.ListCreditTransactionsParams | ||
| } | ||
| ListCreditTransactionsHandler httptransport.HandlerWithArgs[ListCreditTransactionsRequest, ListCreditTransactionsResponse, ListCreditTransactionsParams] | ||
| ) | ||
|
|
||
| func (h *handler) ListCreditTransactions() ListCreditTransactionsHandler { | ||
| return httptransport.NewHandlerWithArgs( | ||
| func(ctx context.Context, r *http.Request, args ListCreditTransactionsParams) (ListCreditTransactionsRequest, error) { | ||
| ns, err := h.resolveNamespace(ctx) | ||
| if err != nil { | ||
| return ListCreditTransactionsRequest{}, err | ||
| } | ||
|
|
||
| page := pagination.NewPage(1, 20) | ||
| if args.Params.Page != nil { | ||
| page = pagination.NewPage( | ||
| lo.FromPtrOr(args.Params.Page.Number, 1), | ||
| lo.FromPtrOr(args.Params.Page.Size, 20), | ||
| ) | ||
| } | ||
|
|
||
| if err := page.Validate(); err != nil { | ||
| return ListCreditTransactionsRequest{}, apierrors.NewBadRequestError(ctx, err, apierrors.InvalidParameters{ | ||
| { | ||
| Field: "page", | ||
| Reason: err.Error(), | ||
| Source: apierrors.InvalidParamSourceQuery, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| req := customerbalance.ListCreditTransactionsInput{ | ||
| CustomerID: customer.CustomerID{ | ||
| Namespace: ns, | ||
| ID: args.CustomerID, | ||
| }, | ||
| Page: page, | ||
| } | ||
|
|
||
| if args.Params.Filter != nil { | ||
| req.Type = fromAPIBillingCreditTransactionType(args.Params.Filter.Type) | ||
|
|
||
| if args.Params.Filter.Currency != nil { | ||
| currency := currencyx.Code(*args.Params.Filter.Currency) | ||
| req.Currency = ¤cy | ||
| } | ||
| } | ||
|
|
||
| return req, nil | ||
| }, | ||
| func(ctx context.Context, request ListCreditTransactionsRequest) (ListCreditTransactionsResponse, error) { | ||
| result, err := h.balanceFacade.ListCreditTransactions(ctx, request) | ||
| if err != nil { | ||
| return ListCreditTransactionsResponse{}, fmt.Errorf("list credit transactions: %w", err) | ||
| } | ||
|
|
||
| return response.NewPagePaginationResponse(toAPIBillingCreditTransactions(result.Items), response.PageMetaPage{ | ||
| Size: request.Page.PageSize, | ||
| Number: request.Page.PageNumber, | ||
| Total: lo.ToPtr(result.TotalCount), | ||
| }), nil | ||
| }, | ||
| commonhttp.JSONResponseEncoderWithStatus[ListCreditTransactionsResponse](http.StatusOK), | ||
| httptransport.AppendOptions( | ||
| h.options, | ||
| httptransport.WithOperationName("list-credit-transactions"), | ||
| httptransport.WithErrorEncoder(apierrors.GenericErrorEncoder()), | ||
| )..., | ||
| ) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package customerscredits | ||
|
|
||
| import ( | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/alpacahq/alpacadecimal" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| api "github.com/openmeterio/openmeter/api/v3" | ||
| "github.com/openmeterio/openmeter/openmeter/ledger" | ||
| "github.com/openmeterio/openmeter/openmeter/ledger/customerbalance" | ||
| "github.com/openmeterio/openmeter/pkg/currencyx" | ||
| "github.com/openmeterio/openmeter/pkg/models" | ||
| ) | ||
|
|
||
| func TestFromAPIBillingCreditTransactionType_Adjusted(t *testing.T) { | ||
| filter := api.BillingCreditTransactionTypeAdjusted | ||
|
|
||
| txType := fromAPIBillingCreditTransactionType(&filter) | ||
|
|
||
| require.NotNil(t, txType) | ||
| require.Equal(t, customerbalance.CreditTransactionTypeAdjusted, *txType) | ||
| } | ||
|
|
||
| func TestToAPIBillingCreditTransaction(t *testing.T) { | ||
| createdAt := time.Date(2026, 4, 10, 9, 0, 0, 0, time.UTC) | ||
| bookedAt := createdAt.Add(time.Second) | ||
|
|
||
| tx := toAPIBillingCreditTransaction(customerbalance.CreditTransaction{ | ||
| ID: models.NamespacedID{ | ||
| Namespace: "ns", | ||
| ID: "tx-1", | ||
| }, | ||
| CreatedAt: createdAt, | ||
| BookedAt: bookedAt, | ||
| Type: customerbalance.CreditTransactionTypeConsumed, | ||
| Currency: currencyx.Code("USD"), | ||
| Amount: alpacadecimal.NewFromInt(-10), | ||
| Balance: customerbalance.CreditTransactionBalance{ | ||
| Before: alpacadecimal.NewFromInt(52), | ||
| After: alpacadecimal.NewFromInt(42), | ||
| }, | ||
| Name: "credit_transaction", | ||
| Annotations: models.Annotations{ | ||
| ledger.AnnotationChargeID: "charge-1", | ||
| }, | ||
| }) | ||
|
|
||
| require.Equal(t, api.ULID("tx-1"), tx.Id) | ||
| require.Equal(t, api.BillingCreditTransactionTypeConsumed, tx.Type) | ||
| require.Equal(t, api.BillingCurrencyCode("USD"), tx.Currency) | ||
| require.Equal(t, api.Numeric("-10"), tx.Amount) | ||
| require.Equal(t, api.Numeric("52"), tx.AvailableBalance.Before) | ||
| require.Equal(t, api.Numeric("42"), tx.AvailableBalance.After) | ||
| require.NotNil(t, tx.Labels) | ||
| require.Equal(t, "charge-1", (*tx.Labels)["charge_id"]) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Invalid transaction-type filters currently get silently dropped
On Line 326, unknown values return
nil, which effectively removes the filter instead of rejecting bad input. That can broaden results unexpectedly for clients.💡 Suggested fix
🤖 Prompt for AI Agents