Преглед изворни кода

Add max reward limit and fix expired plan bug

Mauricio Araujo пре 2 година
родитељ
комит
85f4806083

+ 11 - 0
api/server/handlers/billing/create.go

@@ -24,6 +24,8 @@ const (
 	// defaultPaidAmountCents is the amount paid by the user to get the credits
 	// grant, if set to 0 it means they are free
 	defaultPaidAmountCents = 0
+	// maxReferralRewards is the maximum number of referral rewards a user can receive
+	maxReferralRewards = 10
 )
 
 // CreateBillingHandler is a handler for creating payment methods
@@ -140,6 +142,15 @@ func (c *CreateBillingHandler) grantRewardIfReferral(ctx context.Context, referr
 		return nil
 	}
 
+	referralCount, err := c.Repo().Referral().CountReferralsByProjectID(referral.ProjectID, models.ReferralStatusCompleted)
+	if err != nil {
+		return telemetry.Error(ctx, span, err, "failed to get referral count by referrer id")
+	}
+
+	if referralCount >= maxReferralRewards {
+		return nil
+	}
+
 	referrerProject, err := c.Repo().Project().ReadProject(referral.ProjectID)
 	if err != nil {
 		return telemetry.Error(ctx, span, err, "failed to find referrer project")

+ 14 - 2
dashboard/src/main/home/project-settings/BillingPage.tsx

@@ -50,6 +50,19 @@ function BillingPage(): JSX.Element {
 
   const { usage } = useCustomerUsage("day", true);
 
+  const trialEnding = (starting_on: string, ending_before: string,): string => {
+    if (ending_before === undefined) {
+      return "";
+    }
+
+    const diff = dayjs(ending_before).diff(dayjs());
+    if (diff <= 0) {
+      return `Started on  ${readableDate(starting_on)}`
+    }
+
+    return `Free trial ends ${dayjs().to(dayjs(ending_before))}`
+  }
+
   const processedData = useMemo(() => {
     const before = usage;
     const resultMap = new Map();
@@ -235,8 +248,7 @@ function BillingPage(): JSX.Element {
                       {plan.trial_info !== undefined &&
                         plan.trial_info.ending_before !== "" ? (
                         <Text>
-                          Free trial ends{" "}
-                          {dayjs().to(dayjs(plan.trial_info.ending_before))}
+                          {trialEnding(plan.starting_on, plan.trial_info.ending_before)}
                         </Text>
                       ) : (
                         <Text>Started on {readableDate(plan.starting_on)}</Text>