Skip to content

Commit

Permalink
Add max reward limit and fix expired plan bug
Browse files Browse the repository at this point in the history
  • Loading branch information
MauAraujo committed May 1, 2024
1 parent cb6f58d commit 85f4806
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
11 changes: 11 additions & 0 deletions api/server/handlers/billing/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down
16 changes: 14 additions & 2 deletions dashboard/src/main/home/project-settings/BillingPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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>
Expand Down

0 comments on commit 85f4806

Please sign in to comment.