Skip to content

Commit

Permalink
Proactively cancel inactive trials 3 days before end
Browse files Browse the repository at this point in the history
  • Loading branch information
flvndvd committed Apr 3, 2024
1 parent 3cb6a54 commit 0148ad8
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
31 changes: 31 additions & 0 deletions front/lib/plans/subscription.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { WorkspaceType } from "@dust-tt/types";
import type { PlanType, SubscriptionType } from "@dust-tt/types";
import type Stripe from "stripe";

import type { Authenticator } from "@app/lib/auth";
import { Plan, Subscription, Workspace } from "@app/lib/models";
Expand All @@ -20,6 +21,7 @@ import {
import { redisClient } from "@app/lib/redis";
import { frontSequelize } from "@app/lib/resources/storage";
import { generateModelSId } from "@app/lib/utils";
import { checkWorkspaceActivity } from "@app/lib/workspace_usage";
import logger from "@app/logger/logger";

// Helper function to render PlanType from PlanAttributes
Expand Down Expand Up @@ -463,3 +465,32 @@ export const updateWorkspacePerMonthlyActiveUsersSubscriptionUsage = async ({
}
}
};

/**
* Proactively cancel inactive trials.
*/
export async function maybeCancelInactiveTrials(
stripeSubscription: Stripe.Subscription
) {
const { id: stripeSubscriptionId } = stripeSubscription;

const subscription = await Subscription.findOne({
where: { stripeSubscriptionId },
include: [Workspace],
});

if (!subscription || !subscription.trialing) {
return;
}

const { workspace } = subscription;
const isWorkspaceActive = await checkWorkspaceActivity(workspace);

if (!isWorkspaceActive) {
logger.info({ workspaceId: workspace.sId }, "Canceling inactive trial.");

await cancelSubscriptionImmediately({
stripeSubscriptionId,
});
}
}
35 changes: 34 additions & 1 deletion front/lib/workspace_usage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { format } from "date-fns/format";
import { QueryTypes } from "sequelize";
import { Op, QueryTypes } from "sequelize";

import type { Workspace } from "@app/lib/models";
import {
AgentConfiguration,
AgentMessage,
Conversation,
DataSource,
} from "@app/lib/models";

import { frontSequelize } from "./resources/storage";

Expand Down Expand Up @@ -94,3 +102,28 @@ export async function unsafeGetUsageData(

return csvHeader + csvContent;
}

/**
* Check if a workspace is active during a trial based on the following conditions:
* - Existence of a connected data source
* - Existence of a custom assistant
* - A conversation occurred within the past 7 days
*/
export async function checkWorkspaceActivity(workspace: Workspace) {
const sevenDaysAgo = new Date();
sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 7);

const hasConnectedDataSource = await DataSource.findOne({
where: { workspaceId: workspace.id },
});

const hasCreatedAssistant = await AgentConfiguration.findOne({
where: { workspaceId: workspace.id },
});

const hasRecentConversation = await Conversation.findOne({
where: { workspaceId: workspace.id, updatedAt: { [Op.gte]: sevenDaysAgo } },
});

return hasConnectedDataSource || hasCreatedAssistant || hasRecentConversation;
}
9 changes: 9 additions & 0 deletions front/pages/api/stripe/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from "@app/lib/email";
import { Plan, Subscription, Workspace } from "@app/lib/models";
import { createCustomerPortalSession } from "@app/lib/plans/stripe";
import { maybeCancelInactiveTrials } from "@app/lib/plans/subscription";
import { countActiveSeatsInWorkspace } from "@app/lib/plans/workspace_usage";
import { frontSequelize } from "@app/lib/resources/storage";
import { generateModelSId } from "@app/lib/utils";
Expand Down Expand Up @@ -562,6 +563,14 @@ async function handler(
}

break;

case "customer.subscription.trial_will_end":
stripeSubscription = event.data.object as Stripe.Subscription;

await maybeCancelInactiveTrials(stripeSubscription);

break;

default:
// Unhandled event type
}
Expand Down

0 comments on commit 0148ad8

Please sign in to comment.