-
Notifications
You must be signed in to change notification settings - Fork 178
feat(ledger): balance #4052
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
Merged
Merged
feat(ledger): balance #4052
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
701b999
feat(ledger): customerbalance
GAlexIHU 039d55e
feat(ledger): balance API
GAlexIHU cee4760
fix: pr comments
GAlexIHU 34b8017
fix: rebase fixups — resolve charge ID field access and duplicate Cre…
GAlexIHU 0ed733e
fix: resolve rebase issues — duplicate wire bindings and billing.Serv…
GAlexIHU File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| package customerscredits | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "net/http" | ||
|
|
||
| api "github.com/openmeterio/openmeter/api/v3" | ||
| "github.com/openmeterio/openmeter/api/v3/apierrors" | ||
| "github.com/openmeterio/openmeter/openmeter/customer" | ||
| "github.com/openmeterio/openmeter/openmeter/ledger" | ||
| "github.com/openmeterio/openmeter/openmeter/ledger/customerbalance" | ||
| "github.com/openmeterio/openmeter/pkg/clock" | ||
| "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/models" | ||
| ) | ||
|
|
||
| var errUnsupportedFeatureFilter = errors.New("feature filter is not supported for this balance endpoint") | ||
|
|
||
| type ( | ||
| GetCustomerCreditBalanceRequest struct { | ||
| CustomerID customer.CustomerID | ||
| Currencies customerbalance.CurrencyFilter | ||
| } | ||
| GetCustomerCreditBalanceResponse = api.BillingCreditBalances | ||
| GetCustomerCreditBalanceParams struct { | ||
| CustomerID api.ULID | ||
| Params api.GetCustomerCreditBalanceParams | ||
| } | ||
| GetCustomerCreditBalanceHandler httptransport.HandlerWithArgs[GetCustomerCreditBalanceRequest, GetCustomerCreditBalanceResponse, GetCustomerCreditBalanceParams] | ||
| ) | ||
|
|
||
| func (h *handler) GetCustomerCreditBalance() GetCustomerCreditBalanceHandler { | ||
| return httptransport.NewHandlerWithArgs( | ||
| func(ctx context.Context, r *http.Request, args GetCustomerCreditBalanceParams) (GetCustomerCreditBalanceRequest, error) { | ||
| namespace, err := h.resolveNamespace(ctx) | ||
| if err != nil { | ||
| return GetCustomerCreditBalanceRequest{}, err | ||
| } | ||
|
|
||
| if args.Params.Filter != nil && args.Params.Filter.Feature != nil { | ||
| return GetCustomerCreditBalanceRequest{}, apierrors.NewBadRequestError( | ||
| ctx, | ||
| models.NewGenericValidationError(errUnsupportedFeatureFilter), | ||
| apierrors.InvalidParameters{ | ||
| { | ||
| Field: "filter.feature", | ||
| Reason: errUnsupportedFeatureFilter.Error(), | ||
| Source: apierrors.InvalidParamSourceQuery, | ||
| }, | ||
| }, | ||
| ) | ||
| } | ||
|
|
||
| request := GetCustomerCreditBalanceRequest{ | ||
| CustomerID: customer.CustomerID{ | ||
| Namespace: namespace, | ||
| ID: args.CustomerID, | ||
| }, | ||
| } | ||
|
|
||
| if args.Params.Filter != nil && args.Params.Filter.Currency != nil { | ||
| currency := currencyx.Code(*args.Params.Filter.Currency) | ||
| request.Currencies = customerbalance.CurrencyFilter{ | ||
| Codes: []currencyx.Code{currency}, | ||
| } | ||
| } | ||
|
|
||
| return request, nil | ||
| }, | ||
| func(ctx context.Context, request GetCustomerCreditBalanceRequest) (GetCustomerCreditBalanceResponse, error) { | ||
| _, err := h.customerService.GetCustomer(ctx, customer.GetCustomerInput{ | ||
| CustomerID: &request.CustomerID, | ||
| }) | ||
| if models.IsGenericNotFoundError(err) { | ||
| return GetCustomerCreditBalanceResponse{}, apierrors.NewNotFoundError(ctx, err, "customer") | ||
| } | ||
| if err != nil { | ||
| return GetCustomerCreditBalanceResponse{}, err | ||
| } | ||
|
|
||
| balancesByCurrency, err := h.balanceFacade.GetBalances(ctx, customerbalance.GetBalancesInput{ | ||
| CustomerID: request.CustomerID, | ||
| Currencies: request.Currencies, | ||
| }) | ||
| if err != nil { | ||
| return GetCustomerCreditBalanceResponse{}, err | ||
| } | ||
|
|
||
| balances := make([]api.CreditBalance, 0, len(balancesByCurrency)) | ||
| for _, item := range balancesByCurrency { | ||
| if len(request.Currencies.Codes) == 0 && item.Balance.Settled().IsZero() && item.Balance.Pending().IsZero() { | ||
| continue | ||
| } | ||
|
|
||
| balances = append(balances, mapBalance(item.Currency, item.Balance)) | ||
| } | ||
|
|
||
| return GetCustomerCreditBalanceResponse{ | ||
| RetrievedAt: clock.Now(), | ||
| Balances: balances, | ||
| }, nil | ||
| }, | ||
| commonhttp.JSONResponseEncoderWithStatus[GetCustomerCreditBalanceResponse](http.StatusOK), | ||
| httptransport.AppendOptions( | ||
| h.options, | ||
| httptransport.WithOperationName("get-customer-credit-balance"), | ||
| httptransport.WithErrorEncoder(apierrors.GenericErrorEncoder()), | ||
| )..., | ||
| ) | ||
| } | ||
|
|
||
| func mapBalance(currency currencyx.Code, balance ledger.Balance) api.CreditBalance { | ||
| // Temporary mapping while the v3 credit-balance schema still predates the | ||
| // customerbalance service's settled/live-pending semantics. | ||
| return api.CreditBalance{ | ||
| Currency: api.BillingCurrencyCode(currency), | ||
| Available: balance.Settled().String(), | ||
| Pending: balance.Pending().String(), | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| package customerscredits | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/openmeterio/openmeter/openmeter/customer" | ||
| "github.com/openmeterio/openmeter/openmeter/ledger/customerbalance" | ||
| "github.com/openmeterio/openmeter/pkg/framework/transport/httptransport" | ||
| ) | ||
|
|
||
| type customerBalanceFacade interface { | ||
| GetBalances(ctx context.Context, input customerbalance.GetBalancesInput) ([]customerbalance.BalanceByCurrency, error) | ||
| } | ||
|
|
||
| type Handler interface { | ||
| GetCustomerCreditBalance() GetCustomerCreditBalanceHandler | ||
| } | ||
|
|
||
| type handler struct { | ||
| resolveNamespace func(ctx context.Context) (string, error) | ||
| customerService customer.Service | ||
| balanceFacade customerBalanceFacade | ||
| options []httptransport.HandlerOption | ||
| } | ||
|
|
||
| func New( | ||
| resolveNamespace func(ctx context.Context) (string, error), | ||
| customerService customer.Service, | ||
| balanceFacade customerBalanceFacade, | ||
| options ...httptransport.HandlerOption, | ||
| ) Handler { | ||
| return &handler{ | ||
| resolveNamespace: resolveNamespace, | ||
| customerService: customerService, | ||
| balanceFacade: balanceFacade, | ||
| options: options, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Pendingmeans something different in the public schema.I get the temporary bridge comment, but
balance.Pending()here is the live delta after outstanding charge impacts, whileapi.CreditBalance.Pendingis documented as not-yet-consumable granted credits. That means this endpoint can returnpendingvalues with semantics clients won't expect, potentially even negative ones, so I don't think this field is safe to expose until the response shape catches up.🤖 Prompt for AI Agents