* Resolve the recipients that should receive a limit email, with all opt-outs * already applied (the per-user notifications toggle and unsubscribe prefs). * Returning an empty list means "nobody to notify" — the caller then skips the * claim so the dedup state isn't burned without an email going
(
scope: 'user' | 'organization',
params: { userId?: string; userEmail?: string; userName?: string; organizationId?: string }
)
| 110 | * claim so the dedup state isn't burned without an email going out. |
| 111 | */ |
| 112 | async function resolveRecipients( |
| 113 | scope: 'user' | 'organization', |
| 114 | params: { userId?: string; userEmail?: string; userName?: string; organizationId?: string } |
| 115 | ): Promise<LimitEmailRecipient[]> { |
| 116 | if (scope === 'user') { |
| 117 | if (!params.userId || !params.userEmail) return [] |
| 118 | const rows = await db |
| 119 | .select({ enabled: settings.billingUsageNotificationsEnabled }) |
| 120 | .from(settings) |
| 121 | .where(eq(settings.userId, params.userId)) |
| 122 | .limit(1) |
| 123 | if (rows.length > 0 && rows[0].enabled === false) return [] |
| 124 | if (await isUnsubscribed(params.userEmail)) return [] |
| 125 | return [{ email: params.userEmail, name: params.userName }] |
| 126 | } |
| 127 | |
| 128 | if (!params.organizationId) return [] |
| 129 | const admins = await db |
| 130 | .select({ |
| 131 | email: user.email, |
| 132 | name: user.name, |
| 133 | enabled: settings.billingUsageNotificationsEnabled, |
| 134 | role: member.role, |
| 135 | }) |
| 136 | .from(member) |
| 137 | .innerJoin(user, eq(member.userId, user.id)) |
| 138 | .leftJoin(settings, eq(settings.userId, member.userId)) |
| 139 | .where(eq(member.organizationId, params.organizationId)) |
| 140 | |
| 141 | const recipients: LimitEmailRecipient[] = [] |
| 142 | for (const a of admins) { |
| 143 | if (!isOrgAdminRole(a.role)) continue |
| 144 | if (a.enabled === false) continue |
| 145 | if (!a.email) continue |
| 146 | if (await isUnsubscribed(a.email)) continue |
| 147 | recipients.push({ email: a.email, name: a.name || undefined }) |
| 148 | } |
| 149 | return recipients |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Send a usage-limit threshold email (80% warning / 100% reached) for a |
no test coverage detected