Check if user has an existing active subscription based on app_metadata. The presence of stripe_subscription_id or apple_original_transaction_id indicates an ACTIVE subscription. These fields are cleared when subscription ends. Args: app_metadata: User's app_metadata dict
(app_metadata: dict)
| 374 | |
| 375 | |
| 376 | def check_existing_subscription(app_metadata: dict) -> tuple[bool, str | None, str | None]: |
| 377 | """ |
| 378 | Check if user has an existing active subscription based on app_metadata. |
| 379 | |
| 380 | The presence of stripe_subscription_id or apple_original_transaction_id |
| 381 | indicates an ACTIVE subscription. These fields are cleared when subscription ends. |
| 382 | |
| 383 | Args: |
| 384 | app_metadata: User's app_metadata dict (from JWT or Auth0) |
| 385 | |
| 386 | Returns: |
| 387 | (has_subscription, subscription_id, provider) |
| 388 | - has_subscription: True if active subscription exists |
| 389 | - subscription_id: The subscription/transaction ID |
| 390 | - provider: "apple" or "stripe" or None |
| 391 | """ |
| 392 | if not app_metadata or not isinstance(app_metadata, dict): |
| 393 | return False, None, None |
| 394 | |
| 395 | # Enterprise seat. Checked first because an org member is billed through the |
| 396 | # org's Stripe customer, never their own, so no personal ID will be present. |
| 397 | org_id = app_metadata.get('org_id') |
| 398 | if org_id: |
| 399 | return True, org_id, "org" |
| 400 | |
| 401 | # Check Apple first (arbitrary order, doesn't matter since user can only have one) |
| 402 | apple_id = app_metadata.get('apple_original_transaction_id') |
| 403 | if apple_id: |
| 404 | return True, apple_id, "apple" |
| 405 | |
| 406 | # Check Stripe |
| 407 | stripe_id = app_metadata.get('stripe_subscription_id') |
| 408 | if stripe_id: |
| 409 | return True, stripe_id, "stripe" |
| 410 | |
| 411 | return False, None, None |
no outgoing calls
no test coverage detected