Skip to content

Commit

Permalink
feat: add support for plans with trial periods
Browse files Browse the repository at this point in the history
  • Loading branch information
Henry Fontanier committed Mar 8, 2024
1 parent e7b1d26 commit bab5782
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
16 changes: 16 additions & 0 deletions front/components/poke/plans/form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export type EditingPlanType = {
maxUsers: string | number;
billingType: FreeBillingType | PaidBillingType;
isNewPlan?: boolean;
trialPeriodDays: number;
};

export const fromPlanType = (plan: PlanType): EditingPlanType => {
Expand All @@ -60,6 +61,7 @@ export const fromPlanType = (plan: PlanType): EditingPlanType => {
dataSourcesDocumentsSizeMb: plan.limits.dataSources.documents.sizeMb,
maxUsers: plan.limits.users.maxUsers,
billingType: plan.billingType,
trialPeriodDays: plan.trialPeriodDays ?? 0,
};
};

Expand Down Expand Up @@ -97,6 +99,7 @@ export const toPlanType = (editingPlan: EditingPlanType): PlanType => {
},
},
billingType: editingPlan.billingType,
trialPeriodDays: parseInt(editingPlan.trialPeriodDays.toString(), 10),
};
};

Expand All @@ -119,6 +122,7 @@ const getEmptyPlan = (): EditingPlanType => ({
maxUsers: "",
isNewPlan: true,
billingType: "fixed",
trialPeriodDays: 0,
});

export const useEditingPlan = () => {
Expand Down Expand Up @@ -281,6 +285,18 @@ export const PLAN_FIELDS = {
title: "Billing",
error: (plan: EditingPlanType) => (plan.billingType ? null : "Required"),
},
trialPeriodDays: {
type: "number",
width: "small",
title: "Trial Days",
error: (plan: EditingPlanType) => {
if (plan.billingType === "free") {
return null;
}

return errorCheckNumber(plan.trialPeriodDays);
},
},
} as const;

type FieldProps = {
Expand Down
5 changes: 5 additions & 0 deletions front/lib/models/plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export class Plan extends Model<
declare name: string;
declare stripeProductId: string | null;
declare billingType: FreeBillingType | PaidBillingType;
declare trialPeriodDays: number | null;

// workspace limitations
declare maxMessages: number;
Expand Down Expand Up @@ -86,6 +87,10 @@ Plan.init(
isIn: [[...FREE_BILLING_TYPES, ...PAID_BILLING_TYPES]],
},
},
trialPeriodDays: {
type: DataTypes.INTEGER,
allowNull: true,
},
maxMessages: {
type: DataTypes.INTEGER,
allowNull: false,
Expand Down
1 change: 1 addition & 0 deletions front/lib/plans/stripe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export const createCheckoutSession = async ({
planCode: planCode,
workspaceId: owner.sId,
},
trial_period_days: plan.trialPeriodDays || undefined,
},
metadata: {
planCode: planCode,
Expand Down
3 changes: 3 additions & 0 deletions front/pages/api/poke/plans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export const PlanTypeSchema = t.type({
t.literal("per_seat"),
t.literal("free"),
]),
trialPeriodDays: t.union([t.number, t.null]),
});

export type UpsertPokePlanResponseBody = {
Expand Down Expand Up @@ -108,6 +109,7 @@ async function handler(
},
},
billingType: plan.billingType,
trialPeriodDays: plan.trialPeriodDays,
}));

const stripeProductIds = plans
Expand Down Expand Up @@ -190,6 +192,7 @@ async function handler(
maxDataSourcesDocumentsSizeMb: body.limits.dataSources.documents.sizeMb,
maxUsersInWorkspace: body.limits.users.maxUsers,
billingType: body.billingType,
trialPeriodDays: body.trialPeriodDays || null,
});
res.status(200).json({
plan: body,
Expand Down
1 change: 1 addition & 0 deletions types/src/front/plan.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export type PlanType = {
limits: LimitsType;
stripeProductId: string | null;
billingType: FreeBillingType | PaidBillingType;
trialPeriodDays: number | null;
};

export type SubscriptionType = {
Expand Down

0 comments on commit bab5782

Please sign in to comment.