Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions modules/billing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ e.g. `500 * (1 - 80/100) = 99.99999999999997`, silently missing an exact boundar
comparing the debit's own pre/post balance, and emits `billing.extras.balance_threshold_crossed`
(`{ organizationId, threshold, remaining, planId }`). `billing.email.js` sends a
credit-warning (80%) or credit-exhausted (100%) email off of it.
The subjects use `billing.alerts.balanceUnit` as a plural unit word (default `'credits'`); set it
when your templates speak in another unit, so subject and body agree.

- **Stateless — no `alertedAtN` field.** The weekly `alertedAt80`/`alertedAt100` fields
are scoped to a week and would re-fire every week against a lifetime balance, so this
Expand Down
5 changes: 3 additions & 2 deletions modules/billing/billing.email.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ export const setupBillingEmails = () => {
const appName = mailer.getBrand().name ?? '';
const billingUrl = getBillingUrl();
const isWarning = threshold === 80;
const unit = config.billing?.alerts?.balanceUnit || 'credits';

resolveOrgAdminEmails(organizationId).then((emails) => {
if (!emails.length) return;
Expand All @@ -122,8 +123,8 @@ export const setupBillingEmails = () => {
{
to: email,
subject: isWarning
? `${appName} — your credits are running low`
: `${appName} — you are out of credits`,
? `${appName} — your ${unit} are running low`
: `${appName} — you are out of ${unit}`,
template: isWarning ? 'billing-credit-warning' : 'billing-credit-exhausted',
params: {
remaining: remaining ?? 0,
Expand Down
2 changes: 2 additions & 0 deletions modules/billing/config/billing.development.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,8 @@ const config = {
},
alerts: {
thresholdPercents: [80, 100],
// Plural unit word in the balance-alert mail subjects ("your credits are running low").
balanceUnit: 'credits',
},
events: {
extrasExhausted: 'billing.extras_debit.exhausted',
Expand Down
20 changes: 18 additions & 2 deletions modules/billing/tests/billing.init.email-alerts.unit.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ describe('billing.email setupBillingEmails listeners:', () => {
expect(mockMailer.sendMail).toHaveBeenCalledWith(
expect.objectContaining({
to: 'owner@test.com',
subject: expect.stringContaining('credits are running low'),
subject: 'MyApp — your credits are running low',
template: 'billing-credit-warning',
params: expect.objectContaining({ remaining: 100 }),
}),
Expand All @@ -406,13 +406,29 @@ describe('billing.email setupBillingEmails listeners:', () => {
expect(mockMailer.sendMail).toHaveBeenCalledWith(
expect.objectContaining({
to: 'owner@test.com',
subject: expect.stringContaining('out of credits'),
subject: 'MyApp — you are out of credits',
template: 'billing-credit-exhausted',
params: expect.objectContaining({ remaining: 0 }),
}),
);
});

test('subjects use billing.alerts.balanceUnit when set', async () => {
mockConfig.billing.alerts = { balanceUnit: 'tokens' };

listeners['billing.extras.balance_threshold_crossed']({ organizationId: orgId, threshold: 80, remaining: 10, planId: 'free' });
listeners['billing.extras.balance_threshold_crossed']({ organizationId: orgId, threshold: 100, remaining: 0, planId: 'free' });

await new Promise((r) => setImmediate(r));

expect(mockMailer.sendMail).toHaveBeenCalledWith(
expect.objectContaining({ subject: 'MyApp — your tokens are running low' }),
);
expect(mockMailer.sendMail).toHaveBeenCalledWith(
expect.objectContaining({ subject: 'MyApp — you are out of tokens' }),
);
});

test('subject includes appName from brand (falls back to config.app.title), never the org id/name', async () => {
listeners['billing.extras.balance_threshold_crossed']({
organizationId: orgId,
Expand Down
Loading