Skip to content

Commit

Permalink
Redirect FREE_TEST_PLAN to paywall + allow trial (#9166)
Browse files Browse the repository at this point in the history
* Revert "Revert "Redirect FREE_TEST_PLAN to paywall + allow trial (#9147)" (#9…"

This reverts commit aab1514.

* fix: add a fetch for the plan

* rename hasPreviouSubscription into hasPreviousSubscription

* refactor: make the condition for hasPrevioussubscription more readable

* refactor: rewrite the condition for hasPreviousSubscription into noPreviousSubscription
  • Loading branch information
aubin-tchoi authored and overmode committed Dec 10, 2024
1 parent 892fe4a commit d613c5f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
2 changes: 1 addition & 1 deletion front/lib/plans/free_plans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ const FREE_PLANS_DATA: PlanAttributes[] = [
maxDataSourcesDocumentsCount: 10,
maxDataSourcesDocumentsSizeMb: 2,
trialPeriodDays: 0,
canUseProduct: true,
canUseProduct: false,
},
{
code: FREE_UPGRADED_PLAN_CODE,
Expand Down
14 changes: 8 additions & 6 deletions front/lib/plans/stripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ import type {
BillingPeriod,
LightWorkspaceType,
Result,
SubscriptionType,
WorkspaceType,
} from "@dust-tt/types";
import type { SubscriptionType } from "@dust-tt/types";
import { Err, isDevelopment, Ok } from "@dust-tt/types";
import { Stripe } from "stripe";

import config from "@app/lib/api/config";
import type { Authenticator } from "@app/lib/auth";
import { Plan, Subscription } from "@app/lib/models/plan";
import { PRO_PLAN_SEAT_29_CODE } from "@app/lib/plans/plan_codes";
import {
isOldFreePlan,
PRO_PLAN_SEAT_29_CODE,
} from "@app/lib/plans/plan_codes";
import { countActiveSeatsInWorkspace } from "@app/lib/plans/usage/seats";
import {
isEnterpriseReportUsage,
Expand Down Expand Up @@ -113,11 +116,10 @@ export const createProPlanCheckoutSession = async ({
// User under the grandfathered free plan are not allowed to have a trial.
let trialAllowed = true;
const existingSubscription = await Subscription.findOne({
where: {
workspaceId: owner.id,
},
where: { workspaceId: owner.id },
include: [Plan],
});
if (existingSubscription) {
if (existingSubscription && !isOldFreePlan(existingSubscription.plan.code)) {
trialAllowed = false;
}

Expand Down
3 changes: 3 additions & 0 deletions front/migrations/20241205_free_test_plan_cant_use_product.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
UPDATE plans
SET "canUseProduct" = false
WHERE "code" = 'FREE_TEST_PLAN';
22 changes: 15 additions & 7 deletions front/pages/w/[wId]/subscribe.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { BarHeader, Button, LockIcon, Page } from "@dust-tt/sparkle";
import { useSendNotification } from "@dust-tt/sparkle";
import {
BarHeader,
Button,
LockIcon,
Page,
useSendNotification,
} from "@dust-tt/sparkle";
import type { BillingPeriod, WorkspaceType } from "@dust-tt/types";
import { CreditCardIcon } from "@heroicons/react/20/solid";
import type { InferGetServerSidePropsType } from "next";
Expand All @@ -11,6 +16,7 @@ import { UserMenu } from "@app/components/UserMenu";
import WorkspacePicker from "@app/components/WorkspacePicker";
import { useSubmitFunction } from "@app/lib/client/utils";
import { withDefaultUserAuthPaywallWhitelisted } from "@app/lib/iam/session";
import { isOldFreePlan } from "@app/lib/plans/plan_codes";
import { useUser } from "@app/lib/swr/user";
import { useWorkspaceSubscriptions } from "@app/lib/swr/workspaces";

Expand Down Expand Up @@ -49,9 +55,11 @@ export default function Subscribe({
React.useState<BillingPeriod>("monthly");

// If you had another subscription before, you will not get the free trial again: we use this to show the correct message.
// Current plan is always FREE_NO_PLAN if you're on this paywall.
// Since FREE_NO_PLAN is not on the database, we check if there is at least 1 subscription.
const hasPreviouSubscription = subscriptions?.length > 0;
// Current plan is either FREE_NO_PLAN or FREE_TEST_PLAN if you're on this paywall.
// FREE_NO_PLAN is not on the database, checking it comes down to having at least 1 subscription.
const noPreviousSubscription =
subscriptions.length === 0 ||
(subscriptions.length === 1 && isOldFreePlan(subscriptions[0].plan.code)); // FREE_TEST_PLAN did not pay, they should be asked to start instead of resume

const { submit: handleSubscribePlan } = useSubmitFunction(
async (billingPeriod) => {
Expand Down Expand Up @@ -125,7 +133,7 @@ export default function Subscribe({
icon={CreditCardIcon}
title="Setting up your subscription"
/>
{hasPreviouSubscription ? (
{!noPreviousSubscription ? (
<>
<Page.P>
<span className="font-bold">
Expand Down Expand Up @@ -184,7 +192,7 @@ export default function Subscribe({
<Button
variant="primary"
label={
hasPreviouSubscription
!noPreviousSubscription
? billingPeriod === "monthly"
? "Resume with monthly billing"
: "Resume with yearly billing"
Expand Down

0 comments on commit d613c5f

Please sign in to comment.