Skip to content

Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥

fix: make auto-recharge email show actual credit amount instead of hardcoded 1000 (#2485)

devin-ai-integration[bot]5ba418dfb80d
diff --git a/apps/api/src/services/billing/auto_charge.ts b/apps/api/src/services/billing/auto_charge.ts
index 13a4c5cc4..57ca143d7 100644
--- a/apps/api/src/services/billing/auto_charge.ts
+++ b/apps/api/src/services/billing/auto_charge.ts
@@ -326,6 +326,8 @@ async function _autoChargeScale(
chunk.sub_current_period_end,
chunk,
true,
+ false,
+ { autoRechargeCredits: price.credits },
);
logger.info("Scale auto-recharge successful");
@@ -565,6 +567,8 @@ async function _autoChargeSelfServe(
chunk.sub_current_period_end,
chunk,
true,
+ false,
+ { autoRechargeCredits: AUTO_RECHARGE_CREDITS },
);
// Reset ACUC cache to reflect the new credit balance
diff --git a/apps/api/src/services/notification/email_notification.ts b/apps/api/src/services/notification/email_notification.ts
index 9c89765e2..303efbb42 100644
--- a/apps/api/src/services/notification/email_notification.ts
+++ b/apps/api/src/services/notification/email_notification.ts
@@ -10,10 +10,16 @@ import { redlock } from "../redlock";
import { redisEvictConnection } from "../redis";
import { trackEvent } from "../ledger/tracking";
-const emailTemplates: Record<
- NotificationType,
- { subject: string; html: string }
-> = {
+type NotificationContext = {
+ autoRechargeCredits?: number;
+};
+
+type EmailTemplate = {
+ subject: string;
+ html: string | ((ctx: NotificationContext) => string);
+};
+
+const emailTemplates: Record<NotificationType, EmailTemplate> = {
[NotificationType.APPROACHING_LIMIT]: {
subject: "You've used 80% of your credit limit - Firecrawl",
html: "Hey there,<br/><p>You are approaching your credit limit for this billing period. Your usage right now is around 80% of your total credit limit. Consider upgrading your plan to avoid hitting the limit. Check out our <a href='https://firecrawl.dev/pricing'>pricing page</a> for more info.</p><br/>Thanks,<br/>Firecrawl Team<br/>",
@@ -29,7 +35,13 @@ const emailTemplates: Record<
},
[NotificationType.AUTO_RECHARGE_SUCCESS]: {
subject: "Auto recharge successful - Firecrawl",
- html: "Hey there,<br/><p>Your account was successfully recharged with 1000 credits because your remaining credits were below the threshold. Consider upgrading your plan at <a href='https://firecrawl.dev/pricing'>firecrawl.dev/pricing</a> to avoid hitting the limit.</p><br/>Thanks,<br/>Firecrawl Team<br/>",
+ html: (ctx: NotificationContext) => {
+ const creditsText =
+ ctx.autoRechargeCredits != null
+ ? `${ctx.autoRechargeCredits.toLocaleString()} credits`
+ : "additional credits";
+ return `Hey there,<br/><p>Your account was successfully recharged with ${creditsText} because your remaining credits were below the threshold. Consider upgrading your plan at <a href='https://firecrawl.dev/pricing'>firecrawl.dev/pricing</a> to avoid hitting the limit.</p><br/>Thanks,<br/>Firecrawl Team<br/>`;
+ },
},
[NotificationType.AUTO_RECHARGE_FAILED]: {
subject: "Auto recharge failed - Firecrawl",
@@ -73,6 +85,7 @@ export async function sendNotification(
chunk: AuthCreditUsageChunk,
bypassRecentChecks: boolean = false,
is_ledger_enabled: boolean = false,
+ context: NotificationContext = {},
) {
return withAuth(sendNotificationInternal, undefined)(
team_id,
@@ -82,12 +95,14 @@ export async function sendNotification(
chunk,
bypassRecentChecks,
is_ledger_enabled,
+ context,
);
}
async function sendEmailNotification(
email: string,
notificationType: NotificationType,
+ context: NotificationContext = {},
) {
const resend = new Resend(process.env.RESEND_API_KEY);
@@ -139,12 +154,18 @@ async function sendEmailNotification(
return { success: true }; // Return success since this is an expected case
}
+ const template = emailTemplates[notificationType];
+ const html =
+ typeof template.html === "function"
+ ? template.html(context)
+ : template.html;
+
const { error } = await resend.emails.send({
from: "Firecrawl <[email protected]>",
to: [email],
reply_to: "[email protected]",
- subject: emailTemplates[notificationType].subject,
- html: emailTemplates[notificationType].html,
+ subject: template.subject,
+ html,
});
if (error) {
@@ -184,6 +205,7 @@ async function sendNotificationInternal(
chunk: AuthCreditUsageChunk,
bypassRecentChecks: boolean = false,
is_ledger_enabled: boolean = false,
+ context: NotificationContext = {},
): Promise<{ success: boolean }> {
if (team_id === "preview" || team_id.startsWith("preview_")) {
return { success: true };
@@ -260,7 +282,7 @@ async function sendNotificationInternal(
if (!is_ledger_enabled) {
for (const email of emails) {
- await sendEmailNotification(email.email, notificationType);
+ await sendEmailNotification(email.email, notificationType, context);
}
}