def get_phone_subscription_dates()

in privaterelay/fxa_utils.py [0:0]


def get_phone_subscription_dates(social_account):
    subscription_data = get_subscription_data_from_fxa(social_account)
    if "refreshed" in subscription_data.keys():
        # user token refreshed for expanded scope
        social_account.refresh_from_db()
        # retry getting detailed subscription data
        subscription_data = get_subscription_data_from_fxa(social_account)
        if "refreshed" in subscription_data.keys():
            return None, None, None
    if "subscriptions" not in subscription_data.keys():
        # failed to get subscriptions data which may mean user never had subscription
        # and/or there is data mismatch with FxA
        if not flag_is_active_in_task("free_phones", social_account.user):
            # User who was flagged for having phone subscriptions
            # did not actually have phone subscriptions
            logger.error(
                "accounts_subscription_endpoint_failed",
                extra={"fxa_message": subscription_data.get("message", "")},
            )
        return None, None, None

    date_subscribed_phone = start_date = end_date = None
    product_w_phone_capabilites = [settings.PHONE_PROD_ID, settings.BUNDLE_PROD_ID]
    for sub in subscription_data.get("subscriptions", []):
        # Even if a user upgrade subscription e.g. from monthly to yearly
        # or from phone to VPN bundle use the last subscription subscription dates
        # Later, when the subscription details only show one valid subsription
        # this information can be updated
        subscription_created_timestamp = None
        subscription_start_timestamp = None
        subscription_end_timestamp = None
        if sub.get("product_id") in product_w_phone_capabilites:
            subscription_created_timestamp = sub.get("created")
            subscription_start_timestamp = sub.get("current_period_start")
            subscription_end_timestamp = sub.get("current_period_end")
        else:
            # not a product id for phone subscription, continue
            continue

        subscription_date_none = (
            subscription_created_timestamp
            and subscription_start_timestamp
            and subscription_end_timestamp
        ) is None
        if subscription_date_none:
            # subscription dates are required fields according to FxA documentation:
            # https://mozilla.github.io/ecosystem-platform/api#tag/Subscriptions/operation/getOauthMozillasubscriptionsCustomerBillingandsubscriptions
            logger.error(
                "accounts_subscription_subscription_date_invalid",
                extra={"subscription": sub},
            )
            return None, None, None

        date_subscribed_phone = datetime.fromtimestamp(
            subscription_created_timestamp, tz=UTC
        )
        start_date = datetime.fromtimestamp(subscription_start_timestamp, tz=UTC)
        end_date = datetime.fromtimestamp(subscription_end_timestamp, tz=UTC)
    return date_subscribed_phone, start_date, end_date