Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proactively cancel inactive trials 3 days before end #4553

Merged
merged 6 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT suggestion: Since this will I believe not be reusable anywhere else I would kill and do that directly on the webhook. ff we want to keep here I would just rename to be clearer -> cancelSubscriptionIfInactiveTrial

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I plan to use it in a script to cancel the trials that won't be captured by this change.

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,
});
}
}
30 changes: 29 additions & 1 deletion front/lib/workspace_usage.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { format } from "date-fns/format";
import { QueryTypes } from "sequelize";
import { Op, QueryTypes } from "sequelize";

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

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

Expand Down Expand Up @@ -94,3 +97,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({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want to consider as active if there's a Connection or if there's a Connection or a Folder?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Connection and folder do create a data source 🙃.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seen IRL, fixed.

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
Loading