From 1d42e41911612821ea15ba4b8ba409733e07a8a6 Mon Sep 17 00:00:00 2001 From: Mauricio Araujo Date: Tue, 30 Apr 2024 15:41:08 -0400 Subject: [PATCH] Remove unnecessary changes --- api/server/handlers/billing/credits.go | 59 --------------------- api/server/handlers/billing/plan.go | 46 ++++++++++++++++ api/server/handlers/user/create_test.go | 7 +-- api/server/handlers/user/github_callback.go | 3 ++ api/server/handlers/user/google_callback.go | 3 ++ internal/billing/metronome.go | 10 ---- 6 files changed, 54 insertions(+), 74 deletions(-) delete mode 100644 api/server/handlers/billing/credits.go diff --git a/api/server/handlers/billing/credits.go b/api/server/handlers/billing/credits.go deleted file mode 100644 index a84799de06..0000000000 --- a/api/server/handlers/billing/credits.go +++ /dev/null @@ -1,59 +0,0 @@ -package billing - -import ( - "net/http" - - "github.com/porter-dev/porter/api/server/handlers" - "github.com/porter-dev/porter/api/server/shared" - "github.com/porter-dev/porter/api/server/shared/apierrors" - "github.com/porter-dev/porter/api/server/shared/config" - "github.com/porter-dev/porter/api/types" - "github.com/porter-dev/porter/internal/models" - "github.com/porter-dev/porter/internal/telemetry" -) - -// 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) -} diff --git a/api/server/handlers/billing/plan.go b/api/server/handlers/billing/plan.go index 1b0cc2646a..1d5f0ed70d 100644 --- a/api/server/handlers/billing/plan.go +++ b/api/server/handlers/billing/plan.go @@ -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 diff --git a/api/server/handlers/user/create_test.go b/api/server/handlers/user/create_test.go index 273f0b2429..b130606f57 100644 --- a/api/server/handlers/user/create_test.go +++ b/api/server/handlers/user/create_test.go @@ -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", @@ -47,7 +44,7 @@ func TestCreateUserSuccessful(t *testing.T) { EmailVerified: false, } - gotUser := &types.CreateUserResponse{} + gotUser := &types.CreateUserResponse{} apitest.AssertResponseExpected(t, rr, expUser, gotUser) } diff --git a/api/server/handlers/user/github_callback.go b/api/server/handlers/user/github_callback.go index f6fdb11144..c6fc15c4ed 100644 --- a/api/server/handlers/user/github_callback.go +++ b/api/server/handlers/user/github_callback.go @@ -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)) } @@ -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 } diff --git a/api/server/handlers/user/google_callback.go b/api/server/handlers/user/google_callback.go index 08cdf4beae..472837c59a 100644 --- a/api/server/handlers/user/google_callback.go +++ b/api/server/handlers/user/google_callback.go @@ -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)) } @@ -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 } diff --git a/internal/billing/metronome.go b/internal/billing/metronome.go index 1b7c33c4b0..92cb399cd4 100644 --- a/internal/billing/metronome.go +++ b/internal/billing/metronome.go @@ -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