Skip to content

Commit

Permalink
Remove unnecessary changes
Browse files Browse the repository at this point in the history
  • Loading branch information
MauAraujo committed Apr 30, 2024
1 parent 75c2941 commit 1d42e41
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 74 deletions.
59 changes: 0 additions & 59 deletions api/server/handlers/billing/credits.go

This file was deleted.

46 changes: 46 additions & 0 deletions api/server/handlers/billing/plan.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,52 @@ func (c *ListPlansHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c.WriteResult(w, r, plan)
}

// ListCreditsHandler is a handler for getting available credits
type ListCreditsHandler struct {
handlers.PorterHandlerWriter
}

// NewListCreditsHandler will create a new ListCreditsHandler
func NewListCreditsHandler(
config *config.Config,
writer shared.ResultWriter,
) *ListCreditsHandler {
return &ListCreditsHandler{
PorterHandlerWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
}
}

func (c *ListCreditsHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx, span := telemetry.NewSpan(r.Context(), "serve-list-credits")
defer span.End()

proj, _ := ctx.Value(types.ProjectScope).(*models.Project)

if !c.Config().BillingManager.MetronomeConfigLoaded || !proj.GetFeatureFlag(models.MetronomeEnabled, c.Config().LaunchDarklyClient) {
c.WriteResult(w, r, "")

telemetry.WithAttributes(span,
telemetry.AttributeKV{Key: "metronome-config-exists", Value: c.Config().BillingManager.MetronomeConfigLoaded},
telemetry.AttributeKV{Key: "metronome-enabled", Value: proj.GetFeatureFlag(models.MetronomeEnabled, c.Config().LaunchDarklyClient)},
)
return
}

credits, err := c.Config().BillingManager.MetronomeClient.ListCustomerCredits(ctx, proj.UsageID)
if err != nil {
err := telemetry.Error(ctx, span, err, "error listing credits")
c.HandleAPIError(w, r, apierrors.NewErrInternal(err))
return
}

telemetry.WithAttributes(span,
telemetry.AttributeKV{Key: "metronome-enabled", Value: true},
telemetry.AttributeKV{Key: "usage-id", Value: proj.UsageID},
)

c.WriteResult(w, r, credits)
}

// ListCustomerUsageHandler returns customer usage aggregations like CPU and RAM hours.
type ListCustomerUsageHandler struct {
handlers.PorterHandlerReadWriter
Expand Down
7 changes: 2 additions & 5 deletions api/server/handlers/user/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ func TestCreateUserSuccessful(t *testing.T) {

handler.ServeHTTP(rr, req)

// Use a struct that is the same as types.User but without the
// referral fields. This is because the referral code is randomly
// generated and is tested separately.
expUser := &types.CreateUserResponse{
expUser := &types.CreateUserResponse{
ID: 1,
FirstName: "Mister",
LastName: "Porter",
Expand All @@ -47,7 +44,7 @@ func TestCreateUserSuccessful(t *testing.T) {
EmailVerified: false,
}

gotUser := &types.CreateUserResponse{}
gotUser := &types.CreateUserResponse{}

apitest.AssertResponseExpected(t, rr, expUser, gotUser)
}
Expand Down
3 changes: 3 additions & 0 deletions api/server/handlers/user/github_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func (p *UserOAuthGithubCallbackHandler) ServeHTTP(w http.ResponseWriter, r *htt
// non-fatal send email verification
if !user.EmailVerified {
err = startEmailVerification(p.Config(), w, r, user)

if err != nil {
p.HandleAPIErrorNoWrite(w, r, apierrors.NewErrInternal(err))
}
Expand Down Expand Up @@ -147,11 +148,13 @@ func upsertUserFromToken(config *config.Config, tok *oauth2.Token) (*models.User
}

user, err = config.Repo.User().CreateUser(user)

if err != nil {
return nil, err
}

err = addUserToDefaultProject(config, user)

if err != nil {
return nil, err
}
Expand Down
3 changes: 3 additions & 0 deletions api/server/handlers/user/google_callback.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func (p *UserOAuthGoogleCallbackHandler) ServeHTTP(w http.ResponseWriter, r *htt
// non-fatal send email verification
if !user.EmailVerified {
err = startEmailVerification(p.Config(), w, r, user)

if err != nil {
p.HandleAPIErrorNoWrite(w, r, apierrors.NewErrInternal(err))
}
Expand Down Expand Up @@ -133,11 +134,13 @@ func upsertGoogleUserFromToken(config *config.Config, tok *oauth2.Token) (*model
}

user, err = config.Repo.User().CreateUser(user)

if err != nil {
return nil, err
}

err = addUserToDefaultProject(config, user)

if err != nil {
return nil, err
}
Expand Down
10 changes: 0 additions & 10 deletions internal/billing/metronome.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,6 @@ const (
defaultCollectionMethod = "charge_automatically"
defaultMaxRetries = 10
porterStandardTrialDays = 15

// referralRewardRequirement is the number of referred users required to
// be granted a credits reward
referralRewardRequirement = 5
// defaultRewardAmountCents is the default amount in USD cents rewarded to users
// who reach the reward requirement
defaultRewardAmountCents = 1000
// defaultPaidAmountCents is the amount paid by the user to get the credits
// grant, if set to 0 it means they were free
defaultPaidAmountCents = 0
)

// MetronomeClient is the client used to call the Metronome API
Expand Down

0 comments on commit 1d42e41

Please sign in to comment.