(
subscription: {
status: string;
id: string;
current_period_end: number | null;
canceled_at: number | null;
items: { data: Array<{ price: {
unit_amount: number | null;
currency: string;
recurring: { interval: string } | null;
id: string;
product: string | { id: string; name?: string };
lookup_key: string | null;
} }> };
},
isPersonal: boolean,
)
| 341 | * Single source of truth for tier resolution — all write paths should use this. |
| 342 | */ |
| 343 | export function buildSubscriptionUpdate( |
| 344 | subscription: { |
| 345 | status: string; |
| 346 | id: string; |
| 347 | current_period_end: number | null; |
| 348 | canceled_at: number | null; |
| 349 | items: { data: Array<{ price: { |
| 350 | unit_amount: number | null; |
| 351 | currency: string; |
| 352 | recurring: { interval: string } | null; |
| 353 | id: string; |
| 354 | product: string | { id: string; name?: string }; |
| 355 | lookup_key: string | null; |
| 356 | } }> }; |
| 357 | }, |
| 358 | isPersonal: boolean, |
| 359 | ): SubscriptionUpdatePayload { |
| 360 | const priceData = subscription.items?.data?.[0]?.price; |
| 361 | const amount = priceData?.unit_amount ?? null; |
| 362 | const currency = priceData?.currency ?? null; |
| 363 | const interval = priceData?.recurring?.interval ?? null; |
| 364 | const lookupKey = priceData?.lookup_key ?? null; |
| 365 | const productRef = priceData?.product; |
| 366 | const productId = typeof productRef === 'string' ? productRef : productRef?.id ?? null; |
| 367 | const productName = typeof productRef === 'object' ? productRef?.name ?? null : null; |
| 368 | |
| 369 | const membershipTier = (TIER_PRESERVING_STATUSES as readonly string[]).includes(subscription.status) |
| 370 | ? (tierFromLookupKey(lookupKey) ?? inferMembershipTier(amount, interval, isPersonal)) |
| 371 | : null; |
| 372 | |
| 373 | return { |
| 374 | subscription_status: subscription.status, |
| 375 | stripe_subscription_id: subscription.id, |
| 376 | subscription_current_period_end: subscription.current_period_end |
| 377 | ? new Date(subscription.current_period_end * 1000) : null, |
| 378 | subscription_amount: amount, |
| 379 | subscription_currency: currency, |
| 380 | subscription_interval: interval, |
| 381 | subscription_canceled_at: subscription.canceled_at |
| 382 | ? new Date(subscription.canceled_at * 1000) : null, |
| 383 | subscription_product_id: productId, |
| 384 | subscription_product_name: productName, |
| 385 | subscription_price_id: priceData?.id ?? null, |
| 386 | subscription_price_lookup_key: lookupKey, |
| 387 | membership_tier: membershipTier, |
| 388 | }; |
| 389 | } |
| 390 | |
| 391 | /** |
| 392 | * Count current seat usage by type for an organization. |
no test coverage detected