Skip to content

Commit

Permalink
Add email notification for expired trials
Browse files Browse the repository at this point in the history
After a couple of customers were surprised to have their accounts deleted, this will now warn customers who are active and have their trials expired.

It also changed the cleanup logic so the status more accurately represents the current account state.
  • Loading branch information
BrunoBernardino committed Oct 11, 2023
1 parent cefa323 commit 83a936e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
49 changes: 46 additions & 3 deletions crons/check-subscriptions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Database, { sql } from '/lib/interfaces/database.ts';
import { getSubscriptions as getStripeSubscriptions } from '/lib/providers/stripe.ts';
import { sendSubscriptionExpiredEmail } from '/lib/providers/brevo.ts';
import { sendSubscriptionExpiredEmail, sendTrialExpiredEmail } from '/lib/providers/brevo.ts';
import { updateUser } from '/lib/data-utils.ts';
import { User } from '/lib/types.ts';
import { User, UserSession } from '/lib/types.ts';

const db = new Database();

Expand Down Expand Up @@ -57,10 +57,53 @@ async function checkSubscriptions() {
}
}

console.log('Updated', updatedUsers, 'users');
console.log('Updated the subscriptions of', updatedUsers, 'users');
} catch (error) {
console.log(error);
}
}

async function checkExpiredTrials() {
const now = new Date();
const sevenDaysAgo = new Date(new Date().setUTCDate(new Date().getUTCDate() - 7));

try {
const users = await db.query<User>(
sql`SELECT * FROM "budgetzen_users" WHERE "status" = 'trial' AND "subscription" ->> 'expires_at' <= $1`,
[
now,
],
);

let updatedUsers = 0;

for (const user of users) {
// TCheck if there's any active session in the last 7 days before sending an email
const userSessions = await db.query<Pick<UserSession, 'id'>>(
sql`SELECT * FROM "budgetzen_user_sessions" WHERE "user_id" = $1 AND "last_seen_at" >= $2`,
[
user.id,
sevenDaysAgo,
],
);

if (userSessions.length > 0) {
await sendTrialExpiredEmail(user.email);
}

user.status = 'inactive';

await updateUser(user);

++updatedUsers;
}

console.log('Marked', updatedUsers, 'users as inactive');
} catch (error) {
console.log(error);
}
}

await checkSubscriptions();

await checkExpiredTrials();
2 changes: 1 addition & 1 deletion crons/cleanup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async function cleanupInactiveUsers() {

try {
const result = await db.query<Pick<User, 'id'>>(
sql`SELECT "id" FROM "budgetzen_users" WHERE "status" IN ('inactive', 'trial') AND "subscription" ->> 'expires_at' <= $1`,
sql`SELECT "id" FROM "budgetzen_users" WHERE "status" = 'inactive' AND "subscription" ->> 'expires_at' <= $1`,
[
sevenDaysAgo.toISOString().substring(0, 10),
],
Expand Down
11 changes: 9 additions & 2 deletions lib/providers/brevo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum BrevoTemplateId {
BUDGETZEN_VERIFY_DELETE = 10,
BUDGETZEN_UPDATE_BILLING_EMAIL = 11,
BUDGETZEN_SUBSCRIPTION_EXPIRED = 12,
BUDGETZEN_TRIAL_EXPIRED = 14,
}

interface BrevoResponse {
Expand All @@ -28,7 +29,7 @@ function getApiRequestHeaders() {

interface BrevoRequestBody {
templateId?: number;
params: Record<string, any>;
params: Record<string, any> | null;
to: { email: string; name?: string }[];
cc?: { email: string; name?: string }[];
bcc?: { email: string; name?: string }[];
Expand Down Expand Up @@ -155,5 +156,11 @@ export async function sendUpdateEmailInProviderEmail(
export async function sendSubscriptionExpiredEmail(
email: string,
) {
await sendEmailWithTemplate(email, BrevoTemplateId.BUDGETZEN_SUBSCRIPTION_EXPIRED, {});
await sendEmailWithTemplate(email, BrevoTemplateId.BUDGETZEN_SUBSCRIPTION_EXPIRED, null);
}

export async function sendTrialExpiredEmail(
email: string,
) {
await sendEmailWithTemplate(email, BrevoTemplateId.BUDGETZEN_TRIAL_EXPIRED, null);
}

0 comments on commit 83a936e

Please sign in to comment.