Skip to content

Commit

Permalink
Send email when canceling trial proactively
Browse files Browse the repository at this point in the history
  • Loading branch information
flvndvd committed Apr 4, 2024
1 parent 742a5f5 commit 4c08b02
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
27 changes: 27 additions & 0 deletions front/lib/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,3 +189,30 @@ export async function sendAdminDataDeletionEmail({
};
return sendEmail(email, msg);
}

export async function sendProactiveTrialCancelledEmail(
email: string
): Promise<void> {
const message = {
from: {
name: "Gabriel Hubert",
email: "[email protected]",
},
subject: "[Dust] Your Pro plan trial has been cancelled early",
html: `<p>Hi,</p>
<p>I'm Gabriel, a cofounder of Dust. Thanks for trying us out with a free trial of the Pro Plan.</p>
<p>You've not used core features of the product (adding data sources, creating custom assistants) and you haven't used assistant conversations in the past 7 days.
As a result, to avoid keeping your payment method on file while you may not intend to convert to our paid plan, we've cancelled your trial ahead of time and won't be charging you.</p>
<p>If you did intend to continue on Dust, you can sign up again and your trial will pick up where you left off, with 3 days to go before converting to our paid plan.
If you'd like to extend further, feel free to just email me.</p>
<p>Thanks again for trying Dust out. If you have a second, please let me know if you have any thoughts about what we could do to improve Dust for your needs!</p>
Gabriel`,
};

return sendEmail(email, message);
}
19 changes: 18 additions & 1 deletion front/lib/plans/subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { PlanType, SubscriptionType } from "@dust-tt/types";
import type Stripe from "stripe";

import type { Authenticator } from "@app/lib/auth";
import { sendProactiveTrialCancelledEmail } from "@app/lib/email";
import { Plan, Subscription, Workspace } from "@app/lib/models";
import type { PlanAttributes } from "@app/lib/plans/free_plans";
import { FREE_NO_PLAN_DATA } from "@app/lib/plans/free_plans";
Expand All @@ -21,6 +22,7 @@ import {
import { redisClient } from "@app/lib/redis";
import { frontSequelize } from "@app/lib/resources/storage";
import { generateModelSId } from "@app/lib/utils";
import { getWorkspaceFirstAdmin } from "@app/lib/workspace";
import { checkWorkspaceActivity } from "@app/lib/workspace_usage";
import logger from "@app/logger/logger";

Expand Down Expand Up @@ -487,10 +489,25 @@ export async function maybeCancelInactiveTrials(
const isWorkspaceActive = await checkWorkspaceActivity(workspace);

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

await cancelSubscriptionImmediately({
stripeSubscriptionId,
});

const firstAdmin = await getWorkspaceFirstAdmin(workspace);
if (!firstAdmin) {
logger.info(
{ action: "cancelling-trial", workspaceId: workspace.sId },
"No first adming found -- skipping email."
);

return;
}

await sendProactiveTrialCancelledEmail(firstAdmin.email);
}
}
19 changes: 19 additions & 0 deletions front/lib/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import type {
} from "@dust-tt/types";

import type { Workspace } from "@app/lib/models";
import { User } from "@app/lib/models";
import { MembershipModel } from "@app/lib/resources/storage/models/membership";

export function renderLightWorkspaceType({
workspace,
Expand All @@ -21,3 +23,20 @@ export function renderLightWorkspaceType({
role,
};
}

// TODO: This belong to the WorkspaceResource.
export async function getWorkspaceFirstAdmin(workspace: Workspace) {
return User.findOne({
include: [
{
model: MembershipModel,
where: {
role: "admin",
workspaceId: workspace.id,
},
required: true,
},
],
order: [["createdAt", "ASC"]],
});
}

0 comments on commit 4c08b02

Please sign in to comment.