@@ -4,14 +4,6 @@ import { createLogger } from '@sim/logger'
44import { isOrgAdminRole } from '@sim/platform-authz/workspace'
55import { generateId } from '@sim/utils/id'
66import { and , eq , isNull , sql } from 'drizzle-orm'
7- import {
8- getEmailSubject ,
9- getLimitEmailSubject ,
10- renderCreditsExhaustedEmail ,
11- renderFreeTierUpgradeEmail ,
12- renderUsageLimitReachedEmail ,
13- renderUsageThresholdEmail ,
14- } from '@/components/emails'
157import { getEffectiveBillingStatus } from '@/lib/billing/core/access'
168import { defaultBillingPeriod } from '@/lib/billing/core/billing-period'
179import {
@@ -45,11 +37,23 @@ import { Decimal, toDecimal, toNumber } from '@/lib/billing/utils/decimal'
4537import { isBillingEnabled } from '@/lib/core/config/env-flags'
4638import { getBaseUrl } from '@/lib/core/utils/urls'
4739import type { DbClient } from '@/lib/db/types'
48- import { sendEmail } from '@/lib/messaging/email/mailer'
4940import { getEmailPreferences } from '@/lib/messaging/email/unsubscribe'
5041
5142const logger = createLogger ( 'UsageManagement' )
5243
44+ /**
45+ * Email rendering pulls the React templates and every mail provider into the
46+ * module graph, which is ~1.2s of imports on every route that reaches billing
47+ * attribution. Load it only when a threshold email is actually being sent.
48+ */
49+ async function loadEmailDelivery ( ) {
50+ const [ emails , mailer ] = await Promise . all ( [
51+ import ( '@/components/emails' ) ,
52+ import ( '@/lib/messaging/email/mailer' ) ,
53+ ] )
54+ return { ...emails , sendEmail : mailer . sendEmail }
55+ }
56+
5357export interface OrgUsageLimitResult {
5458 limit : number
5559 minimum : number
@@ -773,6 +777,7 @@ export async function maybeSendUsageThresholdEmail(params: {
773777 const prefs = await getEmailPreferences ( email )
774778 if ( prefs ?. unsubscribeAll || prefs ?. unsubscribeNotifications ) return
775779
780+ const { renderUsageThresholdEmail, getEmailSubject, sendEmail } = await loadEmailDelivery ( )
776781 const html = await renderUsageThresholdEmail ( {
777782 userName : name ,
778783 planName : params . planName ,
@@ -798,6 +803,7 @@ export async function maybeSendUsageThresholdEmail(params: {
798803 const prefs = await getEmailPreferences ( email )
799804 if ( prefs ?. unsubscribeAll || prefs ?. unsubscribeNotifications ) return
800805
806+ const { renderFreeTierUpgradeEmail, getEmailSubject, sendEmail } = await loadEmailDelivery ( )
801807 const html = await renderFreeTierUpgradeEmail ( {
802808 userName : name ,
803809 percentUsed : Math . min ( 100 , Math . round ( params . percentAfter ) ) ,
@@ -830,6 +836,13 @@ export async function maybeSendUsageThresholdEmail(params: {
830836 const prefs = await getEmailPreferences ( email )
831837 if ( prefs ?. unsubscribeAll || prefs ?. unsubscribeNotifications ) return
832838
839+ const {
840+ renderCreditsExhaustedEmail,
841+ renderUsageLimitReachedEmail,
842+ getEmailSubject,
843+ getLimitEmailSubject,
844+ sendEmail,
845+ } = await loadEmailDelivery ( )
833846 const html = useFreeCopy
834847 ? await renderCreditsExhaustedEmail ( {
835848 userName : name ,
0 commit comments