* Fetch organization subscription data
(orgId: string, _signal?: AbortSignal)
| 206 | * Fetch organization subscription data |
| 207 | */ |
| 208 | async function fetchOrganizationSubscription(orgId: string, _signal?: AbortSignal) { |
| 209 | if (!orgId) { |
| 210 | return null |
| 211 | } |
| 212 | |
| 213 | const response = await client.subscription.list({ |
| 214 | query: { referenceId: orgId }, |
| 215 | }) |
| 216 | |
| 217 | if (response.error) { |
| 218 | logger.error('Error fetching organization subscription', { error: response.error }) |
| 219 | return null |
| 220 | } |
| 221 | |
| 222 | // Any paid subscription attached to the org counts as its active sub. |
| 223 | // Priority: Enterprise > Team > Pro (matches `getHighestPrioritySubscription`). |
| 224 | // This intentionally includes `pro_*` plans that have been transferred |
| 225 | // to the org — they are pooled org-scoped subscriptions. |
| 226 | const rawSubscriptions: unknown = response.data |
| 227 | const entitled = (Array.isArray(rawSubscriptions) ? rawSubscriptions : []) |
| 228 | .filter(isOrganizationSubscriptionCandidate) |
| 229 | .filter((sub) => hasPaidSubscriptionStatus(sub.status) && isPaid(sub.plan)) |
| 230 | const enterpriseSubscription = entitled.find((sub) => isEnterprise(sub.plan)) |
| 231 | const teamSubscription = entitled.find((sub) => isTeam(sub.plan)) |
| 232 | const proSubscription = entitled.find((sub) => !isEnterprise(sub.plan) && !isTeam(sub.plan)) |
| 233 | const activeSubscription = enterpriseSubscription || teamSubscription || proSubscription |
| 234 | |
| 235 | return activeSubscription || null |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Hook to fetch organization subscription |
no test coverage detected