Skip to content

Commit

Permalink
Notify customers when their subscription expires.
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoBernardino committed Aug 30, 2023
1 parent 73cf615 commit acb1e29
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
9 changes: 9 additions & 0 deletions crons/check-subscriptions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Database, { sql } from '/lib/interfaces/database.ts';
import { getSubscriptions as getStripeSubscriptions } from '/lib/providers/stripe.ts';
import { getPayments as getPaypalPayments } from '/lib/providers/paypal.ts';
import { sendSubscriptionExpiredEmail } from '/lib/providers/postmark.ts';
import { updateUser } from '/lib/data-utils.ts';
import { User } from '/lib/types.ts';

Expand Down Expand Up @@ -43,6 +44,10 @@ async function checkSubscriptions() {
} else if (subscription.status === 'trialing') {
matchingUser.status = 'trial';
} else {
if (matchingUser.status === 'active') {
await sendSubscriptionExpiredEmail(matchingUser.email);
}

matchingUser.status = 'inactive';
}

Expand Down Expand Up @@ -79,6 +84,10 @@ async function checkSubscriptions() {
if (new Date(matchingUser.subscription.expires_at) > now) {
matchingUser.status = 'active';
} else {
if (matchingUser.status === 'active') {
await sendSubscriptionExpiredEmail(matchingUser.email);
}

matchingUser.status = 'inactive';
}

Expand Down
13 changes: 12 additions & 1 deletion lib/providers/postmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ interface PostmarkResponse {
Message: string;
}

type TemplateAlias = 'verify-login' | 'verify-delete' | 'verify-update' | 'update-billing-email';
type TemplateAlias =
| 'verify-login'
| 'verify-delete'
| 'verify-update'
| 'update-billing-email'
| 'subscription-expired';

function getApiRequestHeaders() {
return {
Expand Down Expand Up @@ -151,3 +156,9 @@ export async function sendUpdateEmailInProviderEmail(

await sendEmailWithTemplate(helpEmail, 'update-billing-email', data);
}

export async function sendSubscriptionExpiredEmail(
email: string,
) {
await sendEmailWithTemplate(email, 'subscription-expired', {});
}

0 comments on commit acb1e29

Please sign in to comment.