Ver Fonte

Get user email from context instead of request, add more telemetry

Mauricio Araujo há 2 anos atrás
pai
commit
763ba0879f

+ 4 - 8
api/server/handlers/billing/customer.go

@@ -22,11 +22,10 @@ type CreateBillingCustomerHandler struct {
 // NewCreateBillingCustomerIfNotExists will create a new CreateBillingCustomerIfNotExists
 func NewCreateBillingCustomerIfNotExists(
 	config *config.Config,
-	decoderValidator shared.RequestDecoderValidator,
 	writer shared.ResultWriter,
 ) *CreateBillingCustomerHandler {
 	return &CreateBillingCustomerHandler{
-		PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, decoderValidator, writer),
+		PorterHandlerReadWriter: handlers.NewDefaultPorterHandler(config, nil, writer),
 	}
 }
 
@@ -35,11 +34,7 @@ func (c *CreateBillingCustomerHandler) ServeHTTP(w http.ResponseWriter, r *http.
 	defer span.End()
 
 	proj, _ := ctx.Value(types.ProjectScope).(*models.Project)
-
-	request := &types.CreateBillingCustomerRequest{}
-	if ok := c.DecodeAndValidate(w, r, request); !ok {
-		return
-	}
+	user, _ := r.Context().Value(types.UserScope).(*models.User)
 
 	if proj.BillingID != "" {
 		c.WriteResult(w, r, "")
@@ -47,7 +42,7 @@ func (c *CreateBillingCustomerHandler) ServeHTTP(w http.ResponseWriter, r *http.
 	}
 
 	// Create customer in Stripe
-	customerID, err := c.Config().BillingManager.CreateCustomer(ctx, request.UserEmail, proj)
+	customerID, err := c.Config().BillingManager.CreateCustomer(ctx, user.Email, proj)
 	if err != nil {
 		err := telemetry.Error(ctx, span, err, "error creating billing customer")
 		c.HandleAPIError(w, r, apierrors.NewErrInternal(fmt.Errorf("error creating billing customer: %w", err)))
@@ -57,6 +52,7 @@ func (c *CreateBillingCustomerHandler) ServeHTTP(w http.ResponseWriter, r *http.
 	telemetry.WithAttributes(span,
 		telemetry.AttributeKV{Key: "project-id", Value: proj.ID},
 		telemetry.AttributeKV{Key: "customer-id", Value: proj.BillingID},
+		telemetry.AttributeKV{Key: "user-email", Value: user.Email},
 	)
 
 	// Update the project record with the customer ID

+ 6 - 0
api/server/handlers/project/create.go

@@ -64,6 +64,12 @@ func (p *ProjectCreateHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
 			return
 		}
 		proj.BillingID = billingID
+
+		telemetry.WithAttributes(span,
+			telemetry.AttributeKV{Key: "project-id", Value: proj.ID},
+			telemetry.AttributeKV{Key: "customer-id", Value: proj.BillingID},
+			telemetry.AttributeKV{Key: "user-email", Value: user.Email},
+		)
 	}
 
 	proj, _, err = CreateProjectWithUser(p.Repo().Project(), proj, user)

+ 0 - 1
api/server/router/project.go

@@ -441,7 +441,6 @@ func getProjectRoutes(
 
 	getOrCreateBillingCustomerHandler := billing.NewCreateBillingCustomerIfNotExists(
 		config,
-		factory.GetDecoderValidator(),
 		factory.GetResultWriter(),
 	)
 

+ 0 - 5
api/types/billing.go

@@ -1,10 +1,5 @@
 package types
 
-// CreateBillingCustomerRequest is a request for creating a new billing customer.
-type CreateBillingCustomerRequest struct {
-	UserEmail string `json:"user_email" form:"required"`
-}
-
 // PaymentMethod is a subset of the Stripe PaymentMethod type,
 // with only the fields used in the dashboard
 type PaymentMethod = struct {

+ 7 - 2
dashboard/src/lib/hooks/useStripe.tsx

@@ -151,15 +151,20 @@ export const checkIfProjectHasPayment = (): TCheckHasPaymentEnabled => {
 };
 
 export const checkBillingCustomerExists = () => {
-  const { user, currentProject } = useContext(Context);
+  const { currentProject } = useContext(Context);
 
   useQuery(["checkCustomerExists", currentProject?.id], async () => {
     if (!currentProject?.id || currentProject.id === -1) {
       return;
     }
+
+    if (!currentProject?.billing_enabled) {
+      return;
+    }
+
     const res = await api.checkBillingCustomerExists(
       "<token>",
-      { user_email: user?.email },
+      {},
       { project_id: currentProject?.id }
     );
     return res.data;

+ 1 - 3
dashboard/src/shared/api.tsx

@@ -3442,9 +3442,7 @@ const removeStackEnvGroup = baseApi<
 
 // Billing
 const checkBillingCustomerExists = baseApi<
-  {
-    user_email?: string;
-  },
+  {},
   {
     project_id?: number;
   }