Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions pkg/cmd/balance/balance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package balance

import (
"fmt"

"github.com/brevdev/brev-cli/pkg/cmd/cmderrors"
"github.com/brevdev/brev-cli/pkg/cmdcontext"
"github.com/brevdev/brev-cli/pkg/entity"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
"github.com/brevdev/brev-cli/pkg/store"
"github.com/brevdev/brev-cli/pkg/terminal"

"github.com/spf13/cobra"
)

type BalanceStore interface {
GetActiveOrganizationOrDefault() (*entity.Organization, error)
GetBillingProfile(organizationID string) (*store.BillingProfile, error)
}

func NewCmdBalance(t *terminal.Terminal, balanceStore BalanceStore) *cobra.Command {
cmd := &cobra.Command{
Annotations: map[string]string{"organization": ""},
Use: "balance",
DisableFlagsInUseLine: true,
Short: "Show active organization's credit balance",
Long: "Print the credit balance for the active organization.",
Example: `
brev balance
brev balance --org myorg
`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
err := cmdcontext.InvokeParentPersistentPreRun(cmd, args)
if err != nil {
return breverrors.WrapAndTrace(err)
}

return nil
},
Args: cmderrors.TransformToValidationError(cobra.NoArgs),
RunE: func(cmd *cobra.Command, args []string) error {
err := RunBalance(t, balanceStore)
if err != nil {
return breverrors.WrapAndTrace(err)
}
return nil
},
}

return cmd
}

func RunBalance(t *terminal.Terminal, balanceStore BalanceStore) error {
org, err := balanceStore.GetActiveOrganizationOrDefault()
if err != nil {
return breverrors.WrapAndTrace(err)
}
if org == nil {
return fmt.Errorf("no orgs exist")
}

profile, err := balanceStore.GetBillingProfile(org.ID)
if err != nil {
return breverrors.WrapAndTrace(err)
}

if profile == nil || profile.CreditDetails == nil || profile.CreditDetails.RemainingCredits == nil {
return fmt.Errorf("failed to retrieve credit balance")
}

remainingCents := *profile.CreditDetails.RemainingCredits
dollars := float64(remainingCents) / 100.0

t.Vprint(t.Green("✓ Retrieved credit balance\n"))
t.Vprintf(" Organization: %s\n", org.Name)
t.Vprintf(" ID: %s\n", org.ID)
t.Vprintf(" Credits: $%.2f\n", dollars)

return nil
}
61 changes: 61 additions & 0 deletions pkg/cmd/balance/balance_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package balance

import (
"testing"

"github.com/brevdev/brev-cli/pkg/entity"
"github.com/brevdev/brev-cli/pkg/store"
"github.com/brevdev/brev-cli/pkg/terminal"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type fakeBalanceStore struct {
org *entity.Organization
profile *store.BillingProfile
}

func (f fakeBalanceStore) GetActiveOrganizationOrDefault() (*entity.Organization, error) {
return f.org, nil
}

func (f fakeBalanceStore) GetBillingProfile(organizationID string) (*store.BillingProfile, error) {
return f.profile, nil
}

func TestRunBalanceUsesActiveOrgBalance(t *testing.T) {
org := &entity.Organization{ID: "org-1", Name: "test-org"}
remaining := int64(500)
profile := &store.BillingProfile{CreditDetails: &store.CreditDetails{RemainingCredits: &remaining}}
balanceStore := fakeBalanceStore{org: org, profile: profile}

err := RunBalance(terminal.New(), balanceStore)
require.NoError(t, err)
assert.Equal(t, org, balanceStore.org)
assert.NotNil(t, balanceStore.profile)
assert.NotNil(t, balanceStore.profile.CreditDetails)
assert.Equal(t, remaining, *balanceStore.profile.CreditDetails.RemainingCredits)
}

func TestRunBalanceFailsWhenCreditBalanceMissing(t *testing.T) {
org := &entity.Organization{ID: "org-1", Name: "test-org"}

tests := []struct {
name string
profile *store.BillingProfile
}{
{name: "billing profile missing", profile: nil},
{name: "credit details missing", profile: &store.BillingProfile{CreditDetails: nil}},
{name: "remaining credits missing", profile: &store.BillingProfile{CreditDetails: &store.CreditDetails{RemainingCredits: nil}}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
balanceStore := fakeBalanceStore{org: org, profile: tt.profile}

err := RunBalance(terminal.New(), balanceStore)
require.Error(t, err)
assert.Contains(t, err.Error(), "failed to retrieve credit balance")
})
}
}
2 changes: 2 additions & 0 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/brevdev/brev-cli/pkg/auth"
"github.com/brevdev/brev-cli/pkg/cmd/agentskill"
analyticscmd "github.com/brevdev/brev-cli/pkg/cmd/analytics"
"github.com/brevdev/brev-cli/pkg/cmd/balance"
"github.com/brevdev/brev-cli/pkg/cmd/completions"
"github.com/brevdev/brev-cli/pkg/cmd/configureenvvars"
"github.com/brevdev/brev-cli/pkg/cmd/copy"
Expand Down Expand Up @@ -317,6 +318,7 @@ func createCmdTree(cmd *cobra.Command, t *terminal.Terminal, loginCmdStore *stor
cmd.AddCommand(set.NewCmdSet(t, loginCmdStore, noLoginCmdStore))
cmd.AddCommand(ls.NewCmdLs(t, loginCmdStore, noLoginCmdStore))
cmd.AddCommand(org.NewCmdOrg(t, loginCmdStore, noLoginCmdStore))
cmd.AddCommand(balance.NewCmdBalance(t, loginCmdStore))
cmd.AddCommand(invite.NewCmdInvite(t, loginCmdStore))
cmd.AddCommand(secrets.NewCmdSecrets(t, loginCmdStore))

Expand Down
31 changes: 31 additions & 0 deletions pkg/store/organization.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,3 +373,34 @@ func (s AuthHTTPStore) RedeemCouponCode(organizationID string, code string) (*Re

return &result, nil
}

type CreditDetails struct {
RemainingCredits *int64 `json:"remaining_credits"`
TopUpAmount *int64 `json:"top_up_amount"`
TopUpThreshold *int64 `json:"top_up_threshold"`
}

type BillingProfile struct {
BillingProfileID string `json:"billing_profile_id"`
CreditDetails *CreditDetails `json:"credit_details"`
}

// GetBillingProfile fetches the billing profile for the given organization.
// remaining_credits is returned in cents (server-side). Caller should divide by 100 for dollars.
func (s AuthHTTPStore) GetBillingProfile(organizationID string) (*BillingProfile, error) {
var result BillingProfile
path := orgPath + "/" + organizationID + "/billingprofile"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this going through brev-deploy? can we go straight to dev-plane?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @patelspratik ,
We have evaluated two approaches for routing the remaining-credits lookup directly to dev-plane, as outlined in this thread.
Thanks.


res, err := s.authHTTPClient.restyClient.R().
SetHeader("Content-Type", "application/json").
SetResult(&result).
Get(path)
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
if res.IsError() {
return nil, NewHTTPResponseError(res)
}

return &result, nil
}
Loading